Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)

like image 732
user1938413 Avatar asked Oct 05 '22 12:10

user1938413


2 Answers

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.

like image 172
Felix K. Avatar answered Oct 10 '22 02:10

Felix K.


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];
    }
like image 31
JasonD Avatar answered Oct 10 '22 04:10

JasonD