[added] intersect.point_in_poly and required plumbing

This commit is contained in:
Max Cahill 2020-04-01 19:28:22 +11:00
parent ef25d12b1b
commit c7d96461cf
2 changed files with 32 additions and 0 deletions

View File

@ -397,4 +397,25 @@ function intersect.aabb_aabb_collide_continuous(
return false return false
end end
--check if a point is in a polygon
--point is the point to test
--poly is a list of points in order
--based on winding number, so re-intersecting areas are counted as solid rather than inverting
function intersect.point_in_poly(point, poly)
local wn = 0
for i, a in ipairs(poly) do
local b = poly[i + 1] or poly[1]
if a.y <= point.y then
if b.y > point.y and vec2.winding_side(a, b, point) > 0 then
wn = wn + 1
end
else
if b.y <= point.y and vec2.winding_side(a, b, point) < 0 then
wn = wn - 1
end
end
end
return wn ~= 0
end
return intersect return intersect

View File

@ -532,6 +532,17 @@ function vec2.vrej(a, b)
return a:copy():vreji(b) return a:copy():vreji(b)
end end
--get the winding side of p, relative to the line a-b
-- (this is based on the signed area of the triangle a-b-p)
-- return value:
-- >0 when p left of line
-- =0 when p on line
-- <0 when p right of line
function vec2.winding_side(a, b, p)
return (b.x - a.x) * (p.y - a.y)
- (p.x - a.x) * (b.y - a.y)
end
----------------------------------------------------------- -----------------------------------------------------------
-- vector extension methods for special purposes -- vector extension methods for special purposes
-- (any common vector ops worth naming) -- (any common vector ops worth naming)