chiark / gitweb /
graph.mp: Pack important magic into definitions.
authorMark Wooding <mdw@distorted.org.uk>
Mon, 4 May 2009 13:21:27 +0000 (14:21 +0100)
committerMark Wooding <mdw@distorted.org.uk>
Mon, 4 May 2009 13:21:27 +0000 (14:21 +0100)
graph.mp

index 33d49e933dc860c12ae59aaeafa16a3cf3dbd80d..b7dd12445773382b43e460325322a92da0296882 100644 (file)
--- a/graph.mp
+++ b/graph.mp
@@ -1,57 +1,60 @@
 numeric paperh, paperw;
-numeric sqsz, hexsz;
 numeric fine;
 numeric margin;
 
 paperh := 297 mm;
 paperw := 210 mm;
-sqsz := 5 mm;
-hexsz := 4 mm;
 fine := 1/4 pt;
 margin := 20 mm;
 
-beginfig(1);
-  numeric w, h;
-  numeric ox, oy, lx, ly;
-
-  w = floor((paperw - 2 margin)/sqsz);
-  h = floor((paperh - 2 margin)/sqsz);
-  ox = (paperw - w * sqsz)/2; lx = ox + w * sqsz;
-  oy = (paperh - h * sqsz)/2; ly = oy + h * sqsz;
-
-  pickup pencircle scaled fine;
-  for i = ox step sqsz until lx:
-    draw (i, oy) .. (i, ly);
-  endfor
-  for i = oy step sqsz until ly:
-    draw (ox, i) .. (lx, i);
-  endfor
-endfig;
-
-beginfig(2);
-  numeric ox, oy, lx, ly;
-  numeric w, h;
-  numeric v;
-  numeric a, b, c, d, e;
-
-  v = hexsz * sind 60;
-  w = floor((paperw - 2 margin)/(3 hexsz) - 1/2);
-  h = floor((paperh - 2 margin)/(2 v) - 1/2);
-  ox = (paperw - (w + 1/2) * 3 hexsz)/2; lx = ox + w * 3 hexsz;
-  oy = (paperh - (h + 1/2) * 2 v)/2; ly = oy + h * 2 v;
-
-  pickup pencircle scaled fine;
-  for i = ox step 3 hexsz until lx:
-    a := i + 1/2 hexsz; b := i + 2 hexsz; c := i + 3/2 hexsz;
-    for j = oy step 2 v until ly:
-      d := j + v; e := j + 2 v;
-      draw (a, e) -- (i, d) -- (a, j) -- (c, j) -- (b, d) -- (c, e);
-      if i < lx:
-       draw (b, d) -- (b + hexsz, d);
-      fi
-    endfor
-    draw (a, e) -- (c, e);
-  endfor
-endfig;
+numeric page; page := 0;
+
+def squares(expr hsqsz, vsqsz) =
+  page := page + 1;
+  beginfig(page);
+    numeric w, h;
+    numeric ox, oy, lx, ly;
+
+    w = floor((paperw - 2 margin)/hsqsz);
+    h = floor((paperh - 2 margin)/vsqsz);
+    ox = (paperw - w * hsqsz)/2; lx = ox + w * hsqsz;
+    oy = (paperh - h * vsqsz)/2; ly = oy + h * vsqsz;
 
-end;
+    pickup pencircle scaled fine;
+    for i = ox step hsqsz until lx:
+      draw (i, oy) .. (i, ly);
+    endfor
+    for i = oy step vsqsz until ly:
+      draw (ox, i) .. (lx, i);
+    endfor
+  endfig;
+enddef;
+
+def hexes(expr hexsz) =
+  page := page + 1;
+  beginfig(page);
+    numeric ox, oy, lx, ly;
+    numeric w, h;
+    numeric v;
+    numeric a, b, c, d, e;
+
+    v = hexsz * sind 60;
+    w = floor((paperw - 2 margin)/(3 hexsz) - 1/2);
+    h = floor((paperh - 2 margin)/(2 v) - 1/2);
+    ox = (paperw - (w + 1/2) * 3 hexsz)/2; lx = ox + w * 3 hexsz;
+    oy = (paperh - (h + 1/2) * 2 v)/2; ly = oy + h * 2 v;
+
+    pickup pencircle scaled fine;
+    for i = ox step 3 hexsz until lx:
+      a := i + 1/2 hexsz; b := i + 2 hexsz; c := i + 3/2 hexsz;
+      for j = oy step 2 v until ly:
+       d := j + v; e := j + 2 v;
+       draw (a, e) -- (i, d) -- (a, j) -- (c, j) -- (b, d) -- (c, e);
+       if i < lx:
+         draw (b, d) -- (b + hexsz, d);
+       fi
+      endfor
+      draw (a, e) -- (c, e);
+    endfor
+  endfig;
+enddef;