j_webgl/algebra/
point3.rs

1use std::ops::{Add, Sub};
2use super::vector3::Vector3;
3
4/// A point in 3D space with x, y, z coordinates
5#[derive(Debug, Clone, Copy, PartialEq)]
6pub struct Point3 {
7    x: f32,
8    y: f32,
9    z: f32,
10}
11
12impl Point3 {
13    /// Create a new Point3 representing the origin
14    pub fn origin() -> Self {
15        Point3::new(0.0, 0.0, 0.0)
16    }
17
18    /// Creates a new Point3 with the given x, y, z coordinates
19    pub fn new(x: f32, y: f32, z: f32) -> Self {
20        Point3 { x, y, z }
21    }
22
23    /// Returns the x coordinate
24    pub fn x(&self) -> f32 {
25        self.x
26    }
27
28    /// Returns the y coordinate
29    pub fn y(&self) -> f32 {
30        self.y
31    }
32
33    /// Returns the z coordinate
34    pub fn z(&self) -> f32 {
35        self.z
36    }
37}
38
39// Implementation for v1 = p2 - p1 (vector from p1 to p2)
40impl Sub<&Point3> for &Point3 {
41    type Output = Vector3;
42
43    fn sub(self, other: &Point3) -> Vector3 {
44        Vector3::new(
45            self.x - other.x,
46            self.y - other.y,
47            self.z - other.z,
48        )
49    }
50}
51
52// Implementation for p1 = p2 + v1 (point plus vector)
53impl Add<&Vector3> for &Point3 {
54    type Output = Point3;
55
56    fn add(self, vector: &Vector3) -> Point3 {
57        Point3 {
58            x: self.x + vector.dx(),
59            y: self.y + vector.dy(),
60            z: self.z + vector.dz(),
61        }
62    }
63}