1use std::ops::{Add, AddAssign, Sub, SubAssign};
2
3#[derive(Debug, Clone, Copy, PartialEq, Default)]
4pub struct Point {
5 pub x: f32,
6 pub y: f32,
7}
8
9impl Point {
10 pub const ZERO: Self = Self::new(0.0, 0.0);
11
12 pub const fn new(x: f32, y: f32) -> Self {
13 Self { x, y }
14 }
15
16 pub const fn to_vector(self) -> Vector {
17 Vector::new(self.x, self.y)
18 }
19}
20
21#[derive(Debug, Clone, Copy, PartialEq, Default)]
22pub struct Vector {
23 pub x: f32,
24 pub y: f32,
25}
26
27impl Vector {
28 pub const ZERO: Self = Self::new(0.0, 0.0);
29
30 pub const fn new(x: f32, y: f32) -> Self {
31 Self { x, y }
32 }
33}
34
35impl Add<Vector> for Point {
36 type Output = Point;
37
38 fn add(self, rhs: Vector) -> Self::Output {
39 Point::new(self.x + rhs.x, self.y + rhs.y)
40 }
41}
42
43impl AddAssign<Vector> for Point {
44 fn add_assign(&mut self, rhs: Vector) {
45 self.x += rhs.x;
46 self.y += rhs.y;
47 }
48}
49
50impl Sub<Point> for Point {
51 type Output = Vector;
52
53 fn sub(self, rhs: Point) -> Self::Output {
54 Vector::new(self.x - rhs.x, self.y - rhs.y)
55 }
56}
57
58impl Add for Vector {
59 type Output = Vector;
60
61 fn add(self, rhs: Vector) -> Self::Output {
62 Vector::new(self.x + rhs.x, self.y + rhs.y)
63 }
64}
65
66impl AddAssign for Vector {
67 fn add_assign(&mut self, rhs: Vector) {
68 self.x += rhs.x;
69 self.y += rhs.y;
70 }
71}
72
73impl Sub for Vector {
74 type Output = Vector;
75
76 fn sub(self, rhs: Vector) -> Self::Output {
77 Vector::new(self.x - rhs.x, self.y - rhs.y)
78 }
79}
80
81impl SubAssign for Vector {
82 fn sub_assign(&mut self, rhs: Vector) {
83 self.x -= rhs.x;
84 self.y -= rhs.y;
85 }
86}
87
88#[derive(Debug, Clone, Copy, PartialEq, Default)]
89pub struct Size {
90 pub width: f32,
91 pub height: f32,
92}
93
94impl Size {
95 pub const ZERO: Self = Self::new(0.0, 0.0);
96
97 pub const fn new(width: f32, height: f32) -> Self {
98 Self { width, height }
99 }
100
101 pub fn is_empty(self) -> bool {
102 self.width <= 0.0 || self.height <= 0.0
103 }
104
105 pub fn clamp(self, min: Size, max: Size) -> Self {
110 Self::new(
111 clamp_length(self.width, min.width, max.width),
112 clamp_length(self.height, min.height, max.height),
113 )
114 }
115}
116
117fn clamp_length(value: f32, min: f32, max: f32) -> f32 {
119 let value = if value.is_nan() { min } else { value };
120 let clamped = value.min(max).max(min);
121 if clamped.is_nan() { 0.0 } else { clamped }
122}
123
124#[derive(Debug, Clone, Copy, PartialEq, Default)]
125pub struct Rect {
126 pub origin: Point,
127 pub size: Size,
128}
129
130impl Rect {
131 pub const ZERO: Self = Self::new(0.0, 0.0, 0.0, 0.0);
132
133 pub const fn new(x: f32, y: f32, width: f32, height: f32) -> Self {
134 Self {
135 origin: Point::new(x, y),
136 size: Size::new(width, height),
137 }
138 }
139
140 pub const fn from_origin_size(origin: Point, size: Size) -> Self {
141 Self { origin, size }
142 }
143
144 pub fn from_points(min: Point, max: Point) -> Self {
145 Self::new(
146 min.x,
147 min.y,
148 (max.x - min.x).max(0.0),
149 (max.y - min.y).max(0.0),
150 )
151 }
152
153 pub const fn x(self) -> f32 {
154 self.origin.x
155 }
156
157 pub const fn y(self) -> f32 {
158 self.origin.y
159 }
160
161 pub const fn width(self) -> f32 {
162 self.size.width
163 }
164
165 pub const fn height(self) -> f32 {
166 self.size.height
167 }
168
169 pub fn max_x(self) -> f32 {
170 self.origin.x + self.size.width
171 }
172
173 pub fn max_y(self) -> f32 {
174 self.origin.y + self.size.height
175 }
176
177 pub fn is_empty(self) -> bool {
178 self.size.is_empty()
179 }
180
181 pub fn contains(self, point: Point) -> bool {
182 point.x >= self.x()
183 && point.x <= self.max_x()
184 && point.y >= self.y()
185 && point.y <= self.max_y()
186 }
187
188 pub fn translate(self, delta: Vector) -> Self {
189 Self::from_origin_size(self.origin + delta, self.size)
190 }
191
192 pub fn inflate(self, x: f32, y: f32) -> Self {
193 Self::new(
194 self.origin.x - x,
195 self.origin.y - y,
196 self.size.width + (x * 2.0),
197 self.size.height + (y * 2.0),
198 )
199 }
200
201 pub fn intersection(self, other: Rect) -> Option<Self> {
202 let min_x = self.x().max(other.x());
203 let min_y = self.y().max(other.y());
204 let max_x = self.max_x().min(other.max_x());
205 let max_y = self.max_y().min(other.max_y());
206
207 if min_x >= max_x || min_y >= max_y {
208 return None;
209 }
210
211 Some(Self::from_points(
212 Point::new(min_x, min_y),
213 Point::new(max_x, max_y),
214 ))
215 }
216
217 pub fn union(self, other: Rect) -> Self {
218 if self.is_empty() {
219 return other;
220 }
221
222 if other.is_empty() {
223 return self;
224 }
225
226 Self::from_points(
227 Point::new(self.x().min(other.x()), self.y().min(other.y())),
228 Point::new(
229 self.max_x().max(other.max_x()),
230 self.max_y().max(other.max_y()),
231 ),
232 )
233 }
234}
235
236#[derive(Debug, Clone, PartialEq)]
237pub enum PathElement {
238 MoveTo(Point),
239 LineTo(Point),
240 QuadTo {
241 ctrl: Point,
242 to: Point,
243 },
244 CubicTo {
245 ctrl1: Point,
246 ctrl2: Point,
247 to: Point,
248 },
249 Close,
250}
251
252#[derive(Debug, Clone, PartialEq, Default)]
253pub struct Path {
254 elements: Vec<PathElement>,
255 bounds: Rect,
256}
257
258impl Path {
259 pub fn new() -> Self {
260 Self::default()
261 }
262
263 pub fn builder() -> PathBuilder {
264 PathBuilder::new()
265 }
266
267 pub fn rect(rect: Rect) -> Self {
268 let mut builder = PathBuilder::with_capacity(RECT_ELEMENTS);
269 builder.push_rect(rect);
270 builder.build()
271 }
272
273 pub fn circle(center: Point, radius: f32) -> Self {
274 let mut builder = PathBuilder::with_capacity(CIRCLE_ELEMENTS);
275 builder.push_circle(center, radius);
276 builder.build()
277 }
278
279 pub fn rounded_rect(rect: Rect, radius: f32) -> Self {
280 let mut builder = PathBuilder::with_capacity(ROUNDED_RECT_ELEMENTS);
281 builder.push_rounded_rect(rect, radius);
282 builder.build()
283 }
284
285 pub fn arc(center: Point, radius: f32, start_angle: f32, sweep_angle: f32) -> Self {
286 let mut builder = Self::builder();
287 builder.push_arc(center, radius, start_angle, sweep_angle);
288 builder.build()
289 }
290
291 pub fn elements(&self) -> &[PathElement] {
292 &self.elements
293 }
294
295 pub const fn bounds(&self) -> Rect {
296 self.bounds
297 }
298
299 pub fn is_empty(&self) -> bool {
300 self.elements.is_empty()
301 }
302}
303
304impl From<Rect> for Path {
305 fn from(value: Rect) -> Self {
306 Self::rect(value)
307 }
308}
309
310const RECT_ELEMENTS: usize = 5;
312const CIRCLE_ELEMENTS: usize = 6;
313const ROUNDED_RECT_ELEMENTS: usize = 10;
314
315#[derive(Debug, Clone, Default)]
316pub struct PathBuilder {
317 elements: Vec<PathElement>,
318 min: Option<Point>,
319 max: Option<Point>,
320}
321
322impl PathBuilder {
323 pub fn new() -> Self {
324 Self::default()
325 }
326
327 pub fn with_capacity(elements: usize) -> Self {
330 Self {
331 elements: Vec::with_capacity(elements),
332 ..Self::default()
333 }
334 }
335
336 pub fn move_to(&mut self, to: Point) -> &mut Self {
337 self.include_point(to);
338 self.elements.push(PathElement::MoveTo(to));
339 self
340 }
341
342 pub fn line_to(&mut self, to: Point) -> &mut Self {
343 self.include_point(to);
344 self.elements.push(PathElement::LineTo(to));
345 self
346 }
347
348 pub fn quad_to(&mut self, ctrl: Point, to: Point) -> &mut Self {
349 self.include_point(ctrl);
350 self.include_point(to);
351 self.elements.push(PathElement::QuadTo { ctrl, to });
352 self
353 }
354
355 pub fn cubic_to(&mut self, ctrl1: Point, ctrl2: Point, to: Point) -> &mut Self {
356 self.include_point(ctrl1);
357 self.include_point(ctrl2);
358 self.include_point(to);
359 self.elements
360 .push(PathElement::CubicTo { ctrl1, ctrl2, to });
361 self
362 }
363
364 pub fn close(&mut self) -> &mut Self {
365 self.elements.push(PathElement::Close);
366 self
367 }
368
369 pub fn push_rect(&mut self, rect: Rect) -> &mut Self {
370 if rect.is_empty() {
371 return self;
372 }
373
374 self.elements.reserve(RECT_ELEMENTS);
375 self.move_to(rect.origin)
376 .line_to(Point::new(rect.max_x(), rect.y()))
377 .line_to(Point::new(rect.max_x(), rect.max_y()))
378 .line_to(Point::new(rect.x(), rect.max_y()))
379 .close();
380 self
381 }
382
383 pub fn push_circle(&mut self, center: Point, radius: f32) -> &mut Self {
384 if radius <= 0.0 {
385 return self;
386 }
387
388 self.elements.reserve(CIRCLE_ELEMENTS);
389 self.append_arc_segments(center, radius, 0.0, std::f32::consts::TAU, true);
390 self.close()
391 }
392
393 pub fn push_rounded_rect(&mut self, rect: Rect, radius: f32) -> &mut Self {
394 if rect.is_empty() {
395 return self;
396 }
397
398 let radius = radius
399 .max(0.0)
400 .min(rect.width() * 0.5)
401 .min(rect.height() * 0.5);
402 if radius <= 0.0 {
403 return self.push_rect(rect);
404 }
405
406 let min_x = rect.x();
407 let min_y = rect.y();
408 let max_x = rect.max_x();
409 let max_y = rect.max_y();
410
411 self.elements.reserve(ROUNDED_RECT_ELEMENTS);
412 self.move_to(Point::new(min_x + radius, min_y))
413 .line_to(Point::new(max_x - radius, min_y));
414 self.append_arc_segments(
415 Point::new(max_x - radius, min_y + radius),
416 radius,
417 -std::f32::consts::FRAC_PI_2,
418 std::f32::consts::FRAC_PI_2,
419 false,
420 );
421 self.line_to(Point::new(max_x, max_y - radius));
422 self.append_arc_segments(
423 Point::new(max_x - radius, max_y - radius),
424 radius,
425 0.0,
426 std::f32::consts::FRAC_PI_2,
427 false,
428 );
429 self.line_to(Point::new(min_x + radius, max_y));
430 self.append_arc_segments(
431 Point::new(min_x + radius, max_y - radius),
432 radius,
433 std::f32::consts::FRAC_PI_2,
434 std::f32::consts::FRAC_PI_2,
435 false,
436 );
437 self.line_to(Point::new(min_x, min_y + radius));
438 self.append_arc_segments(
439 Point::new(min_x + radius, min_y + radius),
440 radius,
441 std::f32::consts::PI,
442 std::f32::consts::FRAC_PI_2,
443 false,
444 );
445 self.close()
446 }
447
448 pub fn push_arc(
449 &mut self,
450 center: Point,
451 radius: f32,
452 start_angle: f32,
453 sweep_angle: f32,
454 ) -> &mut Self {
455 if radius <= 0.0 || sweep_angle == 0.0 {
456 return self;
457 }
458
459 self.append_arc_segments(center, radius, start_angle, sweep_angle, true);
460 self
461 }
462
463 pub fn build(self) -> Path {
464 let bounds = match (self.min, self.max) {
465 (Some(min), Some(max)) => Rect::from_points(min, max),
466 _ => Rect::ZERO,
467 };
468
469 Path {
470 elements: self.elements,
471 bounds,
472 }
473 }
474
475 fn include_point(&mut self, point: Point) {
476 match (self.min.as_mut(), self.max.as_mut()) {
477 (Some(min), Some(max)) => {
478 min.x = min.x.min(point.x);
479 min.y = min.y.min(point.y);
480 max.x = max.x.max(point.x);
481 max.y = max.y.max(point.y);
482 }
483 _ => {
484 self.min = Some(point);
485 self.max = Some(point);
486 }
487 }
488 }
489
490 fn append_arc_segments(
491 &mut self,
492 center: Point,
493 radius: f32,
494 start_angle: f32,
495 sweep_angle: f32,
496 move_to_start: bool,
497 ) {
498 if radius <= 0.0 || sweep_angle == 0.0 {
499 return;
500 }
501
502 let segment_count = (sweep_angle.abs() / std::f32::consts::FRAC_PI_2).ceil() as usize;
503 let segment_count = segment_count.max(1);
504 let delta = sweep_angle / segment_count as f32;
505
506 for index in 0..segment_count {
507 let start = start_angle + (delta * index as f32);
508 let end = start + delta;
509 let start_point = arc_point(center, radius, start);
510 let end_point = arc_point(center, radius, end);
511 let tangent_scale = (4.0 / 3.0) * ((end - start) * 0.25).tan();
512 let ctrl1 = Point::new(
513 start_point.x - (start.sin() * radius * tangent_scale),
514 start_point.y + (start.cos() * radius * tangent_scale),
515 );
516 let ctrl2 = Point::new(
517 end_point.x + (end.sin() * radius * tangent_scale),
518 end_point.y - (end.cos() * radius * tangent_scale),
519 );
520
521 if index == 0 && move_to_start {
522 self.move_to(start_point);
523 }
524 self.cubic_to(ctrl1, ctrl2, end_point);
525 }
526 }
527}
528
529fn arc_point(center: Point, radius: f32, angle: f32) -> Point {
530 Point::new(
531 center.x + (radius * angle.cos()),
532 center.y + (radius * angle.sin()),
533 )
534}
535
536#[derive(Debug, Clone, Copy, PartialEq)]
537pub struct Transform {
538 pub xx: f32,
539 pub yx: f32,
540 pub xy: f32,
541 pub yy: f32,
542 pub dx: f32,
543 pub dy: f32,
544}
545
546impl Transform {
547 pub const IDENTITY: Self = Self::new(1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
548
549 pub const fn new(xx: f32, yx: f32, xy: f32, yy: f32, dx: f32, dy: f32) -> Self {
550 Self {
551 xx,
552 yx,
553 xy,
554 yy,
555 dx,
556 dy,
557 }
558 }
559
560 pub const fn translation(x: f32, y: f32) -> Self {
561 Self::new(1.0, 0.0, 0.0, 1.0, x, y)
562 }
563
564 pub const fn translation_vector(delta: Vector) -> Self {
565 Self::translation(delta.x, delta.y)
566 }
567
568 pub const fn scale(x: f32, y: f32) -> Self {
569 Self::new(x, 0.0, 0.0, y, 0.0, 0.0)
570 }
571
572 pub fn rotation(radians: f32) -> Self {
573 let (sin, cos) = radians.sin_cos();
574 Self::new(cos, sin, -sin, cos, 0.0, 0.0)
575 }
576
577 pub const fn is_identity(self) -> bool {
578 self.xx == 1.0
579 && self.yx == 0.0
580 && self.xy == 0.0
581 && self.yy == 1.0
582 && self.dx == 0.0
583 && self.dy == 0.0
584 }
585
586 pub fn then(self, next: Self) -> Self {
587 Self::new(
588 (next.xx * self.xx) + (next.xy * self.yx),
589 (next.yx * self.xx) + (next.yy * self.yx),
590 (next.xx * self.xy) + (next.xy * self.yy),
591 (next.yx * self.xy) + (next.yy * self.yy),
592 (next.xx * self.dx) + (next.xy * self.dy) + next.dx,
593 (next.yx * self.dx) + (next.yy * self.dy) + next.dy,
594 )
595 }
596
597 pub fn transform_point(self, point: Point) -> Point {
598 Point::new(
599 (self.xx * point.x) + (self.xy * point.y) + self.dx,
600 (self.yx * point.x) + (self.yy * point.y) + self.dy,
601 )
602 }
603
604 pub fn transform_vector(self, vector: Vector) -> Vector {
605 Vector::new(
606 (self.xx * vector.x) + (self.xy * vector.y),
607 (self.yx * vector.x) + (self.yy * vector.y),
608 )
609 }
610
611 pub fn inverse(self) -> Option<Self> {
612 let determinant = (self.xx * self.yy) - (self.xy * self.yx);
613 if !determinant.is_finite() || determinant.abs() <= f32::EPSILON {
614 return None;
615 }
616 let inverse = 1.0 / determinant;
617 let xx = self.yy * inverse;
618 let yx = -self.yx * inverse;
619 let xy = -self.xy * inverse;
620 let yy = self.xx * inverse;
621 let dx = -((xx * self.dx) + (xy * self.dy));
622 let dy = -((yx * self.dx) + (yy * self.dy));
623 let transform = Self::new(xx, yx, xy, yy, dx, dy);
624 [xx, yx, xy, yy, dx, dy]
625 .iter()
626 .all(|value| value.is_finite())
627 .then_some(transform)
628 }
629
630 pub fn transform_rect_bbox(self, rect: Rect) -> Rect {
631 if rect.is_empty() {
632 return rect;
633 }
634 if self.xx == 1.0 && self.yx == 0.0 && self.xy == 0.0 && self.yy == 1.0 {
637 return rect.translate(Vector::new(self.dx, self.dy));
638 }
639
640 let top_left = self.transform_point(rect.origin);
641 let top_right = self.transform_point(Point::new(rect.max_x(), rect.y()));
642 let bottom_left = self.transform_point(Point::new(rect.x(), rect.max_y()));
643 let bottom_right = self.transform_point(Point::new(rect.max_x(), rect.max_y()));
644
645 let min_x = top_left
646 .x
647 .min(top_right.x)
648 .min(bottom_left.x)
649 .min(bottom_right.x);
650 let min_y = top_left
651 .y
652 .min(top_right.y)
653 .min(bottom_left.y)
654 .min(bottom_right.y);
655 let max_x = top_left
656 .x
657 .max(top_right.x)
658 .max(bottom_left.x)
659 .max(bottom_right.x);
660 let max_y = top_left
661 .y
662 .max(top_right.y)
663 .max(bottom_left.y)
664 .max(bottom_right.y);
665
666 Rect::from_points(Point::new(min_x, min_y), Point::new(max_x, max_y))
667 }
668}
669
670#[cfg(test)]
671mod path_tests {
672 use super::{Path, PathElement, Point, Rect};
673
674 fn assert_rect_approx_eq(actual: Rect, expected: Rect) {
675 assert!((actual.x() - expected.x()).abs() < 0.001);
676 assert!((actual.y() - expected.y()).abs() < 0.001);
677 assert!((actual.width() - expected.width()).abs() < 0.001);
678 assert!((actual.height() - expected.height()).abs() < 0.001);
679 }
680
681 #[test]
682 fn path_builder_tracks_elements_and_bounds() {
683 let mut builder = Path::builder();
684 builder
685 .move_to(Point::new(2.0, 3.0))
686 .quad_to(Point::new(12.0, 1.0), Point::new(18.0, 9.0))
687 .line_to(Point::new(6.0, 15.0))
688 .close();
689
690 let path = builder.build();
691
692 assert_eq!(path.bounds(), Rect::new(2.0, 1.0, 16.0, 14.0));
693 assert_eq!(path.elements().len(), 4);
694 assert!(matches!(path.elements()[0], PathElement::MoveTo(_)));
695 assert!(matches!(path.elements()[1], PathElement::QuadTo { .. }));
696 assert!(matches!(path.elements()[3], PathElement::Close));
697 }
698
699 #[test]
700 fn rect_conversion_builds_closed_path() {
701 let path = Path::from(Rect::new(4.0, 5.0, 12.0, 8.0));
702
703 assert_eq!(path.bounds(), Rect::new(4.0, 5.0, 12.0, 8.0));
704 assert_eq!(path.elements().len(), 5);
705 assert!(matches!(path.elements()[4], PathElement::Close));
706 }
707
708 #[test]
709 fn circle_builder_emits_closed_cubic_path() {
710 let path = Path::circle(Point::new(10.0, 20.0), 8.0);
711
712 assert_rect_approx_eq(path.bounds(), Rect::new(2.0, 12.0, 16.0, 16.0));
713 assert_eq!(path.elements().len(), 6);
714 assert!(matches!(path.elements()[0], PathElement::MoveTo(_)));
715 assert!(matches!(path.elements()[1], PathElement::CubicTo { .. }));
716 assert!(matches!(path.elements()[5], PathElement::Close));
717 }
718
719 #[test]
720 fn rounded_rect_builder_preserves_bounds() {
721 let path = Path::rounded_rect(Rect::new(4.0, 6.0, 24.0, 12.0), 3.0);
722
723 assert_rect_approx_eq(path.bounds(), Rect::new(4.0, 6.0, 24.0, 12.0));
724 assert_eq!(path.elements().len(), 10);
725 assert!(matches!(path.elements()[2], PathElement::CubicTo { .. }));
726 assert!(matches!(path.elements()[9], PathElement::Close));
727 }
728
729 #[test]
730 fn arc_builder_emits_open_curved_path() {
731 let path = Path::arc(
732 Point::new(20.0, 20.0),
733 10.0,
734 0.0,
735 std::f32::consts::FRAC_PI_2,
736 );
737
738 assert_eq!(path.bounds(), Rect::new(20.0, 20.0, 10.0, 10.0));
739 assert_eq!(path.elements().len(), 2);
740 assert!(matches!(path.elements()[0], PathElement::MoveTo(_)));
741 assert!(matches!(path.elements()[1], PathElement::CubicTo { .. }));
742 }
743}
744
745impl Default for Transform {
746 fn default() -> Self {
747 Self::IDENTITY
748 }
749}
750
751#[cfg(test)]
752mod tests {
753 use super::{Point, Rect, Size, Transform, Vector};
754
755 #[test]
756 fn size_clamp_keeps_going_with_bounds_that_cannot_hold_a_size() {
757 let size = Size::new(50.0, 50.0);
758 assert_eq!(
760 size.clamp(Size::new(10.0, 60.0), Size::new(40.0, 80.0)),
761 Size::new(40.0, 60.0)
762 );
763 assert_eq!(
765 size.clamp(Size::new(30.0, 30.0), Size::new(20.0, 20.0)),
766 Size::new(30.0, 30.0)
767 );
768 assert_eq!(
770 size.clamp(Size::new(f32::NAN, 10.0), Size::new(40.0, f32::NAN)),
771 Size::new(40.0, 50.0)
772 );
773 assert_eq!(
774 Size::new(f32::NAN, f32::NAN).clamp(Size::new(5.0, 0.0), Size::new(9.0, 9.0)),
775 Size::new(5.0, 0.0)
776 );
777 }
778
779 #[test]
780 fn point_and_vector_math_is_stable() {
781 let origin = Point::new(10.0, 20.0);
782 let delta = Vector::new(5.0, -2.0);
783
784 assert_eq!(origin + delta, Point::new(15.0, 18.0));
785 assert_eq!((origin + delta) - origin, delta);
786 }
787
788 #[test]
789 fn rect_intersection_returns_overlap() {
790 let left = Rect::new(0.0, 0.0, 10.0, 10.0);
791 let right = Rect::new(8.0, 2.0, 10.0, 10.0);
792
793 assert_eq!(
794 left.intersection(right),
795 Some(Rect::new(8.0, 2.0, 2.0, 8.0))
796 );
797 }
798
799 #[test]
800 fn transform_composition_applies_in_order() {
801 let transform = Transform::scale(2.0, 3.0).then(Transform::translation(5.0, 7.0));
802
803 assert_eq!(
804 transform.transform_point(Point::new(4.0, 2.0)),
805 Point::new(13.0, 13.0)
806 );
807 }
808
809 #[test]
810 fn transform_rect_bbox_covers_rotated_rect() {
811 let rect = Rect::new(0.0, 0.0, 10.0, 4.0);
812 let bbox = Transform::rotation(std::f32::consts::FRAC_PI_2).transform_rect_bbox(rect);
813
814 assert!((bbox.x() + 4.0).abs() < 0.001);
815 assert!(bbox.y().abs() < 0.001);
816 assert!((bbox.width() - 4.0).abs() < 0.001);
817 assert!((bbox.height() - 10.0).abs() < 0.001);
818 }
819
820 #[test]
821 fn affine_transform_inverse_round_trips_points_and_vectors() {
822 let transform = Transform::translation(42.0, -18.0)
823 .then(Transform::rotation(0.37))
824 .then(Transform::scale(1.75, 0.8));
825 let inverse = transform.inverse().expect("invertible transform");
826 let point = Point::new(19.0, -7.0);
827 let vector = Vector::new(4.0, 11.0);
828 let point_round_trip = inverse.transform_point(transform.transform_point(point));
829 let vector_round_trip = inverse.transform_vector(transform.transform_vector(vector));
830
831 assert!((point_round_trip.x - point.x).abs() < 0.001);
832 assert!((point_round_trip.y - point.y).abs() < 0.001);
833 assert!((vector_round_trip.x - vector.x).abs() < 0.001);
834 assert!((vector_round_trip.y - vector.y).abs() < 0.001);
835 assert!(Transform::scale(0.0, 1.0).inverse().is_none());
836 }
837}