user1938413
user1938413

Reputation: 63

How to Select Points only at edges of a 2d array

So I have a float[,] heightmap, and as part of my river generating algorithm I want to select two points so long as they are part of one of the edges of the array. This seems a simple task, but I can't seem to come up with a solution that doesn't involve way too many if statements. Is there a way to select from the edges of a 2d array? (IE, x = 0 or x = max, or y = 0 or y = max)

Upvotes: 5

Views: 940

Answers (2)

JasonD
JasonD

Reputation: 16612

Just for novelty, here's a way of doing it which doesn't involve storing all the indices, or any if() blocks:

    static void randPoint(int width, int height, out int x, out int y, Random r)
    {
        int[] dim = {width,height};
        int[] res = new int[2];

        res[0] = r.Next(0, 2) * (width - 1);
        res[1] = r.Next(0, 2) * (height - 1);
        int hv = r.Next(0, 2);
        res[hv] = r.Next(0,dim[hv]);

        x = res[0];
        y = res[1];
    }

Upvotes: 2

Felix K.
Felix K.

Reputation: 6281

You could make a array with all edge-indicies like (0,10) and put all of them in one array, now you could simply select one or more of them.

Upvotes: 2

Related Questions