# How to calculate rotation in 2D in Javascript

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

```                    *nx, ny
.     -
.           -
.  angle          -
*cx,cy.................*x,y
```

cx, cy = rotation center
x,y = current x,y
nx, ny = new coordinates

How to calculate new points in a certain angle?

```function rotate(cx, cy, x, y, angle) {
var radians = (Math.PI / 180) * angle,
nx = (cos * (x - cx)) + (sin * (y - cy)) + cx,
ny = (cos * (y - cy)) - (sin * (x - cx)) + cy;
return [nx, ny];
}
```

The first two parameters are the X and Y coordinates of the central point (the origin around which the second point will be rotated). The next two parameters are the coordinates of the point that we’ll be rotating. The last parameter is the angle, in degrees.

As an example, we’ll take the point (2, 1) and rotate it around the point (1, 1) by 90 degrees clockwise.

```rotate(1, 1, 2, 1, 90);
// > [1, 0]
```

1. For clockwise rotation, the last parameter `angle` should be positive. For counterclockwise rotation (like in the diagram you provided), it should be negative.
```rotate(0, 0, 5, 0, 90);
For this reason, both elements of the resulting array should be expected as a float. You can convert them to integers using `Math.round()`, `Math.ceil()`, or `Math.floor()` as needed.