#### Archived

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

# Pixel Perfect Tile Finding

This topic is 6199 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## 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 on other sites
Yes. It''s called ray casting.

Ben
http://www.icarusindie.com

##### Share on other sites
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;

1. 1
Rutin
24
2. 2
3. 3
4. 4
JoeJ
18
5. 5

• 14
• 19
• 11
• 11
• 9
• ### Forum Statistics

• Total Topics
631762
• Total Posts
3002180
×