rotate method
Rotates the Vector2 with angle
in radians
rotates around center
if it is defined
In a screen coordinate system (where the y-axis is flipped) it rotates in
a clockwise fashion
In a normal coordinate system it rotates in a counter-clockwise fashion
Implementation
void rotate(double angle, {Vector2? center}) {
if (isZero() || angle == 0) {
// No point in rotating the zero vector or to rotate with 0 as angle
return;
}
if (center == null) {
setValues(
x * cos(angle) - y * sin(angle),
x * sin(angle) + y * cos(angle),
);
} else {
setValues(
cos(angle) * (x - center.x) - sin(angle) * (y - center.y) + center.x,
sin(angle) * (x - center.x) + cos(angle) * (y - center.y) + center.y,
);
}
}