Important Rust Pattern: Inheritance does not exist, use enum to store multiple different types in same Collection
This commit is contained in:
parent
b7a1f52360
commit
bb45906753
20
src/main.rs
20
src/main.rs
@ -95,6 +95,12 @@ struct Plane {
|
||||
normal: Vector3,
|
||||
}
|
||||
|
||||
impl Plane {
|
||||
fn new(point: Point3, normal: Vector3) -> Plane {
|
||||
Plane { point, normal }
|
||||
}
|
||||
}
|
||||
|
||||
struct Line {
|
||||
point: Point3,
|
||||
direction: Vector3,
|
||||
@ -111,14 +117,22 @@ impl Sphere {
|
||||
}
|
||||
}
|
||||
|
||||
enum Geometry {
|
||||
Sphere(Sphere),
|
||||
Plane(Plane),
|
||||
}
|
||||
|
||||
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 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(sphere1);
|
||||
v.push(sphere2);
|
||||
v.push(sphere3);
|
||||
v.push(Geometry::Sphere(sphere1));
|
||||
v.push(Geometry::Plane(plane1));
|
||||
v.push(Geometry::Sphere(sphere2));
|
||||
v.push(Geometry::Sphere(sphere3));
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user