320 pub fn intersects(&self, other: &Rect) -> bool { |
320 pub fn intersects(&self, other: &Rect) -> bool { |
321 self.left() <= self.right() |
321 self.left() <= self.right() |
322 && self.right() >= other.left() |
322 && self.right() >= other.left() |
323 && self.top() <= other.bottom() |
323 && self.top() <= other.bottom() |
324 && self.bottom() >= other.top() |
324 && self.bottom() >= other.top() |
|
325 } |
|
326 } |
|
327 |
|
328 pub struct Polygon { |
|
329 vertices: Vec<Point> |
|
330 } |
|
331 |
|
332 impl Polygon { |
|
333 pub fn new(vertices: &[Point]) -> Self { |
|
334 let mut v = Vec::with_capacity(vertices.len() + 1); |
|
335 v.extend_from_slice(vertices); |
|
336 if !v.is_empty() { |
|
337 let start = v[0]; |
|
338 v.push(start); |
|
339 } |
|
340 Self { vertices: v } |
|
341 } |
|
342 |
|
343 pub fn edges_count(&self) -> usize { |
|
344 self.vertices.len() - 1 |
|
345 } |
|
346 |
|
347 pub fn get_edge(&self, index: usize) -> Line { |
|
348 Line::new(self.vertices[index], self.vertices[index + 1]) |
|
349 } |
|
350 |
|
351 pub fn iter<'a>(&'a self) -> impl Iterator<Item = Point> + 'a { |
|
352 (&self.vertices[..self.edges_count()]).iter().cloned() |
|
353 } |
|
354 |
|
355 pub fn iter_edges<'a>(&'a self) -> impl Iterator<Item = Line> + 'a { |
|
356 (&self.vertices[0..self.edges_count()]) |
|
357 .iter() |
|
358 .zip(&self.vertices[1..]) |
|
359 .map(|(s, e)| Line::new(*s, *e)) |
|
360 } |
|
361 } |
|
362 |
|
363 impl From<Vec<Point>> for Polygon { |
|
364 fn from(mut v: Vec<Point>) -> Self { |
|
365 if !v.is_empty() && v[0] != v[v.len() - 1] { |
|
366 let start = v[0]; |
|
367 v.push(start) |
|
368 } |
|
369 Self { vertices: v } |
325 } |
370 } |
326 } |
371 } |
327 |
372 |
328 #[derive(PartialEq, Eq, Clone, Copy, Debug)] |
373 #[derive(PartialEq, Eq, Clone, Copy, Debug)] |
329 pub struct Line { |
374 pub struct Line { |