Rotation:
https://en.wikipedia.org/wiki/Rotation_matrix#In_two_dimensions
Rotating 90 degrees counter-clockwise:
x' = 0 * x - 1 * y = -y
y' = 1 * x + 0 * y = x |
That rotation is around (0,0).
That code first calculates vector d = a - p,
then rotates d and finally adds p back.
1 2 3 4 5 6 7 8 9 10 11
|
// d = a - p
int dx = ax - px;
int dy = ay - py;
// r = rotate(d)
int rx = -dy;
int ry = dx;
// a = r + p
ax = rx + px;
ay = ry + py;
|
Note that a[1] is the center of rotation. a[1] - a[1] == (0,0)
Therefore, r == d == (0,0) and the new coordinates are:
1 2
|
a[1].x = a[1].x - 0;
a[1].y = a[1].y + 0;
|
a[1] does not move. Center of rotation does not move.
The .x coordinate is k%2, so its value is either 0 or 1, depending on the k being even or odd.
The .y coordinate is half of k.