Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function GetDistanceToPointBetweenCoords(pos, start, stop)
- local startDist, stopDist, length = #(pos - start), #(pos - stop), #(start - stop)
- return GetDistanceToLine(startDist, stopDist, length)
- end
- function GetDistanceToPointAlongCoords(pos, start, stop)
- local startDist, stopDist, length = #(pos - start), #(pos - stop), #(start - stop)
- return GetDistanceToLineInf(startDist, stopDist, length)
- end
- function GetDistanceToLine(startDist, stopDist, length)
- if math.max(startDist, stopDist) > length then
- return math.min(startDist, stopDist)
- end
- return GetDistanceToLineInf(startDist, stopDist, length)
- end
- function GetDistanceToLineInf(startDist, stopDist, length)
- local s = (1/2) * (startDist + stopDist + length)
- local area = math.sqrt(s * (s - startDist) * (s - stopDist) * (s - length))
- return area / (length / 2)
- end
- -- Get angle between two coords via an origin point
- -- https://i.imgur.com/2GVvt5I.png
- -- Returns a negative value is target is on the left side of the line
- function CalculateAngleBetweenPoints(origin, line, target)
- local p1x, p1y = origin.x, origin.y
- local p2x, p2y = line.x, line.y
- local p3x, p3y = target.x, target.y
- local numerator = p2y*(p1x-p3x)+p1y*(p3x-p2x)+p3y*(p2x-p1x)
- local denominator = (p2x-p1x)*(p1x-p3x)+(p2y-p1y)*(p1y-p3y)
- local ratio = numerator / denominator
- local anglerad = math.atan(ratio)
- local angledeg = (anglerad*180)/math.pi
- return angledeg
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement