grid.rs 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. extern crate gunpowder_treason as gt;
  2. #[macro_use] extern crate itertools;
  3. const MAP: &'static [u8] =
  4. b"0000000000000000000001110000111100111100111111001111001001000111010011\
  5. 1111101110000001001111010011100111111000000100000001111111111111111100\
  6. 0111111000000000010001111110111011111110000001001110111111100111010011\
  7. 1011111110011101000100001000000111111111000010000001110100010001110000\
  8. 0111010011110111111000000100111101110010011101001111000000100111111011\
  9. 1101110010011100101111011111100010001000000111001001110111110001110110\
  10. 0010011111110111011001110111110101110111001001111101111101100111011111\
  11. 000111000000000000000000000000";
  12. fn main() {
  13. let (w, h) = (11.0, 14.0);
  14. let mut drawing = gt::svg(w, h);
  15. drawing.add(gt::rect((0.0, 0.0), (11.0, 14.0)));
  16. const N: f64 = 0.5;
  17. let rows: usize = ((w - 1.0) / N).floor() as usize;
  18. let cols: usize = ((h - 1.0) / N).floor() as usize;
  19. let x_offset = (w - (rows as f64 * N)) / 2.0;
  20. let y_offset = (h - (cols as f64 * N)) / 2.0;
  21. let get_cell = |x: usize, y: usize| {
  22. let idx = x + y * rows;
  23. MAP.get(idx)
  24. };
  25. for (x, y) in iproduct!(0..rows-1, 0..cols-1) {
  26. let xc = x_offset + x as f64 * N;
  27. let yc = y_offset + y as f64 * N;
  28. let d = N / 5.0;
  29. let cs = N / 10.0;
  30. if get_cell(x, y)
  31. .and_then(|l| get_cell(x + 1, y)
  32. .and_then(|r| l != r)).unwrap_or(false) {
  33. drawing.add(gt::line(xc+N, yc).to(xc+N, yc+N));
  34. let xt = if get_cell(x, y) == '0' as u8 {
  35. xc + N - 0.1
  36. } else {
  37. xc + N + 0.1
  38. };
  39. for n in 0..10 {
  40. let yt = yc + cs * (n as f64 + 0.5);
  41. drawing.add(gt::line(xc+N, yt).to(xt, yt));
  42. }
  43. } else if get_cell(x, y).unwrap() == '1' as u8 {
  44. for n in 1..5 {
  45. drawing.add(gt::line(xc+N, yc + d * n as f64));
  46. }
  47. }
  48. if get_cell(x, y) != get_cell(x, y+1) {
  49. drawing.add(gt::line(xc, yc+N).to(xc+N, yc+N));
  50. let yt = if get_cell(x, y) == '0' as u8 {
  51. yc + N - 0.1
  52. } else {
  53. yc + N + 0.1
  54. };
  55. for n in 0..10 {
  56. let xt = xc + cs * (n as f64 + 0.5);
  57. drawing.add(gt::line(xt, yc+N).to(xt, yt));
  58. }
  59. } else if get_cell(x, y).unwrap() == '1' as u8 {
  60. for n in 1..5 {
  61. drawing.add(gt::line(xc + d * n as f64, yc+N));
  62. }
  63. }
  64. }
  65. drawing.to_stdout();
  66. }