summaryrefslogtreecommitdiffstats
path: root/graphics/src/point.rs
blob: 7793a779498dd89a2baa93e7ce8d3a6d2f6d6cf3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// -*- coding: utf-8 -*-
//
// Copyright 2021 Michael Büsch <m@bues.ch>
//
// Licensed under the Apache License version 2.0
// or the MIT license, at your option.
// SPDX-License-Identifier: Apache-2.0 OR MIT
//

use std::ops::Add;

#[derive(Clone, Copy, Debug)]
pub struct Point3D {
    x: f32,
    y: f32,
    z: f32,
}

impl Point3D {
    pub fn new_zero() -> Point3D {
        Self::new(0.0, 0.0, 0.0)
    }

    pub fn new(x: f32, y: f32, z: f32) -> Point3D {
        Point3D { x, y, z }
    }

    pub fn x(&self) -> f32 {
        self.x
    }

    pub fn set_x(&mut self, x: f32) {
        self.x = x;
    }

    pub fn y(&self) -> f32 {
        self.y
    }

    pub fn set_y(&mut self, y: f32) {
        self.y = y;
    }

    pub fn z(&self) -> f32 {
        self.z
    }

    pub fn set_z(&mut self, z: f32) {
        self.z = z;
    }
}

impl std::fmt::Display for Point3D {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "Point3D(x={:.2}, y={:.2}, z={:.2})",
            self.x(),
            self.y(),
            self.z()
        )
    }
}

impl Default for Point3D {
    fn default() -> Self {
        Self::new_zero()
    }
}

impl Add for Point3D {
    type Output = Point3D;

    fn add(self, other: Self) -> Self {
        Point3D {
            x: self.x + other.x,
            y: self.y + other.y,
            z: self.z + other.z,
        }
    }
}

// vim: ts=4 sw=4 expandtab
bues.ch cgit interface