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 ([email protected]) | |
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)))); |
That code is JavaScript but it should be easy to translate to any language as it is a simple function.
Thanks but a much better solution would be the one presented here .
This doesn't work in a reliable manner.
This doesn't work in a reliable manner.
What inputs reproduce the bug and what are the expected outputs?
I don't think atan2 requires normalization so some floating point error may be introduced, maybe that is what you mean 🤷♂️
have you tried something like line((0,0), (256,0))
I get 90 in my browsers as that would be 90 degrees clockwise from north. What did you get?
Interesting, this actually crashed on my side!
Edit:
Thanks a lot, yes, it works fine, the issue was in my own code.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi there! I am really interested in using this code, but I'm not getting which language is it based on (I'm not an experet in this...). If You could please enlight me I would be very happy! Thanks in advance, cheers.