Compare commits
No commits in common. "45cad795a78aa4dae35c0d18d74fa6d21509da99" and "81205e5b29d05fd5cdff5911d69b651b4d99c386" have entirely different histories.
45cad795a7
...
81205e5b29
195
src/main.rs
195
src/main.rs
@ -1,148 +1,91 @@
|
||||
use std::ops::{Add, Div, Mul, Sub};
|
||||
use std::collections::
|
||||
|
||||
#[derive(Copy, Clone)]
|
||||
struct Vector3 {
|
||||
x: f64,
|
||||
y: f64,
|
||||
z: f64,
|
||||
//#[derive(Copy, Clone)]
|
||||
struct RGB {
|
||||
red: u8,
|
||||
green: u8,
|
||||
blue: u8,
|
||||
}
|
||||
|
||||
impl Vector3 {
|
||||
fn new(x: f64, y: f64, z: f64) -> Vector3 {
|
||||
Vector3 { x, y, z }
|
||||
impl RGB {
|
||||
fn new(red: u8, green: u8, blue: u8) -> RGB {
|
||||
RGB { red, green, blue }
|
||||
}
|
||||
|
||||
fn black() -> RGB {
|
||||
RGB::new(0, 0, 0)
|
||||
}
|
||||
|
||||
fn white() -> RGB {
|
||||
RGB::new(255, 255, 255)
|
||||
}
|
||||
}
|
||||
|
||||
impl Add for Vector3 {
|
||||
type Output = Vector3;
|
||||
struct Image {
|
||||
width: usize,
|
||||
height: usize,
|
||||
data: Vec<RGB>,
|
||||
}
|
||||
|
||||
fn add(self, rhs: Vector3) -> Self::Output {
|
||||
Vector3::new(self.x + rhs.x, self.y + rhs.y, self.z + rhs.z)
|
||||
impl Image {
|
||||
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 {
|
||||
type Output = Vector3;
|
||||
struct Container<'a> {
|
||||
value: &'a RGB,
|
||||
}
|
||||
|
||||
fn sub(self, rhs: Vector3) -> Self::Output {
|
||||
Vector3::new(self.x - rhs.x, self.y - rhs.y, self.z - rhs.z)
|
||||
impl<'a> Container<'a> {
|
||||
fn new(value: &RGB) -> Container {
|
||||
Container { value }
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
fn brighter<'a>(a: &'a RGB, b: &'a RGB) -> &'a RGB {
|
||||
if a.red / 3 + a.green / 3 + a.blue / 3 > b.red / 3 + b.green / 3 + b.blue / 3 {
|
||||
a
|
||||
} else {
|
||||
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() {
|
||||
let sphere1 = Sphere::new(Point3::new(0.0, 0.0, 0.0), 25.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 image = Image::white_image(640, 480);
|
||||
|
||||
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));
|
||||
v.push(Geometry::Plane(plane1));
|
||||
v.push(Geometry::Sphere(sphere2));
|
||||
v.push(Geometry::Sphere(sphere3));
|
||||
let brightest = brighter(c1, &black);
|
||||
|
||||
for geo in v {
|
||||
match geo {
|
||||
Geometry::Sphere(s) => println!("A Sphere"),
|
||||
Geometry::Plane(p) => println!("A Plane"),
|
||||
Geometry::Line(l) => println!("A Line"),
|
||||
}
|
||||
}
|
||||
println!("{}", brightest.red);
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user