Pixel Perfect Tile Finding
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
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.Y;<br> {Next point}<br> If i <> LastPointIndex Then<br> Begin<br> X2 := PollyArray[i+1].X;<br> Y2 := PollyArray[i+1].Y;<br> end<br> else<br> begin<br> X2 := PollyArray[0].X;<br> Y2 := PollyArray[0].Y;<br> end;<br> // this line does it all !<br> Result := (Y1 * X2) - (X1 * Y2) - (Y0 * X2) + (X0 * Y2) + (Y0 * X1) - (X0 * Y1) <= 0;<br><br> If Result = False Then // optimize a bit<br> Break;<br> end;<br>end;<br><br> </i>
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.Y;<br> {Next point}<br> If i <> LastPointIndex Then<br> Begin<br> X2 := PollyArray[i+1].X;<br> Y2 := PollyArray[i+1].Y;<br> end<br> else<br> begin<br> X2 := PollyArray[0].X;<br> Y2 := PollyArray[0].Y;<br> end;<br> // this line does it all !<br> Result := (Y1 * X2) - (X1 * Y2) - (Y0 * X2) + (X0 * Y2) + (Y0 * X1) - (X0 * Y1) <= 0;<br><br> If Result = False Then // optimize a bit<br> Break;<br> end;<br>end;<br><br> </i>
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement