Compare commits

..

No commits in common. "45cad795a78aa4dae35c0d18d74fa6d21509da99" and "81205e5b29d05fd5cdff5911d69b651b4d99c386" have entirely different histories.

View File

@ -1,148 +1,91 @@
use std::ops::{Add, Div, Mul, Sub}; //#[derive(Copy, Clone)]
use std::collections:: struct RGB {
red: u8,
#[derive(Copy, Clone)] green: u8,
struct Vector3 { blue: u8,
x: f64,
y: f64,
z: f64,
} }
impl Vector3 { impl RGB {
fn new(x: f64, y: f64, z: f64) -> Vector3 { fn new(red: u8, green: u8, blue: u8) -> RGB {
Vector3 { x, y, z } RGB { red, green, blue }
}
fn black() -> RGB {
RGB::new(0, 0, 0)
}
fn white() -> RGB {
RGB::new(255, 255, 255)
} }
} }
impl Add for Vector3 { struct Image {
type Output = Vector3; width: usize,
height: usize,
data: Vec<RGB>,
}
fn add(self, rhs: Vector3) -> Self::Output { impl Image {
Vector3::new(self.x + rhs.x, self.y + rhs.y, self.z + rhs.z) fn new(width: usize, height: usize, data: Vec<RGB>) -> Image {
if width * height != data.len() {
panic!("Width and height of image does not match amount of data.");
}
let img = Image {
width,
height,
data,
};
img
}
fn white_image(width: usize, height: usize) -> Image {
let mut data = Vec::new();
for i in 0..width * height {
data.push(RGB::white());
}
Image::new(width, height, data)
}
fn get(&self, x: usize, y: usize) -> &RGB {
self.data.get(y * self.width + x).unwrap()
}
fn get_mut(&mut self, x: usize, y: usize) -> &mut RGB {
self.data.get_mut(y * self.width + x).unwrap()
} }
} }
impl Sub for Vector3 { struct Container<'a> {
type Output = Vector3; value: &'a RGB,
}
fn sub(self, rhs: Vector3) -> Self::Output { impl<'a> Container<'a> {
Vector3::new(self.x - rhs.x, self.y - rhs.y, self.z - rhs.z) fn new(value: &RGB) -> Container {
Container { value }
} }
} }
impl Mul<f64> for Vector3 { fn brighter<'a>(a: &'a RGB, b: &'a RGB) -> &'a RGB {
type Output = Vector3; if a.red / 3 + a.green / 3 + a.blue / 3 > b.red / 3 + b.green / 3 + b.blue / 3 {
a
fn mul(self, rhs: f64) -> Self::Output { } else {
Vector3::new(self.x * rhs, self.y * rhs, self.z * rhs) b
} }
} }
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)]
struct Point3 {
x: f64,
y: f64,
z: f64,
}
impl Point3 {
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)
}
}
struct Plane {
point: Point3,
normal: Vector3,
}
impl Plane {
fn new(point: Point3, normal: Vector3) -> Plane {
Plane { point, normal }
}
}
struct Line {
point: Point3,
direction: Vector3,
}
struct Sphere {
point: Point3,
radius: f64,
}
impl Sphere {
fn new(point: Point3, radius: f64) -> Sphere {
Sphere { point, radius }
}
}
enum Geometry {
Sphere(Sphere),
Plane(Plane),
Line(Line),
}
fn main() { fn main() {
let sphere1 = Sphere::new(Point3::new(0.0, 0.0, 0.0), 25.0); let image = Image::white_image(640, 480);
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 black = RGB::black();
let mut v = Vec::new(); let c1 = image.get(0, 0);
//let c2 = image.get(1, 1);
v.push(Geometry::Sphere(sphere1)); let brightest = brighter(c1, &black);
v.push(Geometry::Plane(plane1));
v.push(Geometry::Sphere(sphere2));
v.push(Geometry::Sphere(sphere3));
for geo in v { println!("{}", brightest.red);
match geo {
Geometry::Sphere(s) => println!("A Sphere"),
Geometry::Plane(p) => println!("A Plane"),
Geometry::Line(l) => println!("A Line"),
}
}
} }