![](https://img.haomeiwen.com/i2882689/457561e5c96f6679.png)
-- 屏幕点 与椭圆的关系
function HomelandTraceManagerHelper:CheckInEllipse(x,y)
local ori = Vector2( self._screenhfWidth, self._screenhfHeight)
local xredis = self._ellipseAxlex -- 长轴
local yredis = self._ellipseAxley -- 短轴
x = x - ori.x
y = y - ori.y
return (x)* (x)/(xredis*xredis) + (y)* (y)/(yredis*yredis)
end
![](https://img.haomeiwen.com/i2882689/49e636ed592ec0a2.png)
--- 直线 椭圆交点
function HomelandTraceManagerHelper:CheckIntersect(lineStartPoint ,lineEndPoint)
local offset = Vector2( self._screenhfWidth, self._screenhfHeight)
local crossPoint1 ,crossPoint2
lineEndPoint = lineEndPoint - offset
lineStartPoint = Vector2(0,0)
local k, c
k = (lineEndPoint.y - lineStartPoint.y) / (lineEndPoint.x - lineStartPoint.x)
c = lineStartPoint.y - k * lineStartPoint.x
local xredis = self._ellipseAxlex
local yredis = self._ellipseAxley
local media = ((2 * xredis * xredis * k * c) * (2 * xredis * xredis * k * c) - 4 * (yredis * yredis + k * k * xredis * xredis) * xredis * xredis * (c * c - yredis * yredis))
if media > 0 then
crossPoint1 = Vector2(0,0)
crossPoint2 = Vector2(0,0)
crossPoint1.x = (-2 * xredis * xredis * k * c + math.sqrt(media)) / (2 * (yredis * yredis + k * k * xredis * xredis))
crossPoint1.y = k * crossPoint1.x + c
crossPoint2.x = (-2 * xredis * xredis * k * c - math.sqrt(media)) / (2 * (yredis * yredis + k * k * xredis * xredis))
crossPoint2.y = k * crossPoint2.x + c
crossPoint1 = crossPoint1 + offset
crossPoint2 = crossPoint2 + offset
elseif media == 0 then
crossPoint1 = Vector2(0,0)
crossPoint1.x = (-2 * xredis * xredis * k * c + math.sqrt(media)) / (2 * (yredis * yredis + k * k * xredis * xredis))
crossPoint1.y = k * crossPoint1.x + c
crossPoint1 = crossPoint1 + offset
else
end
-- screen 坐标
return crossPoint1 ,crossPoint2 ,k
end
![](https://img.haomeiwen.com/i2882689/cf31fe5d35e588f9.png)
-- 判断 两个交点哪个正确 是否在线段内部
function HomelandTraceManagerHelper:CheckInLine(checkPoint ,startPoint ,endPoint )
if not checkPoint then
return
end
local vec1 = endPoint - checkPoint
local vec2 = checkPoint - startPoint
return Vector2.Dot(vec1,vec2) > 0
end
网友评论