// -*- coding: utf-8 -*- // // Copyright 2021 Michael Büsch // // 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