How to rotate a array with x, y values?

41 Views Asked by At

So i have array that can look like this (javascript for the matter):

array = [{x: 0, y: 0}, {x: 1, y: 1}, {x: -4, y: 3}]

Each of the entrys has a x and y position. What i want to do is essentialy rotate those entrys 90 degrees in one direction.

Whats the simplest way to do this?

2

There are 2 best solutions below

2
On BEST ANSWER

Basically you could something like this to perform a counter clockwise $90$ degrees rotation of center $(0,0)$. The point $(x,y)$ is transformed in $(-y,x)$.

c=x
x=-y
y=c

One liner javascript code:

[x, y] = [-y, x];

Drawing it should convince you. You could look at rotation matrices for solutions of broader problems with a different angle for example.

0
On

What about iterating over the array, converting each pair $(x, y)$ to a complex number, using for instance Complex.js, and multiplying by the imaginary unit $i$, which corresponds to a rotation of 90 degrees counterclockwise.