160 likes | 303 Views
Grids to shapes. A small algorithmic problem. How to turn this grid. into these shapes?. Shapes are modelled as polygonal parts, and parts as closed sequences of points, connected by straight lines, clockwise for the outer perimeter, and anticlockwise for any holes.
E N D
Grids to shapes A small algorithmic problem
How to turn this grid into these shapes? Shapes are modelled as polygonal parts, and parts as closed sequences of points, connected by straight lines, clockwise for the outer perimeter, and anticlockwise for any holes. Assuming the lower left corner is the origin, and that the grid squares are unit sized, the shapes in the second diagram can be defined as: 1 (Yellow): Part 1: [(0,0), (0,5), (5,5), (5, 0), (0,0)], [(1,1), (4,1), (4,4), (1,4), (1,1)] Part 2: [(2,2), (2,3), (3,3), (3,2), (2,2)] 2 (Blue): Part 1: [(1,1), (1,4), (4,4), (4,1), (1,1)], [(2,2), (3,2), (3,3), (2,3), (2,2)] In practice there can easily be 2 million grid squares containing a thousand different values. We need to be efficient!
Step 1: Make horizontal boxes 1 1 1 1 1 1 2 2 2 1 1 2 1 2 1 1 2 2 2 1 1 1 1 1 1 Boxes are modelled as closed clockwise sequences of unit vectors.
Step 2: Join boxes of the same valueby removing adjacent complementary vectors 1 1 1 1 1 1 2 2 2 1 1 2 1 2 1 1 2 2 2 1 1 1 1 1 1
Step 2: Join boxes of the same valueby removing adjacent complementary vectors 1 1 1 1 1 1 2 2 2 1 1 2 1 2 1 1 2 2 2 1 1 1 1 1 1
Step 2: Join boxes of the same valueby removing adjacent complementary vectors 1 1 1 1 1 1 2 2 2 1 1 2 1 2 1 1 2 2 2 1 1 1 1 1 1
Step 2: Join boxes of the same valueby removing adjacent complementary vectors 1 1 1 1 1 1 2 2 2 1 1 2 1 2 1 1 2 2 2 1 1 1 1 1 1
Step 2: Join boxes of the same valueby removing adjacent complementary vectors 1 1 1 1 1 1 2 2 2 1 1 2 1 2 1 1 2 2 2 1 1 1 1 1 1
Step 2: Join boxes of the same valueby removing adjacent complementary vectors 1 1 1 1 1 1 2 2 2 1 1 2 1 2 1 1 2 2 2 1 1 1 1 1 1
Step 2: Join boxes of the same valueby removing adjacent complementary vectors 1 1 1 1 1 1 2 2 2 1 1 2 1 2 1 1 2 2 2 1 1 1 1 1 1
Step 2: Join boxes of the same valueby removing adjacent complementary vectors 1 1 1 1 1 1 2 2 2 1 1 2 1 2 1 1 2 2 2 1 1 1 1 1 1
Step 2: Join boxes of the same valueby removing adjacent complementary vectors 1 1 1 1 1 1 2 2 2 1 1 2 1 2 1 1 2 2 2 1 1 1 1 1 1
Step 2: Join boxes of the same valueby removing adjacent complementary vectors 1 1 1 1 1 1 2 2 2 1 1 2 1 2 1 1 2 2 2 1 1 1 1 1 1
Step 2: Join boxes of the same value by removing adjacent complementary vectors 1 1 1 1 1 1 2 2 2 1 1 2 1 2 1 1 2 2 2 1 1 1 1 1 1 How many polygons are there?
Step 3: Make holes by removing complementary pairs within the same box 1 1 1 1 1 1 2 2 2 1 1 2 1 2 1 1 2 2 2 1 1 1 1 1 1
Almost done! 1 1 1 1 1 1 2 2 2 1 1 2 1 2 1 1 2 2 2 1 1 1 1 1 1 All we have to do now is mark the vertices in the order of the arrows: Green: Part 1: [(0,4), (0,5), (5,5), (5, 0), (0,0), (0,4)], [(1,1), (4,1), (4,4), (1,4), (1,1)] Part 2: [(2,2), (2,3), (3,3), (3,2), (2,2)] Blue: Part 1: [(1,3), (1,4), (4,4), (4,1), (1,1), (1,3)], [(2,2), (3,2), (3,3), (2,3), (2,2)]