[fpc-pascal] C translation question

Ryan Joseph ryan at thealchemistguild.com
Sat Oct 22 11:06:04 CEST 2016


I’m trying to translate a function from C (taken from http://www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/pnpoly.html) and although I think I got it correctly it’s not behaving 100% accurately so I’m not sure if it was my translation or not. The “for” statement is pretty confusing to my eyes despite the author of the function giving a description of his code.

Does anyone spot any errors with my translation?

int pnpoly(int nvert, float *vertx, float *verty, float testx, float testy)
{
  int i, j, c = 0;
  for (i = 0, j = nvert-1; i < nvert; j = i++) {
    if ( ((verty[i]>testy) != (verty[j]>testy)) &&
     (testx < (vertx[j]-vertx[i]) * (testy-verty[i]) / (verty[j]-verty[i]) + vertx[i]) )
       c = !c;
  }
  return c;
}


function TPolygonHelper.ContainsPoint (point: TPoint): boolean;
var
	i, j, c: integer;
begin
	i := 0;
	j := high(self);
	c := 0;
	for i := 0 to high(self) do
		begin
			j := i + 1;
			if ((self[i].y > point.y) <> (self[j].y > point.y)) 
					and (point.x < (self[j].x - self[i].x) * (point.y - self[i].y) / (self[j].y - self[i].y) + self[i].x) then
				c := not c;
		end;
	result := c <> 0;
end;

Regards,
	Ryan Joseph




More information about the fpc-pascal mailing list