Posted By

jatkins on 08/24/15


Tagged

math line lines angle Geometry linesegment


Versions (?)

Get Angle between Two Touching Line Segments


 / Published in: JavaScript
 

Released into the public domain by Josh Atkins, 2015.

  1. function pythagSquared(point1, point2) {
  2. return Math.pow(point1.x - point2.x, 2) + Math.pow(point1.y - point2.y, 2);
  3. }
  4.  
  5. function pythag(point1, point2) {
  6. return Math.sqrt(sniktaDraw.misc.pythagSquared(point1, point2));
  7. }
  8.  
  9. function getAngleBetweenTwoTouchingLineSegments(touchPoint, line1EndPoint, line2EndPoint) {
  10. var line2EndPointVector = {x: line2EndPoint.x - touchPoint.x, y: line2EndPoint.y - touchPoint.y},
  11. line1EndPointVector = {x: line1EndPoint.x - touchPoint.x, y: line1EndPoint.y - touchPoint.y},
  12. aSq = pythagSquared(line1EndPoint, line2EndPoint),
  13. b = pythag(line2EndPoint, touchPoint),
  14. c = pythag(line1EndPoint, touchPoint),
  15. crossProduct = line2EndPointVector.x * line1EndPointVector.y - line1EndPointVector.x * line2EndPointVector.y;
  16.  
  17. return (crossProduct < 0 ? -1 : 1) * Math.acos((b * b + c * c - aSq) / (2 * b * c)); // cosine rule
  18. }

Report this snippet  

You need to login to post a comment.