User:Watchduck/bin2svg

bin2svg is a small program that takes a binary matrix and returns the corresponding SVG path of the area covered by ones.

It was initially written in Matlab and later translated to Python. The code is shown below.

Files created with this program are marked with and sorted into the hidden category Created with bin2svg.

Example
>>> from numpy import array >>> from bin2svg import bin2svg >>> mat = array(0, 0, 0, 1], [1, 1, 1, 0], [1, 0, 1, 0], [1, 1, 1, 0) >>> mat array(0, 0, 0, 1],       [1, 1, 1, 0],        [1, 0, 1, 0],        [1, 1, 1, 0) >>> bin2svg(mat) 'M3,0h1v1h-1M0,1h3v3h-3M1,2v1h1v-1'

If the function gets the binary matrix $$\begin{bmatrix} 0 & 0 & 0 & 1 \\ 1 & 1 & 1 & 0 \\ 1 & 0 & 1 & 0 \\ 1 & 1 & 1 & 0 \end{bmatrix}$$ it will create the intermediate matrix $$\begin{bmatrix} 0 & 0 & 0 & 1 & 2 \\ 1 & 0 & 0 & 24 & 3 \\ 0 & 5 & 8 & 0 & 0 \\ 0 & 6 & 7 & 0 & 0 \\ 4 & 0 & 0 & 3 & 0 \end{bmatrix}$$ which shows that there are three cycles of corners: Two on the outside of an area of ones (with clockwise numbers 1...4) and one hole (with anticlockwise numbers 5...8). (The 24 is a 2 and a 4 in the same place.) The outside cycles give the paths  and , and the hole gives. So the output of the function is.

In the following SVG code the result produces the image shown on the right:

Python code

 * See also: http://pastebin.com/y8rY5Vj4