Skip to content

Commit b9845ea

Browse files
committed
math扩展模块添加一个公共领域方法pointInPolygon close #3
1 parent bcb2390 commit b9845ea

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

mathExtend.lua

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,30 @@ function MATH.interpolate(x,x1,y1,x2,y2)
6363
return y1+(x-x1)*(y2-y1)/(x2-x1)
6464
end
6565

66+
-- By Pedro Gimeno,donated to the public domain
67+
function MATH.pointInPolygon(x,y,poly,evenOddRule)
68+
local x1,y1,x2,y2
69+
local len=#poly
70+
x2,y2=poly[len-1],poly[len]
71+
local wn=0
72+
for idx=1,len,2 do
73+
x1,y1=x2,y2
74+
x2,y2=poly[idx],poly[idx+1]
75+
if y1>y then
76+
if y2<=y and (x1-x)*(y2-y)<(x2-x)*(y1-y) then
77+
wn=wn+1
78+
end
79+
else
80+
if y2>y and (x1-x)*(y2-y)>(x2-x)*(y1-y) then
81+
wn=wn-1
82+
end
83+
end
84+
end
85+
if evenOddRule then
86+
return wn%2~=0
87+
else-- non-zero winding rule
88+
return wn~=0
89+
end
90+
end
91+
6692
return MATH

0 commit comments

Comments
 (0)