Skip to content

Instantly share code, notes, and snippets.

@aarondandy
Last active October 12, 2021 18:10
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save aarondandy/604912 to your computer and use it in GitHub Desktop.
Calculates the north relative clockwise orientation of a line.
/*
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))));
Copy link

ghost commented May 29, 2014

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.

@aarondandy
Copy link
Author

That code is JavaScript but it should be easy to translate to any language as it is a simple function.

@Coderx7
Copy link

Coderx7 commented Oct 12, 2021

Thanks but a much better solution would be the one presented here .
This doesn't work in a reliable manner.

@aarondandy
Copy link
Author

This doesn't work in a reliable manner.

What inputs reproduce the bug and what are the expected outputs?

@aarondandy
Copy link
Author

I don't think atan2 requires normalization so some floating point error may be introduced, maybe that is what you mean 🤷‍♂️

@Coderx7
Copy link

Coderx7 commented Oct 12, 2021

have you tried something like line((0,0), (256,0))

@aarondandy
Copy link
Author

I get 90 in my browsers as that would be 90 degrees clockwise from north. What did you get?

@Coderx7
Copy link

Coderx7 commented Oct 12, 2021

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