rust/integral-geometry/src/lib.rs
changeset 14134 09f62bb046ef
parent 14133 a65b60f36b96
child 14135 7f5a591e1c43
equal deleted inserted replaced
14133:a65b60f36b96 14134:09f62bb046ef
     1 extern crate fpnum;
     1 extern crate fpnum;
     2 
     2 
     3 use fpnum::distance;
     3 use fpnum::distance;
     4 use std::cmp::max;
     4 use std::{
     5 use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Range, RangeInclusive, Sub, SubAssign};
     5     cmp::max,
       
     6     ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Range, RangeInclusive, Sub, SubAssign}
       
     7 };
       
     8 
     6 
     9 
     7 #[derive(PartialEq, Eq, Clone, Copy, Debug)]
    10 #[derive(PartialEq, Eq, Clone, Copy, Debug)]
     8 pub struct Point {
    11 pub struct Point {
     9     pub x: i32,
    12     pub x: i32,
    10     pub y: i32,
    13     pub y: i32,
   462 
   465 
   463     pub fn iter<'a>(&'a self) -> impl Iterator<Item = &Point> + 'a {
   466     pub fn iter<'a>(&'a self) -> impl Iterator<Item = &Point> + 'a {
   464         (&self.vertices[..self.edges_count()]).iter()
   467         (&self.vertices[..self.edges_count()]).iter()
   465     }
   468     }
   466 
   469 
   467     fn iter_mut<'a>(&'a mut self) -> impl Iterator<Item = &mut Point> + 'a {
   470     pub fn iter_mut<'a>(&'a mut self) -> impl Iterator<Item = &mut Point> + 'a {
   468         let edges_count = self.edges_count();
   471         let edges_count = self.edges_count();
   469         (&mut self.vertices[..edges_count]).iter_mut()
   472         let start = self.vertices.as_mut_ptr();
   470     }
   473         let end = unsafe { start.add(self.vertices.len()) };
   471 
   474         PolygonPointsIteratorMut { source: self, start, end }
   472     pub fn for_each<F>(&mut self, f: F)
   475     }
   473         where F: (Fn(&mut Point))
   476 
   474     {
   477     fn force_close(&mut self) {
   475         if !self.vertices.is_empty() {
   478         if !self.vertices.is_empty() {
   476             self.iter_mut().for_each(f);
   479             self.vertices[0] = self.vertices[self.vertices.len() - 1];
   477             let edges_count = self.edges_count();
       
   478             self.vertices[edges_count] = self.vertices[0]
       
   479         }
   480         }
   480     }
   481     }
   481 
   482 
   482     pub fn iter_edges<'a>(&'a self) -> impl Iterator<Item = Line> + 'a {
   483     pub fn iter_edges<'a>(&'a self) -> impl Iterator<Item = Line> + 'a {
   483         (&self.vertices[0..self.edges_count()])
   484         (&self.vertices[0..self.edges_count()])
   484             .iter()
   485             .iter()
   485             .zip(&self.vertices[1..])
   486             .zip(&self.vertices[1..])
   486             .map(|(s, e)| Line::new(*s, *e))
   487             .map(|(s, e)| Line::new(*s, *e))
       
   488     }
       
   489 }
       
   490 
       
   491 struct PolygonPointsIteratorMut<'a> {
       
   492     source: &'a mut Polygon,
       
   493     start: *mut Point,
       
   494     end: *mut Point
       
   495 }
       
   496 
       
   497 impl <'a> Iterator for PolygonPointsIteratorMut<'a> {
       
   498     type Item = &'a mut Point;
       
   499 
       
   500     fn next(&mut self) -> Option<<Self as Iterator>::Item> {
       
   501         if self.start == self.end {
       
   502             None
       
   503         } else {
       
   504             unsafe {
       
   505                 let result = &mut *self.start;
       
   506                 self.start = self.start.add(1);
       
   507                 Some(result)
       
   508             }
       
   509         }
       
   510     }
       
   511 }
       
   512 
       
   513 impl <'a> Drop for PolygonPointsIteratorMut<'a> {
       
   514     fn drop(&mut self) {
       
   515         self.source.force_close();
   487     }
   516     }
   488 }
   517 }
   489 
   518 
   490 impl From<Vec<Point>> for Polygon {
   519 impl From<Vec<Point>> for Polygon {
   491     fn from(mut v: Vec<Point>) -> Self {
   520     fn from(mut v: Vec<Point>) -> Self {