Instantly share code, notes, and snippets.

# aarondandy/line direction N CW Created Sep 30, 2010

What would you like to do?
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. */ var TWOPI = Math.PI * 2; /* 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 / TWOPI; } var noNegDegrees = function(d){ while(d < 0){ d += 360; } return d; } alert(noNegDegrees(radiansToDegrees(lineDirection(4, 3, 2, 1))));

### 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.
Owner

### aarondandy commented Jun 10, 2014

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