Re: finding if point is inside irregular rectangle
- Posted by Jiri Babor <jbabor at PARADISE.NET.NZ> Feb 28, 2002
- 441 views
Tone, looking at my five year old code, unearthed by Thomas, I realized the function can be simplified and, hopefully, made a bit faster as well. jiri function inpoly(sequence poly, integer x, integer y) -- return 1 (true) if point x,y is inside polygon integer m,n,x1,x2,y1,y2 m = length(poly) n = 0 x2 = poly[m][1] y2 = poly[m][2] for i = 1 to m do x1 = x2 y1 = y2 if x = x1 and y = y1 then -- vertices are tricky... return 1 end if x2 = poly[i][1] y2 = poly[i][2] if y1 = y2 and y = y1 then -- horizontal side if y = y1 then return (x1<=x and x2>=x) or (x2<=x and x1>=x) end if elsif (y1 < y and y2 > y) or (y2 < y and y1 > y) then if x > x1 + (x2-x1)*(y-y1)/(y2-y1) then n += 1 end if end if end for return and_bits(n, 1) -- odd end function