Archived

This topic is now archived and is closed to further replies.

David Rostowsky

Pixel Perfect Tile Finding

Recommended Posts

Hi, I''m trying to figure out if there is some fancy math formula I can chug my mouse x,y through to figure out which tile Im in with pixel perfect accuracy. Im using 64x32 isometric tiles. Thanks in advance! Dave

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
Hi,

The following code is in Delphi-Pascal, but the concept is mathematical, so it should be easy to convert.

Greetz,

Eddy Sterckx


{**********************************************************************************}
Function PointInPolygon(CheckPoint : TPoint; PollyArray : Array of TPoint) : Boolean;
{**********************************************************************************}
Var
X0, Y0, X1, Y1, X2, Y2 : Integer;
i : Integer;
LastPointIndex : Integer;
Begin
// Decide whether or not a point lies within a convex polygon
// Polygon coordinate points must be given in clockwise fashion

Result := True; // default : point is in polygon until otherwise proven

X0 := CheckPoint.X;
Y0 := CheckPoint.Y;

LastPointIndex := High(PollyArray);

// take every "line" in turn and check on which "side" of that line lies
// the point to check. If for every line-pair the point to check is on
// the "inside" than the point lies in the polygon.
For i := 0 to LastPointIndex do
Begin
{First point of the line}
X1 := PollyArray.X;
Y1 := PollyArray[i].Y;
{Next point}
If i <> LastPointIndex Then
Begin
X2 := PollyArray[i+1].X;
Y2 := PollyArray[i+1].Y;
end
else
begin
X2 := PollyArray[0].X;
Y2 := PollyArray[0].Y;
end;
// this line does it all !
Result := (Y1 * X2) - (X1 * Y2) - (Y0 * X2) + (X0 * Y2) + (Y0 * X1) - (X0 * Y1) <= 0;

If Result = False Then // optimize a bit
Break;
end;
end;

Share this post


Link to post
Share on other sites