Slide around in a spiral, rather than in random straight lines. This produces a much denser overall texture. default tip
authorBenjamin Smedberg <benjamin@smedbergs.us>
Mon, 15 Dec 2008 15:46:12 -0500
changeset 17 f968472d57b8
parent 16 fd877bb1e6be
push id5
push userbsmedberg@mozilla.com
push dateMon, 15 Dec 2008 20:46:17 +0000
Slide around in a spiral, rather than in random straight lines. This produces a much denser overall texture.
wordmap.js
--- a/wordmap.js
+++ b/wordmap.js
@@ -226,27 +226,37 @@ function hitTest(fullimg, wordimg, x, y)
 
       if (wv && fv)
         return true;
     }
   }
   return false;
 }
 
-function slide(fullimg, wordimg, x1, y1, x2, y2)
+function slide(fullimg, wordimg, x, y)
 {
-  var hitting, steps, step, ix, iy;
+  var hitting, i, a, d, ix, iy;
+
+  hitting = hitTest(fullimg, wordimg, x, y);
 
-  hitting = hitTest(fullimg, wordimg, x1, y1);
+  for (i = 0; ; ++i) {
+    a = Math.pow(Math.log(i), 3) / 4;
+    d = a * 4 / 3;
 
-  steps = Math.max(Math.abs(x2 - x1), Math.abs(y2 - y1));
+    if (d > kWidth)
+      break;
+
+    ix = Math.floor(x + Math.sin(a) * d);
+    iy = Math.floor(y + Math.cos(a) * d);
 
-  for (step = 0; step < steps; ++step) {
-    ix = Math.floor(x1 + (x2 - x1) * step / steps);
-    iy = Math.floor(y1 + (y2 - y1) * step / steps);
+    if (ix < 0 || iy < 0 ||
+        ix + wordimg.width > fullimg.width ||
+        iy + wordimg.height > fullimg.height)
+      continue;
+
     if (hitTest(fullimg, wordimg, ix, iy) != hitting)
       return [ix, iy];
   }
 
   return [null, null];
 }
 
 function merge(fullimg, wordimg, x, y)
@@ -419,23 +429,20 @@ function draw(text)
   if (gShark) {
     stopShark();
     disconnectShark();
   }
 }
 
 function placeWord(imgdata, wdata)
 {
-  let x = normalInt(0, kWidth - wdata.width, 1);
-  let y = normalInt(0, kHeight - wdata.height, 1);
+  let x = normalInt(Math.floor(kWidth / 4), Math.floor(kWidth / 4 * 3), 3);
+  let y = normalInt(Math.floor(kHeight / 4), Math.floor(kHeight / 4 * 3), 3);
   for (let t = 1; t < 20; ++t) {
-    let x2 = normalInt(0, kWidth - wdata.width, t);
-    let y2 = normalInt(0, kWidth - wdata.height, t);
-
-    [x, y] = slide(imgdata, wdata, x, y, x2, y2);
+    [x, y] = slide(imgdata, wdata, x, y);
     if (x != null) {
       yield [x, y];
       return;
     }
 
     yield;
 
     x = x2;