Help understand the marching squares algorithm

In my game, I want to place squares on the edges of a monochrome image: enter image description here

So, I found this algorithm that should solve the problem. http://en.wikipedia.org/wiki/Marching_squares

It seems like it's hard to implement, I just think I don’t understand what the Wiki is saying. I think I need to split the image into cells, where each cell displays 2x2 pixels in the image? It's right? Then I lose this instruction:

For each cell in the contour grid:

1.Perform 4 bits in the corners of the cell to build a binary index: clockwise the cell, add a bit to the index using bitwise OR and left shift, from the most significant bit in the upper left, to the least significant bit in the lower left corner. The resulting 4-bit index can have 16 possible values ​​in the range 0-15.

I am not sure how to add a bit.

thanks

+6
source share
1 answer

After creating 2x2 cells for each of them, you calculate a number like this:

  • set value to 0
  • if the top left point is above the threshold, add 8
  • if top right dot is higher add 4
  • if lower right dot is higher add 2
  • if the bottom left point is above the threshold, add 1.

Edited formatting.

+1
source

Source: https://habr.com/ru/post/887967/


All Articles