[fpc-pascal] C translation question

José Mejuto joshyfun at gmail.com
Sat Oct 22 12:44:44 CEST 2016

El 22/10/2016 a las 11:59, Ryan Joseph escribió:

>>> j := i + 1;
>> might want this at the end of the loop.
> Doing that defiantly broke it. I think “j” is being set inside the for() construct and before each pass of the loop. The author explains the for loop even but the fact it requires an explanation makes my worry about how easy it is to mess up. :)


It is at the end of the loop for sure, it points to the "previous" point 
in the polygon and in the case of the first testing point the "previous" 
one is the last one.

So the correct code is:

   j := i;

This is my automated C code conversion for that function:

function pnpoly(nvert: Integer; vertx: PointerTo_Single; verty: 
PointerTo_Single; testx: Single; testy: Single): Integer;
   c: Integer = 0;
   j: Integer = 0;
   i: Integer = 0;
   {INITCODE} c := 0;
   {INITCODE} j := 0;
   {INITCODE} i := 0;
   i := 0;
   j := nvert - 1;
   while i < nvert do
     if IsTrue(IsTrue(((verty[i] > testy) <> (verty[j] > testy)))
       IsTrue((testx < (vertx[j] - vertx[i]) * (testy - verty[i])
              div  (verty[j] - verty[i]) + vertx[i]))) then
       c := BooleanNot (c);
     j := PostInc(i);
   exit (c);


More information about the fpc-pascal mailing list