Compare commits
No commits in common. "c4a794c4f25ada8c51db41aae1d7999525d3b637" and "0a692c01de89877b97e68b7eb02587b31dc9f71a" have entirely different histories.
c4a794c4f2
...
0a692c01de
@ -1,56 +0,0 @@
|
|||||||
use crate::math::{Point3, Vector3};
|
|
||||||
|
|
||||||
pub struct Plane {
|
|
||||||
point: Point3,
|
|
||||||
normal: Vector3,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Plane {
|
|
||||||
pub fn new(point: Point3, normal: Vector3) -> Plane {
|
|
||||||
Plane { point, normal }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct Line {
|
|
||||||
point: Point3,
|
|
||||||
direction: Vector3,
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct Sphere {
|
|
||||||
point: Point3,
|
|
||||||
radius: f64,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Sphere {
|
|
||||||
pub fn new(point: Point3, radius: f64) -> Sphere {
|
|
||||||
Sphere { point, radius }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub enum Geometry {
|
|
||||||
Sphere(Sphere),
|
|
||||||
Plane(Plane),
|
|
||||||
Line(Line),
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
mod tests {
|
|
||||||
use super::*;
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn new_plane() {
|
|
||||||
let p = Point3::new(1.0, 2.0, 3.0);
|
|
||||||
let n = Vector3::new(4.0, 5.0, 6.0);
|
|
||||||
|
|
||||||
let plane = Plane::new(p, n);
|
|
||||||
|
|
||||||
assert_eq!(plane.point, p);
|
|
||||||
assert_eq!(plane.normal, n);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
#[should_panic]
|
|
||||||
fn foo() {
|
|
||||||
panic!("This code panics!");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,8 +1,5 @@
|
|||||||
use hello::math::Point3;
|
use hello::math::Point3;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let p1 = Point3::new(0.0, 0.0, 0.0);
|
let p = Point3::new(0.0, 0.0, 0.0);
|
||||||
let p2 = Point3::new(0.0, 0.0, 0.0);
|
|
||||||
|
|
||||||
let v = p1 - p2;
|
|
||||||
}
|
}
|
||||||
|
|||||||
124
src/math.rs
124
src/math.rs
@ -1,124 +0,0 @@
|
|||||||
use std::ops::{Add, Div, Mul, Sub};
|
|
||||||
|
|
||||||
#[derive(Copy, Clone, PartialEq, Debug)]
|
|
||||||
pub struct Vector3 {
|
|
||||||
x: f64,
|
|
||||||
y: f64,
|
|
||||||
z: f64,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Vector3 {
|
|
||||||
pub fn new(x: f64, y: f64, z: f64) -> Vector3 {
|
|
||||||
Vector3 { x, y, z }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Add for Vector3 {
|
|
||||||
type Output = Vector3;
|
|
||||||
|
|
||||||
fn add(self, rhs: Vector3) -> Self::Output {
|
|
||||||
Vector3::new(self.x + rhs.x, self.y + rhs.y, self.z + rhs.z)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Sub for Vector3 {
|
|
||||||
type Output = Vector3;
|
|
||||||
|
|
||||||
fn sub(self, rhs: Vector3) -> Self::Output {
|
|
||||||
Vector3::new(self.x - rhs.x, self.y - rhs.y, self.z - rhs.z)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Mul<f64> for Vector3 {
|
|
||||||
type Output = Vector3;
|
|
||||||
|
|
||||||
fn mul(self, rhs: f64) -> Self::Output {
|
|
||||||
Vector3::new(self.x * rhs, self.y * rhs, self.z * rhs)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Mul<Vector3> for f64 {
|
|
||||||
type Output = Vector3;
|
|
||||||
|
|
||||||
fn mul(self, rhs: Vector3) -> Self::Output {
|
|
||||||
Vector3::new(self * rhs.x, self * rhs.y, self * rhs.z)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Mul for Vector3 {
|
|
||||||
type Output = f64;
|
|
||||||
|
|
||||||
fn mul(self, rhs: Vector3) -> Self::Output {
|
|
||||||
self.x * rhs.x + self.y * rhs.y + self.z * rhs.z
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Div<f64> for Vector3 {
|
|
||||||
type Output = Vector3;
|
|
||||||
|
|
||||||
fn div(self, rhs: f64) -> Self::Output {
|
|
||||||
Vector3::new(self.x / rhs, self.y / rhs, self.z / rhs)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Copy, Clone, PartialEq, Debug)]
|
|
||||||
pub struct Point3 {
|
|
||||||
x: f64,
|
|
||||||
y: f64,
|
|
||||||
z: f64,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Point3 {
|
|
||||||
pub fn new(x: f64, y: f64, z: f64) -> Point3 {
|
|
||||||
Point3 { x, y, z }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Add<Vector3> for Point3 {
|
|
||||||
type Output = Point3;
|
|
||||||
|
|
||||||
fn add(self, rhs: Vector3) -> Self::Output {
|
|
||||||
Point3::new(self.x + rhs.x, self.y + rhs.y, self.z + rhs.z)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Sub for Point3 {
|
|
||||||
type Output = Vector3;
|
|
||||||
|
|
||||||
fn sub(self, rhs: Point3) -> Self::Output {
|
|
||||||
Vector3::new(self.x - rhs.x, self.y - rhs.y, self.z - rhs.z)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
mod tests {
|
|
||||||
use super::*;
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn new_vector3() {
|
|
||||||
let v = Vector3::new(1.0, 2.0, 3.0);
|
|
||||||
assert_eq!(v.x, 1.0);
|
|
||||||
assert_eq!(v.y, 2.0);
|
|
||||||
assert_eq!(v.z, 3.0);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn add_vector3_to_vector3() {
|
|
||||||
let v1 = Vector3::new(1.0, 2.0, 3.0);
|
|
||||||
let v2 = Vector3::new(4.0, 5.0, 6.0);
|
|
||||||
|
|
||||||
let r = v1 + v2;
|
|
||||||
|
|
||||||
assert_eq!(r.x, 5.0);
|
|
||||||
assert_eq!(r.y, 7.0);
|
|
||||||
assert_eq!(r.z, 9.0);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn new_point3() {
|
|
||||||
let p = Point3::new(1.0, 2.0, 3.0);
|
|
||||||
assert_eq!(p.x, 1.0);
|
|
||||||
assert_eq!(p.y, 2.0);
|
|
||||||
assert_eq!(p.z, 3.0);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,2 +0,0 @@
|
|||||||
#[test]
|
|
||||||
fn very_long_running_test() {}
|
|
||||||
Loading…
x
Reference in New Issue
Block a user