a simple edge-crossing algorithm here:
http://alienryderflex.com/polygon/
/ Globals which should be set before calling this function:
//
// int polySides = how many corners the polygon has
// float polyX[] = horizontal coordinates of corners
// float polyY[] = vertical coordinates of corners
// float x, y = point to be tested
//
// (Globals are used in this example for purposes of speed. Change as
// desired.)
//
// The function will return YES if the point x,y is inside the
polygon, or
// NO if it is not. If the point is exactly on the edge of the
polygon,
// then the function may return YES or NO.
//
// Note that division by zero is avoided because the division is
protected
// by the “if” clause which surrounds it.
bool pointInPolygon() {
int i, j=polySides-1 ;
boolean oddNodes=NO ;
for (i=0; i<polySides; i++) {
if (polyY[i]=y
|| polyY[j]=y) {
if (polyX[i]+(y-polyY[i])/(polyY[j]-polyY[i])*(polyX[j]-polyX
[i])<x) {
oddNodes=!oddNodes; }}
j=i; }
return oddNodes; }
—–
This is easily translated it to REALbasic:
I use it as a "contains" method in my "polygon" class. Polygon is a
subclass of a figureshape
—–
contains (x as integer, y as integer) returns boolean
//relies on three polygon properties that are set when the polygon
is made:
//polyX() – a 1d array of the x values of the polygon corners
//PolyY() – a 1d array of the y values of the polygon corners
//Polysides – number of polygon sides
dim oddNodes as Boolean
dim i,j as integer
j=Polysides-1
oddNodes=false
for i=0 to Polysides-1
if (polyY(i)=y) or (polyY(j)=y)
then
if (polyx(i)+(y-polyY(i))/(polyY(j)-polyY(i))*(polyx(j)-polyX
(i))<x) then
oddNodes=not oddNodes
end if
end if
j=i
next
Return(oddNodes)

You must be logged in to post a comment.