Permalink
Browse files

mod: default params

  • Loading branch information...
1 parent 6da32ca commit 40f353e838b6dc2bbf91c553bc4003aef51dbd69 @qiao committed Feb 16, 2012
Showing with 49 additions and 36 deletions.
  1. +11 −11 demo/index.html
  2. +0 −2 demo/main.js
  3. +38 −23 lib/terrain.js
View
@@ -50,7 +50,7 @@ <h1 id="title">Fractal Terrain Generator</h1>
<input class="opt" id="opt-smoothness" type="text" name="smoothness" min="0.1" max="5" step="0.1" value="1.0"><br>
<label for="z-scale">Z-Scale:</label>
- <input class="opt" id="opt-z" type="text" name="z-scale" min="0" max="400" step="20" value="200"><br>
+ <input class="opt" id="opt-z" type="text" name="z-scale" min="0" max="500" step="20" value="340"><br>
<a id="new" href="#">new</a>
</form>
@@ -61,17 +61,17 @@ <h1 id="title">Fractal Terrain Generator</h1>
<a href="https://github.com/qiao/fractal-terrain-generator"><img style="position: absolute; top: 0; right: 0; border: 0;" src="https://a248.e.akamai.net/assets.github.com/img/71eeaab9d563c2b3c590319b398dd35683265e85/687474703a2f2f73332e616d617a6f6e6177732e636f6d2f6769746875622f726962626f6e732f666f726b6d655f72696768745f677261795f3664366436642e706e67" alt="Fork me on GitHub"></img></a>
<script type="text/javascript" src="./main.js"></script>
<script type="text/javascript">
- $(function() {
- var _gaq = _gaq || [];
- _gaq.push(['_setAccount', 'UA-21856083-3']);
- _gaq.push(['_trackPageview']);
+ TerrainController.init();
- (function() {
- var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
- ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
- var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
- })();
- });
+ var _gaq = _gaq || [];
+ _gaq.push(['_setAccount', 'UA-21856083-3']);
+ _gaq.push(['_trackPageview']);
+
+ (function() {
+ var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
+ ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
+ var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
+ })();
</script>
</body>
</html>
View
@@ -378,5 +378,3 @@ var TerrainController = (function() {
return { init: init };
})();
-
-TerrainController.init();
View
@@ -106,15 +106,17 @@
*/
function diamond(matrix, depth, smoothness) {
- var matSize = matrix.length - 1;
+ var len = matrix.length;
+ var terrainSize = len - 1;
var numSegs = 1 << (depth - 1);
- var span = matSize / numSegs;
+ var span = terrainSize / numSegs;
+ var half = span / 2;
// enumerate sub-squares
// for each sub-square, the height of the center is caculated
// by averaging the height of its four vertices plus a random offset.
- for (var x = 0; x < matSize; x += span) {
- for (var y = 0; y < matSize; y += span) {
+ for (var x = 0; x < terrainSize; x += span) {
+ for (var y = 0; y < terrainSize; y += span) {
// (x, y)
// \
// a---b---c
@@ -129,7 +131,7 @@
//
var va = [x, y];
var vc = [x + span, y];
- var ve = [x + span / 2, y + span / 2];
+ var ve = [x + half, y + half];
var vg = [x, y + span];
var vi = [x + span, y + span];
@@ -159,13 +161,15 @@
*/
function square(matrix, depth, smoothness) {
- var matSize = matrix.length - 1;
+ var len = matrix.length
+ var terrainSize = len - 1;
var numSegs = 1 << (depth - 1);
- var span = matSize / numSegs;
+ var span = terrainSize / numSegs;
+ var half = span / 2;
// enumerate sub-dimaonds
- for (var x = 0; x < matSize; x += span) {
- for (var y = 0; y < matSize; y += span) {
+ for (var x = 0; x < terrainSize; x += span) {
+ for (var y = 0; y < terrainSize; y += span) {
// for each sub-square, the height of the center is caculated
// by averaging the height of its four vertices plus a random offset.
// for example,
@@ -190,19 +194,30 @@
// span
//
var va = [x, y];
- var vb = [x + span / 2, y];
+ var vb = [x + half, y];
var vc = [x + span, y];
- var vf = [x, y + span / 2];
- var vg = [x + span / 2, y + span / 2];
- var vh = [x + span, y + span / 2];
+ var vf = [x, y + half];
+ var vg = [x + half, y + half];
+ var vh = [x + span, y + half];
var vk = [x, y + span];
- var vl = [x + span / 2, y + span];
+ var vl = [x + half, y + span];
var vm = [x + span, y + span];
+
+ // right of h
+ var vhr = [x + half * 3, y + half];
+ if (vhr[0] > terrainSize) vhr[0] = half;
- var vhr = [(x + span / 2 * 3) % matrix.length, y + span / 2]; // right of h
- var vfl = [(x - span / 2 + matrix.length) % matrix.length, y + span / 2]; // left of f
- var vlu = [x + span / 2, (y + span / 2 * 3) % matrix.length]; // under l
- var vba = [x + span / 2, (y - span / 2 + matrix.length) % matrix.length]; // above b
+ // left of f
+ var vfl = [x - half, y + half]
+ if (vfl[0] < 0) vfl[0] = terrainSize - half;
+
+ // under l
+ var vlu = [x + half, y + half * 3];
+ if (vlu[1] > terrainSize) vlu[1] = half;
+
+ // above b
+ var vba = [x + half, y - half]
+ if (vba[1] < 0) vba[1] = terrainSize - half;
squareHelper(matrix, depth, smoothness, va, vg, vk, vfl, vf);
squareHelper(matrix, depth, smoothness, va, vba, vc, vg, vb);
@@ -213,11 +228,11 @@
// set the elevations of the rightmost and bottom vertices to
// equal the leftmost and topmost ones'.
- for (var y = 0; y < matSize; y += span) {
- matrix[y][matSize] = matrix[y][0];
+ for (var y = 0; y < terrainSize; y += span) {
+ matrix[y][terrainSize] = matrix[y][0];
}
- for (var x = 0; x < matSize; x += span) {
- matrix[matSize][x] = matrix[0][x];
+ for (var x = 0; x < terrainSize; x += span) {
+ matrix[terrainSize][x] = matrix[0][x];
}
}
@@ -240,7 +255,7 @@
function getH(smoothness, depth) {
var sign = Math.random() > 0.5 ? 1 : -1;
var reduce = 1;
- for (var i = 1; i < depth; ++i) {
+ for (var i = 0; i < depth; ++i) {
reduce *= Math.pow(2, -smoothness);
}
return sign * Math.random() * reduce;

0 comments on commit 40f353e

Please sign in to comment.