SHARE
TWEET

Untitled

a guest Jul 20th, 2019 65 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #[derive(Debug)]
  2. struct Indexer {
  3.     from:  i64,
  4.     to:    i64,
  5.     index: i64,
  6. }
  7. impl Indexer {
  8.     fn new(from: i64, to: i64) -> Indexer {
  9.         let index = from;
  10.         Indexer { from, to, index }
  11.     }
  12. }
  13.  
  14. enum Mode {
  15.     SetupTop,
  16.     RenderTop,
  17.     SetupBottom,
  18.     RenderBottom,
  19.     Done
  20. }
  21.  
  22. struct Triangle {
  23.     mode:   Mode,
  24.     v_scan: Indexer,
  25.     h_scan: Indexer,
  26.     v0:     Point,
  27.     v1:     Point,
  28.     v2:     Point,
  29.     height: i64,
  30. }
  31. impl Triangle {
  32.     fn new(v0: Point, v1: Point, v2: Point) -> Triangle {
  33.         let mut set = vec![v0, v1, v2];
  34.         set.sort_by(|a, b| a.y.cmp(&b.y));
  35.         let mode   = Mode::SetupTop;
  36.         let v0     = set[0].clone();
  37.         let v1     = set[1].clone();
  38.         let v2     = set[2].clone();
  39.         let height = v2.y - v0.y;
  40.         let v_scan = Indexer::new(0, 0);
  41.         let h_scan = Indexer::new(0, 0);
  42.         Triangle { mode, v_scan, h_scan, v0, v1, v2, height }
  43.     }
  44.    
  45.     #[inline]
  46.     fn set_horizontal_indexer_top(&mut self) {
  47.         let y      = self.v_scan.index;
  48.         let height = self.v1.y - self.v0.y;
  49.         let sx     = (y - self.v0.y) as f32 / self.height as f32;
  50.         let sy     = (y - self.v0.y) as f32 / height as f32;
  51.         let m0     = (self.v0.x + (self.v2.x - self.v0.x)) as f32 * sx;
  52.         let m1     = (self.v0.x + (self.v1.x - self.v0.x)) as f32 * sy;
  53.         let min    = if m0 < m1 { m0 } else { m1 };
  54.         let max    = if m0 > m1 { m0 } else { m1 };
  55.         self.h_scan = Indexer::new(min as i64, max as i64);
  56.     }
  57.  
  58.     #[inline]
  59.     fn set_horizontal_indexer_bottom(&mut self) {
  60.         let y      = self.v_scan.index;
  61.         let height = self.v2.y - self.v1.y;
  62.         let sx     = (y - self.v0.y) as f32 / self.height as f32;
  63.         let sy     = (y - self.v1.y) as f32 / height as f32;
  64.         let m0     = (self.v0.x + (self.v2.x - self.v0.x)) as f32 * sx;
  65.         let m1     = (self.v1.x + (self.v1.x - self.v1.x)) as f32 * sy;
  66.         let min    = if m0 < m1 { m0 } else { m1 };
  67.         let max    = if m0 > m1 { m0 } else { m1 };
  68.         self.h_scan = Indexer::new(min as i64, max as i64);
  69.     }
  70.  
  71.     fn increment(&mut self) {
  72.         match self.mode {
  73.             Mode::Done => {
  74.                 panic!("increment called in done mode.")
  75.             }
  76.             Mode::SetupTop => {
  77.                 self.mode = Mode::RenderTop
  78.             },
  79.             Mode::RenderTop => {
  80.                 self.h_scan.index += 1;
  81.                 if self.h_scan.index > self.h_scan.to {
  82.                     self.v_scan.index += 1;
  83.                     if self.v_scan.index > self.v_scan.to {
  84.                         self.mode = Mode::SetupBottom;
  85.                     }
  86.                 }
  87.             },
  88.             Mode::SetupBottom => {
  89.                 self.mode = Mode::RenderBottom
  90.             },
  91.             Mode::RenderBottom => {
  92.                 self.h_scan.index += 1;
  93.                 if self.h_scan.index > self.h_scan.to {
  94.                     self.v_scan.index += 1;
  95.                     if self.v_scan.index > self.v_scan.to {
  96.                         self.mode = Mode::Done;
  97.                     }
  98.                 }
  99.             }
  100.         }
  101.     }
  102.  
  103.     fn step(&mut self) -> Option<(i64, i64)> {
  104.         match self.mode {
  105.             Mode::SetupTop => {
  106.                 self.v_scan = Indexer::new(self.v0.y, self.v1.y);
  107.                 self.set_horizontal_indexer_top();
  108.                 let x = self.h_scan.index;
  109.                 let y = self.v_scan.index;
  110.                 self.increment();
  111.                 Some((x, y))
  112.             },
  113.             Mode::RenderTop => {
  114.                 let x = self.h_scan.index;
  115.                 let y = self.v_scan.index;
  116.                 self.increment();
  117.                 Some((x, y))
  118.             },
  119.             Mode::SetupBottom => {
  120.                 self.v_scan = Indexer::new(self.v1.y, self.v2.y);
  121.                 self.set_horizontal_indexer_bottom();
  122.                 let x = self.h_scan.index;
  123.                 let y = self.v_scan.index;
  124.                 self.increment();
  125.                 Some((x, y))
  126.             },
  127.             Mode::RenderBottom => {
  128.                 let x = self.h_scan.index;
  129.                 let y = self.v_scan.index;
  130.                 self.increment();
  131.                 Some((x, y))
  132.             },
  133.             Mode::Done => None
  134.         }
  135.     }
  136. }
  137.  
  138. impl Iterator for Triangle {
  139.     type Item = (i64, i64);
  140.  
  141.     fn next(&mut self) -> Option<(i64, i64)> {
  142.        self.step()
  143.     }
  144. }
  145.  
  146. fn main() {
  147.  
  148.     let mut triangle = Triangle::new(
  149.         Point::new( 0,  0),
  150.         Point::new(-10, 10),
  151.         Point::new( 10, 10)
  152.     );
  153.  
  154.     for x in triangle {
  155.         println!("{:?}", x);
  156.     }
  157.  
  158.     // println!("{:?}", triangle.next());
  159.     // println!("{:?}", triangle.next());
  160.     // println!("{:?}", triangle.next());
  161.     // println!("{:?}", triangle.next());
  162.     // println!("{:?}", triangle.next());
  163. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top