0

I made a map using the diamond-square algorithm. It works, but there are areas exclusively at the top of the map that seem to be using values from the other side of the map. I do not know what is causing this, and would like some help finding it.

Here's an image of the problem: The Problem

This is the relevant code, but I do not know where the problem occurs.

p = [
    [y[0] + c, y[1]],
    [y[0] - c, y[1]],
    [y[0], y[1] + c],
    [y[0], y[1] - c]
];

for (var m = 0; m < 4; m++) {
    //Calculate Current Suare
    var t = [];
    // Add only those points to t that do not cross the edge in 
    // the direction in which they move away from p[m]:
    if (p[m][0] + c <= s) {
        t.push([p[m][0] + c, p[m][1]]);
    }
    if (p[m][0] - c >= 0) {
        t.push([p[m][0] - c, p[m][1]]);
    }
    if (p[m][1] + c <= s) {
        t.push([p[m][0], p[m][1] + c]);
    }
    if (p[m][1] - c >= 0) {
        t.push([p[m][0], p[m][1] - c]);
    }
    var z = [
        p[m],
        y
    ];
    //Check for edge
    if ((p[m][0] === 0 || p[m][0] == s) || (p[m][1] === 0 || p[m][1] == s)) {
        for (var k = 0; k < t.length; k++) {
            if ((t[k][0] < 0 || t[k][0] > s) || (t[k][1] < 0 || t[k][1] > s)) {
                t.splice(k, 1);
                break;
            }
        }
    }
    //Set values
    ar[p[m][0]][p[m][1]] = (t.map(function (e) {
        return ar[e[0]][e[1]];
    }).reduce(function (a, b) {
        return a + b;
    }) / t.length) + rand(n);
}

Here is a fiddle of the complete code: http://jsfiddle.net/Shamadruu/7zutLnfL/18/

4
  • I have not seen your algorithm fully but I think splice might be your issue. Can you please add a k-- after your t.splice(k,1) and run your code again ?
    – trk
    Oct 31, 2015 at 22:05
  • Make sure to remove the break as you add the k--. This is because you cannot be sure that there is only one edge that is crossed. There might be two of them, so you need to stay in the loop to search for another. But k-- is then important.
    – trincot
    Oct 31, 2015 at 22:11
  • Adding k-- and removing the break doesn't seem to have any effect. I only need to check for one point being over the edge because the point they are calculated from is ON the edge, and must be at least c away from any other edge. Oct 31, 2015 at 22:37
  • To clarify, p[m] is a point on the edge when it goes through that loop, and is at least c away from any other edge. The points in t are the points that are used to calculate the value of p[m]. Since p[m] is on the edge, there must be one, and only one, point past the edge. Oct 31, 2015 at 22:41

1 Answer 1

0

The problem is in the for(var k loop: you stop looking for cross-edge conditions once you have found one (you break), but there could be two of them. Although you could solve that by removing the break and adding k--;, there is a more efficient way to deal with the edges: test before adding the points to t whether they are within the edges. With these particular points that means you need to test them only against one edge each, which is a savings of factor 4!

var t = [];
// Add only those points to t that do not cross the edge in 
// the direction in which they move away from p[m]:
if (p[m][0] + c <= s) {
    t.push([p[m][0] + c, p[m][1]]);
}
if ([p[m][0] - c >= 0) {
    t.push([p[m][0] - c, p[m][1]]);
}
if (p[m][1] + c <= s) {
    t.push([p[m][0], p[m][1] + c]);
}
if (p[m][1] - c >= 0) {
    t.push([p[m][0], p[m][1] - c]);
}
var z = [
    p[m],
    y,
];
//Set values ...etc.

Similarly, you will have to review the inclusion of squares in z to make sure they do not cross the edge:

//Find New Square
switch (m) {
    case 0:
        if (p[m][0] - c >= 0 && p[m][1] + c <= s) {
            z.push([p[m][0] - c, p[m][1] + c], [p[m][0], p[m][1] + c]);
        }
        break;
// ... etc.
4
  • Unfortunately, the problem persists. The change you provided functions, but it doesn't solve the problem. Oct 31, 2015 at 22:58
  • I have added another part that might need change. I don't know the algorithm, and it seems quite complex.
    – trincot
    Oct 31, 2015 at 23:21
  • actually pretty simple. Here's a Wikipedia article on the algorithm: en.wikipedia.org/wiki/Diamond-square_algorithm. As my code stands, z cannot be off the edge since I've already set it up so that the addition/subtraction cannot take it over the edge. The program would crash if it tried to use a value that is off the edge anyway, so the fact that it doesn't crash suggests that such doesn't happen. The problem I have probably isn't caused by it trying to grab a value that is over an edge. Oct 31, 2015 at 23:24
  • I look now to your full code, but it is not clean. In functions that receive a global variable as an argument you still refer to the global variable instead of the argument. Look for example in draw(), where you use both em and ems. Are you trying to trick yourself? This is really hard-to-read code!
    – trincot
    Nov 1, 2015 at 7:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.