rust_3d/
line_segment_3d.rs1use std::fmt;
26
27use crate::*;
28
29#[derive(Debug, PartialEq, PartialOrd, Eq, Clone, Hash)]
32pub struct LineSegment3D {
34 pub start: Point3D,
35 pub end: Point3D,
36}
37
38impl LineSegment3D {
39 pub fn new(start: Point3D, end: Point3D) -> Self {
41 LineSegment3D { start, end }
42 }
43}
44
45impl IsMovable3D for LineSegment3D {
46 fn move_by(&mut self, x: f64, y: f64, z: f64) {
47 self.start.move_by(x, y, z);
48 self.end.move_by(x, y, z);
49 }
50}
51
52impl HasLength for LineSegment3D {
53 fn length(&self) -> f64 {
54 dist_3d(&self.start, &self.end)
55 }
56}
57
58impl HasBoundingBox3DMaybe for LineSegment3D {
59 fn bounding_box_maybe(&self) -> Result<BoundingBox3D> {
60 BoundingBox3D::from_iterator([&self.start, &self.end].iter().map(|x| *x))
61 }
62}
63
64impl HasCenterOfGravity3D for LineSegment3D {
65 fn center_of_gravity(&self) -> Result<Point3D> {
66 Ok(center_3d(&self.start, &self.end))
67 }
68}
69
70impl IsScalable for LineSegment3D {
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 IsMatrix4Transformable for LineSegment3D {
80 fn transformed(&self, m: &Matrix4) -> Self {
81 let mut new = self.clone();
82 new.transform(m);
83 new
84 }
85
86 fn transform(&mut self, m: &Matrix4) {
87 self.start.transform(m);
88 self.end.transform(m);
89 }
90}
91
92impl fmt::Display for LineSegment3D {
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.start.z(),
100 self.end.x(),
101 self.end.y(),
102 self.end.z()
103 )
104 }
105}