Skip to content

Instantly share code, notes, and snippets.

@rowanwins
Last active April 29, 2020 22:55
Show Gist options
  • Save rowanwins/3edee82cd2b1acc4cee125e9a014fe40 to your computer and use it in GitHub Desktop.
Save rowanwins/3edee82cd2b1acc4cee125e9a014fe40 to your computer and use it in GitHub Desktop.
function radiansToDegrees (radians) {
return radians * (180 / Math.PI)
}
function angleToPoint (p, otherPoint) {
const angleRadians = Math.atan2(otherPoint.y - p.y, otherPoint.x - p.x)
return radiansToDegrees(angleRadians)
}
function scenario1 (anglePrev, angleNext, currentPBearing) {
return anglePrev < currentPBearing && currentPBearing < angleNext
}
function scenario2 (anglePrev, angleNext, currentPBearing) {
return currentPBearing > anglePrev || currentPBearing < angleNext
}
const anglePrev = angleToPoint(centerPoint, prevPoint)
const angleNext = angleToPoint(centerPoint, nextPoint)
const angleToTestPoint = angleToPoint(centerPoint, pointToTest)
const requiredCalculation = anglePrev < angleNext ? scenario1 : scenario2
const isInRange = requiredCalculation(anglePrev, angleNext, angleToTestPoint)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment