Search code examples
javascriptalgorithmcanvasdrawinggraph-algorithm

Algorithm to Move / Rotate Points


What is the algorithm to move / "rotate" a point in the (x, y) coordinate by x degrees relative to (0, 0)? For example, in the picture below, I want to move point A to B, by x degrees; the distance between A and (0, 0) should be the same between B and (0, 0).

I need to do in a front-end environment i.e. JavaScript.

enter image description here


Solution

  • B.x = A.x * cos(x) - A.y * sin(x)
    B.y = A.x * sin(x) + A.y * cos(x)
    

    x is assumed to be in radians, you must convert otherwise.