Created
November 30, 2017 15:36
-
-
Save anonymous/26be0905cb311eb38fd87d858eee4074 to your computer and use it in GitHub Desktop.
Rust code shared from the playground
This file contains 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
pub struct Convex<T: AsRef<[Point]> + ?Sized> { | |
vertices: T | |
} | |
pub struct Point(f32, f32); | |
impl<T: AsRef<[Point]>> Convex<T> { | |
pub fn new(vertices: T) -> Option<Convex<T>> { | |
fn is_polygon_convex(_: &[Point]) -> bool { true } | |
let is_convex = { | |
let vs = vertices.as_ref(); | |
vs.len() >= 3 && is_polygon_convex(vs) | |
}; | |
if is_convex { | |
Some(Convex { vertices }) | |
} else { | |
None | |
} | |
} | |
} | |
impl<T: AsRef<[Point]> + ?Sized> Convex<T> { | |
fn pointsum(&self) -> Point { | |
self.vertices.as_ref().iter().fold(Point(0.0, 0.0), |p1, p2|Point(p1.0 + p2.0, p1.1 + p2.1)) | |
} | |
} | |
fn main() { | |
let points = [Point(1.0, 2.0), Point(3.0, 4.0), Point(5.0, 6.0)]; | |
let c = Convex::new(points).expect("A convex"); | |
let c: &Convex<[Point]> = &c; | |
let ps = c.pointsum(); | |
println!("{} {}", ps.0, ps.1); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment