Understanding Marching Square algorithm

Viewed 336

The following picture is retrieved from Wikipedia:

I haven't understood this part:

I have two questions here:

  1. Where did they obtain [8,4,1,2] from and what did they want to tell us by that?
  2. Take a look at cell [0, 0] whose value is 13. If I go clockwise along with its contouring values, I obtain the binary string 0010 which is 2. How does the 1st cell-value become 13?

.

enum What
{
    lines, surface, both
}
class Program
{
    public static void Print(int[,] data, int xn, int yn)
    {
        for (int j = 0; j < yn; j++)
        {
            for (int i = 0; i < xn; i++)
            {
                Console.Write(data[i,j] + ", ");
            }
            Console.WriteLine();
        }
    }
    public static int[,] normalize(int[,] data, int xn, int yn)
    {

        for (int j = 0; j < yn; j++)
        {
            for (int i = 0; i < xn; i++)
            {
                if (data[i, j] > 1)
                {
                    data[i, j] = 0;
                }
                else
                {
                    data[i, j] = 1;
                }
            }
        }

        return data;
    }

    public static int[,] marching_square(int x, int y, int[,] data, int isovalue, What what)
    {
        int xn = x;
        int yn = y;

        data = normalize(data, xn, yn);

        int[,] bitMask = new int[xn - 1, yn - 1];


        for (int j = 0; j < yn - 1; j++)
        {
            for (int i = 0; i < xn - 1; i++)
            {
                StringBuilder sb = new StringBuilder();
                sb.Append(data[i, j]);
                sb.Append(data[i, j + 1]);
                sb.Append(data[i + 1, j]);
                sb.Append(data[i + 1, j + 1]);

                bitMask[i, j] = Convert.ToInt32(sb.ToString(), 2);
            }
        }

        return bitMask;
    }

    static void Main(string[] args)
    {
        int[,] data = new int[,] { 
                                  { 1,1,1,1,1 },
                                  { 1,2,3,2,1 },
                                  { 1,3,3,3,1 },
                                  { 1,2,3,2,1 },
                                  { 1,1,1,1,1 }
                                  };

        Print(data, 5,5);

        int[,] bitMask = marching_square(5,5,data, 0, What.lines);

        Print(bitMask, 4, 4);
    }
}

Output:

1, 1, 1, 1, 1,
1, 2, 3, 2, 1,
1, 3, 3, 3, 1,
1, 2, 3, 2, 1,
1, 1, 1, 1, 1,

14, 10, 10, 11,
12, 0, 0, 3,
12, 0, 0, 3,
13, 5, 5, 7,

I inverted the bits. But, the output looks different.

0 Answers
Related