Search code examples
javascriptpaperjs

PaperJS - angle between two points


Any idea how one can get the angle between two path segments in paper js?

I've tried point1.getDirectedAngle(point2), and I don't quite understand the returned value, as it doesn't correspond to the real angle.


Solution

  • getAngle(point), getAngleInRadians(point) and getDirectedAngle(point) all consider the given points as vectors.

    The result is in degree except for getAngleInRadians().

    So when you do

     point1.getDirectedAngle(point2)
    

    point1 must be the vector from your current point to the previous point, and point2 must be the vector from your current point to the next point:

    for segment in path.segments
        point1 = segment.previous.point.subtract(segment.point)
        point2 = segment.next.point.subtract(segment.point)
        angle = point1.getDirectedAngle(point2)