-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path2svg
executable file
·116 lines (103 loc) · 3.19 KB
/
2svg
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#!jq -rf
##
# Converts voronoi diagram generated by ./voronoi.sh (or voronoi/voronoi.jq) to SVG.
# Usage:
# ./2svg --argjson box '[600, 600]' # where box = [width, height]
# or
# ./2svg --argjson box '[0, 0, 600, 600]' # where box = [minX, minY, maxX, maxY]
#
# @author hosuaby
##
# An implementation of the Microsoft C Runtime rand() pseudorandom number generator (PRNG) function.
# Credit to Joel Purra @ https://github.com/joelpurra
# @see https://github.com/joelpurra/jq-random-prng-microsoft
##
# 15-bit integers generated using the same formula as rand() from the Microsoft C Runtime.
# @input {[ number, number, number ]} count, state, rand
# @output {[ number, number, number ]} count, state, rand
def next_rand:
. as [ $count, $state ]
| ( (214013 * $state) + 2531011 ) % 2147483648 # mod 2^31
| [ $count+1 , ., (. / 65536 | floor) ]
;
##
# Generates the first n pseudo-random numbers:
# @input {void} nothing
# @param seed {number} seed for generator
# @param n {number} number of pseudo-random numbers to generate
# @output {number} pseudo-random numbers (n outputs)
def rand(seed; n):
[ 0, seed ]
| next_rand # the seed is not so random
| recurse(
if .[0] < n then
next_rand
else
empty
end
)
| .[2]
;
##
# Generates $n random RGB colors.
# @input {void} nothing
# @param {string[]} arrays of random colors
def random_colors($n):
[ rand(42; $n) | . % 256 ] as $red
| [ rand(17; $n) | . % 256 ] as $green
| [ rand(21; $n) | . % 256 ] as $blue
| reduce range($n) as $i (
[];
. + [ "rgb(\($red[$i]), \($green[$i]), \($blue[$i]))" ]
)
;
##
# Returns graphical representation of supplied site.
# @input {[ number, number ]} site coordinates
# @output {string} SVG circle representing a site
def site:
"<circle cx=\"\(.[0])\" cy=\"\(.[1])\" r=\"3\" fill=\"white\" />"
;
##
# Returns graphical representation of supplied cell.
# @input {[ number, number ][]} array of points forming cells polygon
# @param $i {number} number of the cell
# @param $colors {string[]} array of random colors for cells
# @output {string} SVG polygon representing a cell
def cell($i; $colors):
$colors[$i] as $color
| map("\(.[0]),\(.[1])")
| join(" ")
| "<polygon points=\"\(.)\" style=\"fill:\($color)\" />"
;
##
# Start of script
length as $l
| ( if $box | length == 4 then
$box
else
[0, 0, $box[0], $box[1]]
end ) as [ $minX, $minY, $maxX, $maxY ]
| ( $maxX - $minX ) as $width
| ( $maxY - $minY ) as $height
| random_colors($l) as $colors
| map(.[0] | site) as $sites
| ( reduce range($l) as $i (
{
cells: map(.[1:]),
polygons: []
};
( .cells[$i] | cell($i; $colors) ) as $polygon
| { polygons: (.polygons + [ $polygon ]), cells }
)
| .polygons ) as $polygons
| "<?xml version=\"1.0\" standalone=\"no\"?>
<svg xmlns=\"http://www.w3.org/2000/svg\"
version=\"1.1\"
width=\"\($width)px\"
height=\"\($height)px\"
viewBox=\"\($minX) \($minY) \($width) \($height)\">
<desc>jq-voronoi by hosuaby @ https://github.com/hosuaby</desc>
\($polygons | join("\n"))
\($sites | join("\n"))
</svg>"