How to calculate rotation in 2D in Javascript

前端 未结 5 1817
旧巷少年郎
旧巷少年郎 2020-12-07 17:07

I am not so familiar trigonometry, but I have only two points to rotate in 2D:

                    *nx, ny
               .     -
          .           -
            


        
5条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-07 17:24

    1. First, translate the rotation center to the origin
    2. Calculate the new coordinates (nx, ny)
    3. Translate back to the original rotation center

    Step 1

    Your new points are

    1. center: (0,0)
    2. point: (x-cx, y-cy)

    Step 2

    1. nx = (x-cx)*cos(theta) - (y-cy)*sin(theta)
    2. ny = (y-cy)*cos(theta) + (x-cx)*sin(theta)

    Step 3

    Translate back to original rotation center:

    1. nx = (x-cx)*cos(theta) - (y-cy)*sin(theta) + cx
    2. ny = (y-cy)*cos(theta) + (x-cx)*sin(theta) + cy

    For deeper explanation, with some fancy diagrams, I recommend looking at this.

提交回复
热议问题