Compare commits
3 Commits
09ff36dff8
...
0a692c01de
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0a692c01de | ||
|
|
41eb9a9dc0 | ||
|
|
5b4c205234 |
126
src/lib.rs
126
src/lib.rs
@ -1,124 +1,2 @@
|
|||||||
use std::ops::{Add, Div, Mul, Sub};
|
mod geometry;
|
||||||
|
pub mod math;
|
||||||
#[derive(Copy, Clone)]
|
|
||||||
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)]
|
|
||||||
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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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),
|
|
||||||
}
|
|
||||||
|
|||||||
44
src/main.rs
44
src/main.rs
@ -1,45 +1,5 @@
|
|||||||
use hello::*;
|
use hello::math::Point3;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let sphere1 = Sphere::new(Point3::new(0.0, 0.0, 0.0), 25.0);
|
let p = Point3::new(0.0, 0.0, 0.0);
|
||||||
let sphere2 = Sphere::new(Point3::new(1.0, -45.4, 3.55), 2.0);
|
|
||||||
let sphere3 = Sphere::new(Point3::new(3.0, 2.0, 1.0), 0.5);
|
|
||||||
|
|
||||||
let plane1 = Plane::new(Point3::new(1.0, 2.0, 3.0), Vector3::new(0.0, 1.0, 0.0));
|
|
||||||
|
|
||||||
let mut v = Vec::new();
|
|
||||||
|
|
||||||
v.push(Geometry::Sphere(sphere1));
|
|
||||||
v.push(Geometry::Plane(plane1));
|
|
||||||
v.push(Geometry::Sphere(sphere2));
|
|
||||||
v.push(Geometry::Sphere(sphere3));
|
|
||||||
|
|
||||||
for geo in v {
|
|
||||||
match geo {
|
|
||||||
Geometry::Sphere(s) => println!("A Sphere"),
|
|
||||||
Geometry::Plane(p) => println!("A Plane"),
|
|
||||||
Geometry::Line(l) => println!("A Line"),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut lottery = [4, 8, 15, 16, 23, 42];
|
|
||||||
|
|
||||||
let mut sum = 0;
|
|
||||||
|
|
||||||
for value in lottery {
|
|
||||||
sum += value;
|
|
||||||
}
|
|
||||||
|
|
||||||
println!("The sum is {}", sum);
|
|
||||||
|
|
||||||
let mut lottery_string = Vec::new();
|
|
||||||
for value in lottery {
|
|
||||||
lottery_string.push(value.to_string());
|
|
||||||
}
|
|
||||||
|
|
||||||
let lottery_string_2: Vec<String> = lottery
|
|
||||||
.iter()
|
|
||||||
.filter(|x| **x > 8)
|
|
||||||
.map(|x| x.to_string())
|
|
||||||
.collect();
|
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user