Game Development Reference
In-Depth Information
Using pointInPolygon
You can also determine whether a point is inside a polygon. The following code (adapted from the
functions in the articles “Determining if a point lies on the interior of a polygon,” by Paul Bourke
[ http://paulbourke.net/geometry/insidepoly/ ] and “Point in Polygon Strategies,” by Eric Haynes
[ http://erich.realtimerendering.com/ptinpoly/ ] enables you to do this:
function pointInPolygon(x,y,poly)
n = #poly
inside = false
p1x,p1y = poly[1], poly[2]
for i=1,n-2, 2 do
p2x,p2y = poly[i+2], poly[i+3]
if y > math.min(p1y,p2y) then
if y <= math.max(p1y,p2y) then
if x <= math.max(p1x,p2x) then
if p1y ~= p2y then
xinters = (y-p1y)*(p2x-p1x)/(p2y-p1y)+p1x
end
if p1x == p2x or x <= xinters then
inside = not inside
end
end
end
end
p1x,p1y = p2x,p2y
end
return inside
end
Other Generic Functions
This section examines some additional functions that you might find useful in your Lua development
projects.
Comparing Boolean Values
Another common task that you might want to accomplish in Lua is comparing Boolean values. At
first glance, you might think that these two commands are the same:
if someValueOn then print("On") end
and
if someValueOn==true then print("On") end
 
Search WWH ::




Custom Search