Created
May 6, 2020 23:09
-
-
Save sakex/efcb79cadd175a8d1a2d343b8cf341a7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct Point { | |
x: f64, | |
y: f64, | |
z: f64, | |
} | |
impl Point { | |
pub fn mid_point(&self, other: &Point) -> Point { | |
Point { | |
x: (self.x + other.x) / 2.0, | |
y: (self.y + other.y) / 2.0, | |
z: (self.z + other.z) / 2.0, | |
} | |
} | |
} | |
impl Clone for Point { | |
fn clone(&self) -> Point { | |
Point { x: self.x, y: self.y, z: self.z } | |
} | |
} | |
struct Triangle { | |
points: (Point, Point, Point) // Let's say | |
} | |
impl Triangle { | |
pub fn new(p1: &Point, p2: &Point, p3: &Point) -> Triangle { | |
Triangle { | |
points: (p1.clone(), p2.clone(), p3.clone()) | |
} | |
} | |
fn mid_points(&self) -> (Point, Point, Point) { | |
// Returns a list of midpoints between angles in this order: (0, 1), (0, 2), (1, 2) | |
(self.points.0.mid_point(&self.points.1), | |
self.points.0.mid_point(&self.points.2), | |
self.points.1.mid_point(&self.points.2) | |
) | |
} | |
fn divide(&self, depth: i32) -> Vec<Triangle> { | |
let mid_points = self.mid_points(); | |
// adjust mid points to be at a distance r from the center while still being aligned in the middle | |
let new_triangles: Vec<Triangle> = vec![ | |
Triangle::new(&mid_points.0, &self.points.1, &mid_points.2,), // Up | |
Triangle::new(&self.points.0, &mid_points.0, &mid_points.1), // left | |
Triangle::new(&mid_points.0, &mid_points.2, &mid_points.1), // middle | |
Triangle::new(&mid_points.1, &mid_points.2, &self.points.2), // right | |
]; | |
if depth > 0 { | |
let mut ret: Vec<Triangle>; | |
new_triangles.iter().map(|triangle| triangle.divide(depth - 1)).flatten().collect() | |
} | |
else { | |
new_triangles | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment