8
Challenge
Write a program which, given a 2-dimensional boolean array (equivalently, a monochromatic bitmap), outputs a series of polygons that describe the outline of the region that is “true” (1).
The input is provided as a sequence of '#'
(hash), ' '
(space) and \n
(newline) characters. Lines may differ in length, in which case the missing parts are assumed to be spaces. The output should be a (newline-separated) list of polygons, each polygon represented by a (comma-separated) list of coordinates.
Examples & Requirements
The co-ordinates must be listed in clockwise order. Input:
#
Acceptable outputs include:
(0,0), (1,0), (1,1), (0,1) (1,0), (1,1), (0,1), (0,0) (1,1), (0,1), (0,0), (1,0) (0,1), (0,0), (1,0), (1,1)
Disjoint regions must return multiple polygons. Input:
# #
Example output (actual output must consist of two lines):
(0,0), (1,0), (1,1), (0,1) (2,0), (3,0), (3,1), (2,1)
Holes in a polygon must be listed as a separate polygon, but in anti-clockwise order. Input:
### # # ###
Example output:
(0,0), (3,0), (3,3), (0,3) (1,1), (1,2), (2,2), (2,1)
You are free to choose whether diagonally adjacent vertices join up or not. Input:
# #
Example output:
(0,0), (1,0), (1,1), (0,1) (1,1), (2,1), (2,2), (1,2)
or
(0,0), (1,0), (1,1), (2,1), (2,2), (1,2), (1,1), (0, 1)
The lists of coordinates need not be optimally short. For example:
##
Acceptable outputs:
(0,0), (2,0), (2,1), (0,1) // Redundant coordinates along a straight line are acceptable (0,0), (1,0), (2,0), (2,1), (1,1), (0,1) // Duplicate start- and end-point are acceptable (0,0), (2,0), (2,1), (0,1), (0,0)
As usual, shortest program “wins”.
I’ve relaxed the conditions a little, so whether diagonally-adjacent things join up or not is now optional. I’ve also deleted my comments defending the old requirements. – Timwi – 2015-12-19T21:58:41.897
1Can you explain the corollary of point 5? I find it highly counterintuitive. – Peter Taylor – 2011-03-08T17:19:27.463
if you said that in a square AB over CD then BC are always connected and AD are always disconnected that would be consistent, but you seem to be saying (in effect) that the squares aren't really squares and their shapes change subtly when you convert a # to a space or vice versa. – Peter Taylor – 2011-03-09T08:06:09.563
I've added a couple of tags to help categorize this beast. [optimized-output] is intended to represent you condition "The lists of coordinates need not be optimally short", and I would be happy if someone could find a better expression for it. – dmckee --- ex-moderator kitten – 2011-03-11T19:25:29.783