Compare commits

...

11 Commits

Author SHA1 Message Date
Stephan Rehfeld
45cad795a7 Use enums 2026-04-29 18:40:36 +02:00
Stephan Rehfeld
5daaf9e1bc Iterating over collection using match 2026-04-29 18:31:53 +02:00
Stephan Rehfeld
bb45906753 Important Rust Pattern: Inheritance does not exist, use enum to store multiple different types in same Collection 2026-04-29 18:28:57 +02:00
Stephan Rehfeld
b7a1f52360 Putting some Sphere into a Vec 2026-04-29 18:19:56 +02:00
Stephan Rehfeld
b1963c7e6a Adding some Geometries 2026-04-29 18:14:00 +02:00
Stephan Rehfeld
9ef8e08936 Impl Trait add for Point ad Vector3 2026-04-29 18:08:30 +02:00
Stephan Rehfeld
31e224f9b0 Implementing Div Trait 2026-04-29 18:02:48 +02:00
Stephan Rehfeld
07b2fc28d8 Implemting multiplication of vector3 with scalar 2026-04-29 17:53:40 +02:00
Stephan Rehfeld
31c9bda5a2 Implementing Sub for our Vector 2026-04-29 17:49:48 +02:00
Stephan Rehfeld
794228dfce Implementing Add for our Vector3 2026-04-29 17:47:42 +02:00
Stephan Rehfeld
28df45da67 Minimal example to show implementation of traits 2026-04-29 17:38:52 +02:00

View File

@ -1,91 +1,148 @@
//#[derive(Copy, Clone)]
struct RGB {
red: u8,
green: u8,
blue: u8,
use std::ops::{Add, Div, Mul, Sub};
use std::collections::
#[derive(Copy, Clone)]
struct Vector3 {
x: f64,
y: f64,
z: f64,
}
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 Vector3 {
fn new(x: f64, y: f64, z: f64) -> Vector3 {
Vector3 { x, y, z }
}
}
struct Image {
width: usize,
height: usize,
data: Vec<RGB>,
}
impl Add for Vector3 {
type Output = Vector3;
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()
fn add(self, rhs: Vector3) -> Self::Output {
Vector3::new(self.x + rhs.x, self.y + rhs.y, self.z + rhs.z)
}
}
struct Container<'a> {
value: &'a RGB,
}
impl Sub for Vector3 {
type Output = Vector3;
impl<'a> Container<'a> {
fn new(value: &RGB) -> Container {
Container { value }
fn sub(self, rhs: Vector3) -> Self::Output {
Vector3::new(self.x - rhs.x, self.y - rhs.y, self.z - rhs.z)
}
}
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<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)]
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 image = Image::white_image(640, 480);
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 black = RGB::black();
let plane1 = Plane::new(Point3::new(1.0, 2.0, 3.0), Vector3::new(0.0, 1.0, 0.0));
let c1 = image.get(0, 0);
//let c2 = image.get(1, 1);
let mut v = Vec::new();
let brightest = brighter(c1, &black);
v.push(Geometry::Sphere(sphere1));
v.push(Geometry::Plane(plane1));
v.push(Geometry::Sphere(sphere2));
v.push(Geometry::Sphere(sphere3));
println!("{}", brightest.red);
for geo in v {
match geo {
Geometry::Sphere(s) => println!("A Sphere"),
Geometry::Plane(p) => println!("A Plane"),
Geometry::Line(l) => println!("A Line"),
}
}
}