terrain-map
ARCHIVED - repo for blog post http://www.vogt.world/writing/procedural-terrain-generation/
git clone https://git.vogt.world/terrain-map.git
Log | Files | README.md
← Commit log
commit
message
Reducing margin on Greedy Raindrop algorithm
author
Ben Vogt <[email protected]>
date
2016-05-31 01:01:31
stats
2 file(s) changed, 21 insertions(+), 21 deletions(-)
files
index.html
landmap.js
  1diff --git a/index.html b/index.html
  2index f80397f..fb9e74d 100644
  3--- a/index.html
  4+++ b/index.html
  5@@ -127,7 +127,7 @@
  6       <div class="row">
  7         <h4>Smoothing a Complex Erosion Map</h4>
  8         <p>
  9-          Eroding a map before applying smoothing. <code>regular -> complexErosion -> smooth</code>
 10+          Eroding a map with Complex Erosion Algorithm before applying smoothing. <code>regular -> complexErosion -> smooth</code>
 11           <button class="run button-primary" id="smooth-complex-erosion">GENERATE</button>
 12         </p>
 13       </div>
 14@@ -136,7 +136,7 @@
 15       <div class="row">
 16         <h4>Smoothing a Simple Erosion Map</h4>
 17         <p>
 18-          Eroding a map before applying smoothing. <code>regular -> simpleErosion -> smooth</code>
 19+          Eroding a map with Simple Erosion Algorithm before applying smoothing. <code>regular -> simpleErosion -> smooth</code>
 20           <button class="run button-primary" id="smooth-simple-erosion">GENERATE</button>
 21         </p>
 22       </div>
 23diff --git a/landmap.js b/landmap.js
 24index f7ebcf2..e3c8a12 100644
 25--- a/landmap.js
 26+++ b/landmap.js
 27@@ -215,10 +215,9 @@ LandMap.prototype.grd = function(options) {
 28   }
 29 
 30   var operationAray = new Array(this.size * this.size);
 31-  var size = this.size
 32   for (var y = 0; y < this.size; y++) {
 33     for (var x = 0; x < this.size; x++) {
 34-      operationAray[(x + size * y)] = [0];
 35+      operationAray[(x + this.size * y)] = [0];
 36     }
 37   }
 38 
 39@@ -238,46 +237,48 @@ LandMap.prototype.grd = function(options) {
 40     }
 41 
 42     // iterate through all
 43-    for (var y = MARGIN; y < this.size - MARGIN; y++) {
 44-      for (var x = MARGIN; x < this.size - MARGIN; x++) {
 45+    // for (var xRange = Math.max(x - Math.round(MARGIN), 0); xRange < Math.min(x + Math.round(MARGIN), this.size); xRange++) {
 46+    //   for (var yRange = Math.max(y - Math.round(MARGIN), 0); yRange < Math.min(y + Math.round(MARGIN), this.size); yRange++) {
 47+    for (var y = 1; y < this.size-2; y++) {
 48+      for (var x = 1; x < this.size-2; x++) {
 49         var neighbors = [
 50           this.get(featureTo, x - 1, y),
 51           this.get(featureTo, x + 1, y),
 52           this.get(featureTo, x, y - 1),
 53           this.get(featureTo, x, y + 1)
 54         ];
 55-        operationAray[(x + size * y)].push(this.get(featureTo, x, y));
 56+        operationAray[(x + this.size * y)].push(this.get(featureTo, x, y));
 57         var index = indexOfMax(featureTo);
 58         var thisValue = this.get(featureTo, x, y);
 59         if (neighbors[index] > thisValue) {
 60           if (index == 0) {
 61             // WEST (left)
 62-            operationAray[(x + size * y)].push(this.get(featureTo, x - 1, y) * percent);
 63-            operationAray[((x - 1) + size * y)].push(this.get(featureTo, x - 1, y) * (percent) * (-1));
 64+            operationAray[(x + this.size * y)].push(this.get(featureTo, x - 1, y) * percent);
 65+            operationAray[((x - 1) + this.size * y)].push(this.get(featureTo, x - 1, y) * (percent) * (-1));
 66           } else if (index == 1) {
 67             // EAST (right)
 68-            operationAray[(x + size * y)].push(this.get(featureTo, x + 1, y) * percent);
 69-            operationAray[((x + 1) + size * y)].push(this.get(featureTo, x + 1, y) * (percent) * (-1));
 70+            operationAray[(x + this.size * y)].push(this.get(featureTo, x + 1, y) * percent);
 71+            operationAray[((x + 1) + this.size * y)].push(this.get(featureTo, x + 1, y) * (percent) * (-1));
 72           } else if (index == 2) {
 73             // NORTH (up)
 74-            operationAray[(x + size * y)].push(this.get(featureTo, x, y - 1) * percent);
 75-            operationAray[(x + size * (y - 1))].push(this.get(featureTo, x - 1, y) * (percent) * (-1));
 76+            operationAray[(x + this.size * y)].push(this.get(featureTo, x, y - 1) * percent);
 77+            operationAray[(x + this.size * (y - 1))].push(this.get(featureTo, x - 1, y) * (percent) * (-1));
 78           } else if (index == 3) {
 79             // SOUTH (down)
 80-            operationAray[(x + size * y)].push(this.get(featureTo, x, y + 1) * percent);
 81-            operationAray[(x + size * (y + 1))].push(this.get(featureTo, x, y + 1) * (percent) * (-1));
 82+            operationAray[(x + this.size * y)].push(this.get(featureTo, x, y + 1) * percent);
 83+            operationAray[(x + this.size * (y + 1))].push(this.get(featureTo, x, y + 1) * (percent) * (-1));
 84           }
 85         }
 86       }
 87     }
 88     //iterate through summing the operationAray, and setting it
 89-    for (var y = MARGIN; y < size - MARGIN; y++) {
 90-      for (var x = MARGIN; x < size - MARGIN; x++) {
 91-        var value = operationAray[(x + size * y)].reduce(function(a, b) {
 92+    for (var y = 1; y < this.size - 2; y++) {
 93+      for (var x = 1; x < this.size - 2; x++) {
 94+        var value = operationAray[(x + this.size * y)].reduce(function(a, b) {
 95           return a + b;
 96         }, 0);
 97         this.set(featureTo, x, y, value);
 98-        operationAray[(x + size * y)] = [value];
 99+        operationAray[(x + this.size * y)] = [value];
100       }
101     }
102   }