rotatePosition2D function
Returns a function to rotate positions by the radians
around the origin.
If both cx
and cy
are given then rotate points around this pivot point.
Implementation
TransformPosition rotatePosition2D(num radians, {num? cx, num? cy}) =>
<T extends Position>(T source) {
final s = math.sin(radians);
final c = math.cos(radians);
var x = source.x;
var y = source.y;
// if has pivot point, then move origin
if (cx != null && cy != null) {
x -= cx;
y -= cy;
}
// rotate point
final xnew = x * c - y * s;
final ynew = x * s + y * c;
// return rotated point
if (cx != null && cy != null) {
return source.copyWith(
x: xnew + cx,
y: ynew + cy,
) as T;
} else {
return source.copyWith(x: xnew, y: ynew) as T;
}
};