j_webgl/algebra/
point3.rs1use std::ops::{Add, Sub};
2use super::vector3::Vector3;
3
4#[derive(Debug, Clone, Copy, PartialEq)]
6pub struct Point3 {
7 x: f32,
8 y: f32,
9 z: f32,
10}
11
12impl Point3 {
13 pub fn origin() -> Self {
15 Point3::new(0.0, 0.0, 0.0)
16 }
17
18 pub fn new(x: f32, y: f32, z: f32) -> Self {
20 Point3 { x, y, z }
21 }
22
23 pub fn x(&self) -> f32 {
25 self.x
26 }
27
28 pub fn y(&self) -> f32 {
30 self.y
31 }
32
33 pub fn z(&self) -> f32 {
35 self.z
36 }
37}
38
39impl 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
52impl 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}