--定义一个多边形
local Polygon = class('Polygon')
function Polygon:Ctor(point)
self.point = point
end
function Polygon:VectorNumber()
return #self.point
end
function Polygon:GetVector(index)
return self.point[index];
end
--多边形与点的碰撞
function PolygonToPoint(polygon,point)
local vectorNum = polygon:VectorNumber();
local collision = false
local next = 0
for current = 0 , vectorNum do
if (current == vectorNum - 1) then
next = 0
else
next = current + 1
end
local vc = polygon:GetVector(current);
local vn = polygon:GetVector(next);
if PointToLine(point,vc, vn) then
return true
end
--若尔当曲线定理 需要的同学可以去学习一下
if (((vc.y >= py and vn.y < py) or (vc.y < py and vn.y >= py)) and
(px < (vn.x - vc.x) * (py - vc.y) / (vn.y - vc.y) + vc.x)) then
collision = not collision;
end
end
return collision
end
--点与直线的碰撞
function PointToLine(point , linePoint1, linePoint2)
--思想点与线两边线段的距离跟线的距离的判断,相等就是在同一直线
local line1 = math.sqrt((linePoint1.x - point.x) * (linePoint1.x - point.x) + (linePoint1.y - point.y)* (linePoint1.y - point.y))
local line2 = math.sqrt((linePoint2.x - point.x) * (linePoint2.x - point.x) + (linePoint2.y - point.y)* (linePoint2.y - point.y))
local line = math.sqrt((linePoint2.x - linePoint1.x) * (linePoint2.x - linePoint1.x) + (linePoint2.y - linePoint1.y)* (linePoint2.y - linePoint1.y))
return (line1 + line2) == line
end