Skip to content

Commit f674554

Browse files
Elliott Clarke38
authored andcommitted
Botched the math last time... Fix that and add tests to make sure it works.
1 parent d59b1b9 commit f674554

File tree

1 file changed

+22
-4
lines changed

1 file changed

+22
-4
lines changed

plotters-backend/src/text.rs

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,10 +151,12 @@ impl FontTransform {
151151
FontTransform::Rotate90 => (-y, x),
152152
FontTransform::Rotate180 => (-x, -y),
153153
FontTransform::Rotate270 => (y, -x),
154-
FontTransform::RotateAngle(angle) => (
155-
((x as f32) * angle.cos()).round() as i32,
156-
((y as f32) * angle.sin()).round() as i32,
157-
),
154+
FontTransform::RotateAngle(angle) => {
155+
let (x, y) = (x as f32, y as f32);
156+
let (sin, cos) = angle.to_radians().sin_cos();
157+
let (x, y) = (x * cos - y * sin, x * sin + y * cos);
158+
(x.round() as i32, y.round() as i32)
159+
}
158160
}
159161
}
160162
}
@@ -249,3 +251,19 @@ pub trait BackendTextStyle {
249251
draw: DrawFunc,
250252
) -> Result<Result<(), E>, Self::FontError>;
251253
}
254+
255+
#[cfg(test)]
256+
mod tests {
257+
use crate::FontTransform;
258+
259+
#[test]
260+
fn text_rotation_works() {
261+
assert_eq!(FontTransform::None.transform(1, 0), (1, 0));
262+
assert_eq!(FontTransform::Rotate90.transform(1, 0), (0, 1));
263+
assert_eq!(FontTransform::Rotate180.transform(1, 0), (-1, 0));
264+
assert_eq!(FontTransform::Rotate270.transform(1, 0), (0, -1));
265+
266+
assert_eq!(FontTransform::RotateAngle(45f32).transform(10, 0), (7, 7));
267+
assert_eq!(FontTransform::RotateAngle(45f32).transform(0, 10), (-7, 7));
268+
}
269+
}

0 commit comments

Comments
 (0)