-
Notifications
You must be signed in to change notification settings - Fork 0
/
knot
executable file
·182 lines (150 loc) · 4.15 KB
/
knot
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#!/usr/bin/env ruby
require 'nokogiri'
require 'optparse'
require_relative 'lib/knot'
include Knot
def debug_grid(doc, svg, grid)
Nokogiri::XML::Node.new 'g', doc do |g|
svg.add_child g
g[:fill] = 'white'
grid.each do |a|
Nokogiri::XML::Node.new 'circle', doc do |circle|
g.add_child circle
circle[:cx] = a.c.x
circle[:cy] = a.c.y
circle[:r] = 1
end
end
end
Nokogiri::XML::Node.new 'g', doc do |g|
svg.add_child g
g[:fill] = 'cyan'
grid.each do |a|
a.i.each do |k, v|
Nokogiri::XML::Node.new 'circle', doc do |circle|
g.add_child circle
circle[:cx] = v.x
circle[:cy] = v.y
circle[:r] = 1
end
end
end
grid.each do |a|
a.o.each do |k, v|
Nokogiri::XML::Node.new 'circle', doc do |circle|
g.add_child circle
circle[:cx] = v.x
circle[:cy] = v.y
circle[:r] = 1
end
end
end
end
end
options = {
vertical: 2,
horizontal: 2,
cell_size: 96,
knot_color: 'white',
knot_width: 24,
stroke_color: 'black',
stroke_width: 8,
debug: false,
}
OptionParser.new do |opts|
opts.banner = "Usage: ./knot [options]"
opts.on("-h", "--help", "Prints this help.") do
puts opts
exit
end
opts.on("-d", "--debug", "Draw the grid points.") do
options[:debug] = true
end
opts.on("-VVERTICAL", "--vertical=VERTICAL", "Number of cells vertically.") do |opt|
options[:vertical] = opt
end
opts.on("-HHORIZONTAL", "--horizontal=HORIZONTAL", "Number of cells horizontally.") do |opt|
options[:horizontal] = opt
end
opts.on("-GCELL_SIZE", "--cell-size=CELL_SIZE", "Dimensions of each cell in the grid.") do |opt|
options[:cell_size] = opt
end
opts.on("-bBG_COLOR", "--background-color=BG_COLOR", "Background colour.") do |opt|
options[:background_color] = opt
end
opts.on("-kKNOT_COLOR", "--knot-color=KNOT_COLOR", "Knot colour.") do |opt|
options[:knot_color] = opt
end
opts.on("-KKNOT_WIDTH", "--knot-width=KNOT_WIDTH", "Knot width.") do |opt|
options[:knot_width] = opt
end
opts.on("-sSTROKE_COLOR", "--stroke-color=STROKE_COLOR", "Stroke colour.") do |opt|
options[:stroke_color] = opt
end
opts.on("-SSTROKE_WIDTH", "--stroke-width=STROKE_WIDTH", "Stroke width.") do |opt|
options[:stroke_width] = opt
end
end.parse!
ROWS = options[:vertical].to_i * 2 + 1
COLS = options[:horizontal].to_i * 2 + 1
CELLSIZE = options[:cell_size].to_i
W = CELLSIZE * COLS
H = CELLSIZE * ROWS
grid = Grid.build(ROWS, COLS) do |i, j|
c = Point[CELLSIZE * j + CELLSIZE / 2,
CELLSIZE * i + CELLSIZE / 2]
Cell.new(c, options[:knot_width].to_i,
options[:stroke_width].to_i)
end
doc = Nokogiri::XML::Document.new
Nokogiri::XML::Node.new 'svg', doc do |svg|
doc.add_child svg
svg[:xmlns] = 'http://www.w3.org/2000/svg'
svg[:viewBox] = "0 0 #{W} #{H}"
svg[:width] = W
svg[:height] = H
bg_d = "M0,0 v#{H} h#{W} v-#{H} z";
# Background.
Nokogiri::XML::Node.new 'path', doc do |path|
svg.add_child path
path[:d] = bg_d
path[:fill] = options[:background_color]
end if options[:background_color]
# Stroke Mask
Nokogiri::XML::Node.new 'mask', doc do |mask|
svg.add_child mask
mask[:id] = 'strokeMask'
Nokogiri::XML::Node.new 'path', doc do |path|
mask.add_child path
path[:d] = bg_d
path[:fill] = 'white'
end
# Mask
Nokogiri::XML::Node.new 'path', doc do |path|
mask.add_child path
path[:d] = Knot.draw_mask grid
path[:fill] = 'black'
end
end
# Outline stroke
Nokogiri::XML::Node.new 'g', doc do |g|
svg.add_child g
g[:fill] = options[:stroke_color]
g[:mask] = 'url(#strokeMask)'
Nokogiri::XML::Node.new 'path', doc do |path|
g.add_child path
path[:d] = Knot.draw_outline grid
end
end
# Knot line
Nokogiri::XML::Node.new 'g', doc do |g|
svg.add_child g
g[:fill] = options[:knot_color]
Nokogiri::XML::Node.new 'path', doc do |path|
g.add_child path
path[:d] = Knot.draw_knot grid
end
end
debug_grid doc, svg, grid if options[:debug]
end
puts doc.to_xml indent:2