Last active
October 12, 2021 18:10
-
-
Save aarondandy/604912 to your computer and use it in GitHub Desktop.
Calculates the north relative clockwise orientation of a line.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
The MIT License | |
Copyright (c) 2010 Aaron Dandy (aaron.dandy@gmail.com) | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in | |
all copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
THE SOFTWARE. | |
*/ | |
/* | |
Function: lineDirection | |
Calculates the north relative clockwise orientation of a line. | |
Parameters: | |
ax - The x-coordinate of the first point defining a line. | |
ay - The y-coordinate of the first point defining a line. | |
bx - The x-coordinate of the second point defining a line. | |
by - The y-coordinate of the second point defining a line. | |
Returns: | |
The direction of the line or Number.NaN on failure. | |
*/ | |
var lineDirection = function (ax, ay, bx, by) { | |
var dx = bx - ax; | |
var dy = by - ay; | |
var m = Math.sqrt((dx * dx) + (dy * dy)); | |
return m ? Math.atan2(dx / m, dy / m) : Number.NaN; | |
} | |
var radiansToDegrees = function (r){ | |
return 180 * r / Math.PI; | |
} | |
var noNegDegrees = function(d){ | |
while(d < 0){ | |
d += 360; | |
} | |
return d; | |
} | |
alert(noNegDegrees(radiansToDegrees(lineDirection(4, 3, 2, 1)))); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Interesting, this actually crashed on my side!
Edit:
Thanks a lot, yes, it works fine, the issue was in my own code.