rotate90 method
Returns this position rotated by 90 degrees steps
times clockwise.
The default is to rotate 90 degrees once, but you can rotate multiple
times by providing a different number of steps
, or counter-clockwise by
providing a negative number of steps.
A 90° rotation is a fixed-point rotation, i.e. it does not use floating
point numbers or π
, and is derived entirely from integer offsets.
See rotate45 for 45° rotations.
Visualizing the rotations
Imagine the following original position, perhaps at (3, 2)
:
y
^
|
| *
|
O-----> x
A 90° Clockwise rotate90()
would move the position to (2, -3)
:
O-----> x
|
|
| *
v
y
A 180° Clockwise rotate90(2)
would move the position to (-3, -2)
:
x <-----O
|
* |
|
v
y
And finally, a 270° Clockwise rotate90(-1)
is (-2, 3)
:
y
^
* |
|
|
x <-----O
Example
final a = Pos(10, 20);
print(a.rotate90()); // => Pos(20, -10)
print(a.rotate90(2)); // => Pos(-10, -20)
print(a.rotate90(-1)); // => Pos(-20, 10)
Implementation
Pos rotate90([int steps = 1]) {
return switch (steps % 4) {
0 => this,
1 => Pos(-y, x),
2 => Pos(-x, -y),
_ => Pos(y, -x),
};
}