Advertisement
Guest User

Untitled

a guest
Oct 27th, 2016
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.73 KB | None | 0 0
  1. use std::collections::HashMap;
  2.  
  3. #[derive(PartialEq, PartialOrd, Debug)]
  4. struct Point(u32, u32);
  5.  
  6. #[derive(PartialEq, PartialOrd, Debug)]
  7. struct Dimension(u32, u32);
  8.  
  9. #[derive(PartialEq, PartialOrd, Debug)]
  10. struct Rectangle(Point, Point);
  11.  
  12. #[derive(PartialEq, PartialOrd, Debug, Hash, Eq)]
  13. struct Coordinate(u32, u32);
  14.  
  15. #[derive(Clone, Copy, Debug)]
  16. enum Shape {
  17. Canvas,
  18. Circle,
  19. HorizontalLine,
  20. VerticalLine,
  21. DiagonalLineLeftToRight,
  22. DiagonalLineRightToLeft,
  23. }
  24.  
  25. fn canvas_index_to_coords(i: u32, num: u32) -> Coordinate {
  26. if i < num { Coordinate(i, 0) }
  27. else { Coordinate(i % num, i / num) }
  28. }
  29.  
  30. fn write(coords: &Coordinate, chr: char, num: u32) {
  31. if coords.0 == num - 1 { println!("{}", chr); }
  32. else { print!("{} ", chr); }
  33. // if coords.0 == num - 1 { println!("{} ({}/{})", chr, coords.0, coords.1); }
  34. // else { print!("{} ({}/{}) ", chr, coords.0, coords.1); }
  35. }
  36.  
  37. fn combine(a: HashMap<Coordinate, Shape>, b: HashMap<Coordinate, Shape>) -> HashMap<Coordinate, Shape> {
  38. let mut combined = HashMap::new();
  39.  
  40. for (key, val) in a {
  41. combined.insert(key, val);
  42. }
  43.  
  44. for (key, val) in b {
  45. combined.insert(key, val);
  46. }
  47.  
  48. combined
  49. }
  50.  
  51. fn canvas(size: Dimension) -> HashMap<Coordinate, Shape> {
  52. let mut canvas_coords = HashMap::new();
  53.  
  54. for i in 0..(size.0 * size.1) {
  55. canvas_coords.insert(canvas_index_to_coords(i, size.0), Shape::Canvas);
  56. }
  57.  
  58. canvas_coords
  59. }
  60.  
  61. fn circle(radius: u32, point: Point) -> HashMap<Coordinate, Shape> {
  62. let x0 = point.0;
  63. let y0 = point.1;
  64.  
  65. let mut x = radius;
  66. let mut y = 0;
  67.  
  68. let mut err: i32 = 0;
  69.  
  70. let mut coords = HashMap::new();
  71.  
  72. while x >= y {
  73. coords.insert(Coordinate(x0 + x, y0 + y), Shape::Circle);
  74. coords.insert(Coordinate(x0 + y, y0 + x), Shape::Circle);
  75. coords.insert(Coordinate(x0 - y, y0 + x), Shape::Circle);
  76. coords.insert(Coordinate(x0 - x, y0 + y), Shape::Circle);
  77. coords.insert(Coordinate(x0 - x, y0 - y), Shape::Circle);
  78. coords.insert(Coordinate(x0 - y, y0 - x), Shape::Circle);
  79. coords.insert(Coordinate(x0 + y, y0 - x), Shape::Circle);
  80. coords.insert(Coordinate(x0 + x, y0 - y), Shape::Circle);
  81.  
  82. y += 1;
  83. err += 1 + 2 * y as i32;
  84.  
  85. if 2 * (err - x as i32) + 1 > 0
  86. {
  87. x -= 1;
  88. err += 1 - 2 * x as i32;
  89. }
  90. }
  91.  
  92. coords
  93. }
  94.  
  95. fn line_shape(rectangle: Rectangle) -> Shape {
  96. let x0 = (rectangle.0).0;
  97. let y0 = (rectangle.0).1;
  98. let x1 = (rectangle.1).0;
  99. let y1 = (rectangle.1).1;
  100.  
  101. if x0 != x1 && y0 > y1 { Shape::DiagonalLineLeftToRight }
  102. else if x0 != x1 && y0 < y1 { Shape::DiagonalLineRightToLeft }
  103. else if y0 == y1 { Shape::HorizontalLine }
  104. else { Shape::VerticalLine }
  105. }
  106.  
  107. fn line(rectangle: Rectangle) -> HashMap<Coordinate, Shape> {
  108. let x0 = (rectangle.0).0 as i32;
  109. let y0 = (rectangle.0).1 as i32;
  110. let x1 = (rectangle.1).0 as i32;
  111. let y1 = (rectangle.1).1 as i32;
  112.  
  113. let dx = ((x1 - x0)).abs();
  114.  
  115. let sx: i32 = if x0 < x1 { 1 } else { -1 };
  116.  
  117. let dy = ((y1 - y0)).abs();
  118. let sy: i32 = if y0 < y1 { 1 } else { -1 };
  119.  
  120. let tmp = if dx > dy { dx } else { -dy };
  121. let mut err = tmp / 2;
  122. let mut e2;
  123.  
  124. let mut x0_m = x0;
  125. let mut y0_m = y0;
  126.  
  127. let mut coords = HashMap::new();
  128. let line_shape = line_shape(rectangle);
  129.  
  130. loop {
  131. coords.insert(Coordinate(x0_m as u32, y0_m as u32), line_shape);
  132.  
  133. if x0_m == x1 as i32 && y0_m == y1 as i32 {
  134. break;
  135. }
  136.  
  137. e2 = err;
  138.  
  139. if e2 > -dx {
  140. err -= dy;
  141. x0_m += sx;
  142. }
  143.  
  144. if e2 < dy {
  145. err += dx;
  146. y0_m += sy;
  147. }
  148. }
  149.  
  150. coords
  151. }
  152.  
  153. fn draw(num: u32, coords: HashMap<Coordinate, Shape>) {
  154. let mut vec = Vec::new();
  155.  
  156. for (key, value) in &coords {
  157. vec.push((key, value));
  158. }
  159.  
  160. vec.sort_by_key(|&(coord, _)| coord.0);
  161. vec.sort_by_key(|&(coord, _)| (coord.1 as i32) * -1);
  162.  
  163. for (coord, shape) in vec {
  164. match shape {
  165. &Shape::Canvas => write(coord, ' ', num),
  166. &Shape::Circle => write(coord, 'o', num),
  167. &Shape::HorizontalLine => write(coord, '-', num),
  168. &Shape::VerticalLine => write(coord, '|', num),
  169. &Shape::DiagonalLineLeftToRight => write(coord, '\', num),
  170. &Shape::DiagonalLineRightToLeft => write(coord, '/', num),
  171. }
  172. }
  173. }
  174.  
  175. fn main() {
  176. let num = 10;
  177. let canvas_size = Dimension(num, num);
  178. let point_1 = Point(2, 2);
  179. let point_2 = Point(3, 4);
  180. let point_3 = Point(7, 7);
  181. let rectangle = Rectangle(Point(0, 0), Point(0, 9));
  182.  
  183. draw(num, combine(canvas(canvas_size), combine(circle(1, point_3), combine(circle(1, point_2), combine(circle(1, point_1), line(rectangle))))));
  184. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement