rust_3d/
line_segment_2d.rs1use std::fmt;
26
27use crate::*;
28
29#[derive(Debug, PartialEq, PartialOrd, Eq, Clone, Hash)]
32pub struct LineSegment2D {
34 pub start: Point2D,
35 pub end: Point2D,
36}
37
38impl LineSegment2D {
39 pub fn new(start: Point2D, end: Point2D) -> Self {
41 LineSegment2D { start, end }
42 }
43}
44
45impl IsMovable2D for LineSegment2D {
46 fn move_by(&mut self, x: f64, y: f64) {
47 self.start.move_by(x, y);
48 self.end.move_by(x, y);
49 }
50}
51
52impl HasLength for LineSegment2D {
53 fn length(&self) -> f64 {
54 dist_2d(&self.start, &self.end)
55 }
56}
57
58impl HasBoundingBox2DMaybe for LineSegment2D {
59 fn bounding_box_maybe(&self) -> Result<BoundingBox2D> {
60 BoundingBox2D::from_iterator([&self.start, &self.end].iter().map(|x| *x))
61 }
62}
63
64impl HasCenterOfGravity2D for LineSegment2D {
65 fn center_of_gravity(&self) -> Result<Point2D> {
66 Ok(center_2d(&self.start, &self.end))
67 }
68}
69
70impl IsScalable for LineSegment2D {
71 fn scale(&mut self, factor: Positive) {
72 if let Ok(c) = self.bounding_box_maybe().map(|x| x.center_bb()) {
73 self.start.increase_distance_to_by(&c, factor);
74 self.end.increase_distance_to_by(&c, factor);
75 }
76 }
77}
78
79impl IsMatrix3Transformable for LineSegment2D {
80 fn transformed(&self, m: &Matrix3) -> Self {
81 let mut new = self.clone();
82 new.transform(m);
83 new
84 }
85
86 fn transform(&mut self, m: &Matrix3) {
87 self.start.transform(m);
88 self.end.transform(m);
89 }
90}
91
92impl fmt::Display for LineSegment2D {
93 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
94 write!(
95 f,
96 "({}, {} -> {}, {})",
97 self.start.x(),
98 self.start.y(),
99 self.end.x(),
100 self.end.y()
101 )
102 }
103}