Class: Astromapper::Svg
- Inherits:
-
Object
- Object
- Astromapper::Svg
- Defined in:
- lib/astromapper/svg.rb
Instance Method Summary collapse
- #build_routes ⇒ Object
- #center_of(locx) ⇒ Object
- #config ⇒ Object
- #consulate(c) ⇒ Object
- #convert ⇒ Object
- #draw_belt(c) ⇒ Object
- #draw_planet(c, w) ⇒ Object
- #footer ⇒ Object
- #frame(k = 'Frame') ⇒ Object
- #from_file ⇒ Object
- #gas_giant(c) ⇒ Object
- #header ⇒ Object
- #hex_grid ⇒ Object
- #hex_row(row, top = false) ⇒ Object
-
#initialize(filename) ⇒ Svg
constructor
A new instance of Svg.
- #namestamp ⇒ Object
- #navy_base(c) ⇒ Object
- #pirates(c) ⇒ Object
- #polygon(x, y, sx, sy, sides = 4) ⇒ Object
- #scout_base(c) ⇒ Object
- #star_coords(r1, r2, points) ⇒ Object
- #stars(c, stars) ⇒ Object
- #tract_marks ⇒ Object
- #volumes ⇒ Object
- #world(volume) ⇒ Object
Constructor Details
#initialize(filename) ⇒ Svg
Returns a new instance of Svg.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/astromapper/svg.rb', line 6 def initialize(filename) @rows = 40 @columns = 32 @source_filename = filename sectorname = filename.gsub(File.extname(filename), '') @svg_filename = sectorname + '.svg' @side = 40 @factor = 1.732 @height = (@side * @factor * (@rows + 0.5)).ceil @width = (@side * (@columns * 1.5 + 0.5)).ceil @mark = 13 @zones = [] @volumes = [] # @routes = {} @routes = [] @name = config['name'] @hex = { :side_h => (@side * (@factor / 2)).tweak, :side_w => (@side / 2).tweak, :width => @side } end |
Instance Method Details
#build_routes ⇒ Object
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 |
# File 'lib/astromapper/svg.rb', line 32 def build_routes routes = [] keys = @volumes.map do |v| v[0..3] end keys.each do |k| x = k[0..1].to_i y = k[2..3].to_i # @routes[k] = [] if @routes[k].nil? (-4..4).to_a.each do |n| (-4..4).to_a.each do |m| x1 = (x + m) y1 = (y + n) d = Math.sqrt((x - x1)**2 + (y - y1)**2).floor next if y1 > 40 next if x1 > 32 next if d == 0 c = "%02d%02d" % [x1, y1] # if (keys.include?(c) and !@routes[c].include?(k)) if (keys.include?(c) and !@routes.include?("#{k}#{c}")) a = center_of(k) b = center_of(c) routes << "<!-- #{k} > #{c} --><line class='line#{d}' x1='#{a[0]}' y1='#{a[1]}' x2='#{b[0]}' y2='#{b[1]}' />" # @routes[k] << c # @routes[c] = [k] if @routes[c].nil? @routes << "#{c}#{k}" # reverse look-up. end end end end routes.join("\n") end |
#center_of(locx) ⇒ Object
168 169 170 171 172 173 174 |
# File 'lib/astromapper/svg.rb', line 168 def center_of(locx) column = locx[0..1].to_i row = locx[2..3].to_i x = @side + ((column - 1) * @side * 1.5) # 40 + ((COL - 1) * 60 ) y = (row - 1) * @side * @factor + (@side * @factor / (1 + (column % 2))) return [x.tweak,y.tweak] end |
#config ⇒ Object
3 4 5 |
# File 'lib/astromapper/svg.rb', line 3 def config Astromapper.config end |
#consulate(c) ⇒ Object
300 301 302 |
# File 'lib/astromapper/svg.rb', line 300 def consulate(c); return "<!-- Consulate --><text class='symbol' x='#{(c[0]-(@side/1.5)).to_i}' y='#{(c[1]+(@side/7)).to_i}'>\u2691</text>\n" end |
#convert ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/astromapper/svg.rb', line 64 def convert from_file svg = [] svg << header svg << tract_marks svg << hex_grid svg << build_routes svg << @volumes.map {|v| world(v) } svg << volumes svg << frame svg << File.open(@svg_filename,'w').write(svg.flatten.join("\n")) end |
#draw_belt(c) ⇒ Object
239 240 241 242 243 244 245 246 247 |
# File 'lib/astromapper/svg.rb', line 239 def draw_belt(c) output = " <g class='belt'>\n" 7.times do x = c[0] + Random.rand(@side/3) - @side/6 y = c[1] + Random.rand(@side/3) - @side/6 output += " <circle class='belt' cx='#{x.to_i}' cy='#{y.to_i}' r='#{(@side/15).tweak}' />\n" end output + " </g>\n" end |
#draw_planet(c, w) ⇒ Object
235 236 237 238 |
# File 'lib/astromapper/svg.rb', line 235 def draw_planet(c,w) k = (w[3] == '0') ? 'Desert' : 'Planet' " <circle class='planet' cx='#{c[0].to_i}' cy='#{c[1].to_i}' r='#{(@side/7).to_i}' />\n" end |
#footer ⇒ Object
78 79 80 |
# File 'lib/astromapper/svg.rb', line 78 def return "</svg>" end |
#frame(k = 'Frame') ⇒ Object
248 249 250 251 252 |
# File 'lib/astromapper/svg.rb', line 248 def frame(k='Frame') style = k.to_sym z = 0; w = @width.to_i - 0; h = @height.to_i - z; " <polyline class='frame' points='#{z},#{z} #{w},#{z} #{w},#{h} #{z},#{h} #{z},#{z}' />" end |
#from_file ⇒ Object
29 30 31 |
# File 'lib/astromapper/svg.rb', line 29 def from_file File.open(@source_filename,'r').readlines.each { |line| @volumes << line if /^\d{4}/.match(line) } end |
#gas_giant(c) ⇒ Object
288 289 290 291 292 293 294 295 296 |
# File 'lib/astromapper/svg.rb', line 288 def gas_giant(c) x = (c[0]+(@side/1.8)).tweak; y = (c[1]+(@side/3)).tweak; return" <g class='gas-giant'><!-- Has Gas Giant -->\n <ellipse cx='\#{x.to_i}' cy='\#{y.to_i}' rx='\#{(@side/(@mark * 0.5)).to_i}' ry='\#{(@side/@mark * 0.3).tweak}' />\n <circle cx='\#{x.to_i}' cy='\#{y.to_i}' r='\#{(@side/(@mark * 1.2)).to_i}' />\n </g>\n GIANT\nend\n" |
#header ⇒ Object
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 |
# File 'lib/astromapper/svg.rb', line 81 def header return"<?xml version=\"1.0\" standalone=\"no\"?>\n <!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n\"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n<svg width=\"\#{@width}px\" height=\"\#{@height}px\" version=\"1.1\" xmlns=\"http://www.w3.org/2000/svg\">\n <desc>Subsector Map Grid</desc>\n <style>\nsvg {\n fill: #FFF;\n}\ntext {\n text-anchor: middle;\n fill: #567;\n font: 8px sans-serif;\n}\ntext.tract {\n text-anchor: left;\n fill: #eed;\n font: 120px sans-serif;\n}\ntext.namestamp {\n font-size: 13px;\n}\ntext.symbol {\n font-size: 14px;\n fill: #222;\n}\ng.volumes text {\n fill: #DDD;\n}\nline {\n opacity: 0.5;\n}\nline.line1 {\n stroke: #666;\n stroke-width:4;\n}\nline.line2 {\n stroke: #66C;\n stroke-width:3;\n}\nline.line3 {\n stroke: #C60;\n stroke-width:2;\n xstroke-dasharray: 5, 5, 1, 5;\n display:none;\n}\nline.line4 {\n stroke: #C00;\n stroke-width:1;\n stroke-dasharray: 2,6;\n stroke-linecap: round;\n display:none;\n}\npolyline {\n fill: none;\n stroke: #DDD;\n stroke-width: 1;\n}\ng.gas-giant {\n stroke: #034;\n}\ng.gas-giant circle {\n fill: #034;\n stroke: #034;\n stroke-width:1;\n}\ng.gas-giant ellipse {\n stroke: #034;\n stroke-width: 1;\n}\ncircle {\n fill: #222;\n stroke: #fff;\n stroke-width: 1;\n}\n.zone {\n fill: none;\n stroke: #B90;\n stroke-width: 3;\n stroke-dasharray: 3,6;\n}\n </style>\n <rect width='\#{@width}' height='\#{@height}' />\n EOS\nend\n" |
#hex_grid ⇒ Object
311 312 |
# File 'lib/astromapper/svg.rb', line 311 def hex_grid; (@rows * 3 + 2).times.map { |j| hex_row((j/2).floor, (j % 2 != 0)) }; end |
#hex_row(row, top = false) ⇒ Object
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 |
# File 'lib/astromapper/svg.rb', line 316 def hex_row(row, top=false) ly = (row * 2 * @hex[:side_h]) + @hex[:side_h] points = [] x = 0; y = 0 (@columns/2).ceil.times do |j| x = j * @side * 3 y = ly points << "#{x.to_i},#{y.to_i}" x += @hex[:side_w] y = (top) ? y - @hex[:side_h] : y + @hex[:side_h] points << "#{x.to_i},#{y.to_i}" x += @hex[:width] points << "#{x.to_i},#{y.to_i}" x += @hex[:side_w] y = (top) ? y + @hex[:side_h] : y - @hex[:side_h] points << "#{x.to_i},#{y.to_i}" x += @hex[:width] points << "#{x.to_i},#{y.to_i}" end x += @hex[:side_w] y = (top) ? y - @hex[:side_h] : y + @hex[:side_h] points << "#{x.to_i},#{y.to_i}" " <polyline points='#{points.join(' ')}' />" end |
#namestamp ⇒ Object
313 314 315 |
# File 'lib/astromapper/svg.rb', line 313 def namestamp return "<text class='namestamp' x='30' y='2800'>#{@name}</text>" end |
#navy_base(c) ⇒ Object
307 308 309 310 |
# File 'lib/astromapper/svg.rb', line 307 def navy_base(c); return "<!-- Navy Base --><text class='symbol' x='#{c[0]-(@side/1.8)}' y='#{c[1]-(@side/6)}'>\u2693</text>\n" '<!-- NB -->' +polygon(c[0]-(@side/1.8), c[1]-(@side/3.7), @side/(@mark/2), @side/@mark, 5); end |
#pirates(c) ⇒ Object
297 298 299 |
# File 'lib/astromapper/svg.rb', line 297 def pirates(c); return "<!-- Pirates --><text class='symbol' x='#{(c[0]-(@side/3.1)).to_i}' y='#{(c[1]+(@side/7)).to_i}'>\u2620</text>\n" end |
#polygon(x, y, sx, sy, sides = 4) ⇒ Object
284 285 286 287 |
# File 'lib/astromapper/svg.rb', line 284 def polygon(x, y, sx, sy, sides=4) polygon = star_coords(sx, sy, sides).map { |c| "#{(x + c[0]).to_i},#{(y.tweak+c[1]).to_i}" } " <polygon points='#{polygon.join(' ')}' />\n" end |
#scout_base(c) ⇒ Object
303 304 305 306 |
# File 'lib/astromapper/svg.rb', line 303 def scout_base(c); return "<!-- Scout Base --><text class='symbol' x='#{(c[0]-(@side/1.8)).to_i}' y='#{(c[1]+(@side/2.4)).to_i}'>\u269C</text>\n" '<!-- SB -->' + polygon(c[0]-(@side/1.8),c[1]+(@side/3.7), @side/(@mark/2), @side/@mark, 3); end |
#star_coords(r1, r2, points) ⇒ Object
175 176 177 178 179 180 181 182 183 184 185 |
# File 'lib/astromapper/svg.rb', line 175 def star_coords(r1,r2,points) pangle = 2*@@pi/points sangle = @@pi/points oangle = @@pi/-2 coords = [] points.times do |j| coords << [r1 * Math::cos(pangle * j + oangle), r1 * Math::sin(pangle * j + oangle)] coords << [r2 * Math::cos((pangle * j) + sangle + oangle), r2 * Math::sin((pangle * j) + sangle + oangle)] end return coords end |
#stars(c, stars) ⇒ Object
224 225 226 227 228 229 230 231 232 233 234 |
# File 'lib/astromapper/svg.rb', line 224 def stars(c,stars) output = '' x = (c[0]+(@side/1.8)).tweak + 2 y = (c[1]-(@side/3)).tweak + 3 stars.split('/').each do |star| output += " <text x='#{x.to_i}' y='#{y.to_i}'>#{star[0..1].strip}</text>\n" x += 3 y += 7 end output end |
#tract_marks ⇒ Object
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 |
# File 'lib/astromapper/svg.rb', line 253 def tract_marks height = (@height / 4).floor width = (@width / 4).ceil # width -= 2 output = '' letters = ('A'..'P').to_a 5.times do |r| h1 = ((height.floor * r) - (8*r)).to_i; h2 = (h1 + height - 8).to_i w2 = 0 4.times do |c| w1 = w2.to_i; w2 += (width - [-4,4,5,-4][c]).to_i output += " <text class='tract' x='#{w1 + 70}' y='#{h1 + 110}'>#{letters.shift}</text>\n" output += " <polyline class='tract' points='#{w1},#{h1} #{w2},#{h1} #{w2},#{h2} #{w1},#{h2} #{w1},#{h1}' />\n" # raise output end end output += namestamp return output end |
#volumes ⇒ Object
273 274 275 276 277 278 279 280 281 282 283 |
# File 'lib/astromapper/svg.rb', line 273 def volumes output = "<g class='volumes'>" (@rows+2).times do |r| (@columns+1).times do |c| x = @side + ((c-1) * @side * 1.5) y = (c % 2 == 1) ? (r-1) * @side * @factor + (0.2 * @side) : (r-1) * @side * @factor + @hex[:side_h]+ (0.2 * @side) output += "<text x='#{x.to_i}' y='#{y.to_i}'>%02d%02d</text>\n" % [c,r] end end output += "</g>" end |
#world(volume) ⇒ Object
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 |
# File 'lib/astromapper/svg.rb', line 186 def world(volume) # TAB 0 - World Details # 0. Location # 1. UWP # 2. Temp # 3. NSG (Features) # 4. Travel Zone # TAB 1 - Trade Codes # TAB 2 - Factions # TAB 3 - Name #1101 A505223-B ..G.. »·IC,Lo,Va »N,O,N »·G0V »Omivarium details, trades, factions, star, name = volume.split(/\t/) locx, uwp, temp, nsg, zone = details.split(/\s+/) spaceport = uwp[0] size = uwp[1] c = center_of(locx) # get Location's x,y Coordinates curve = @side / 2 output = "<!-- Volume: #{volume.strip.gsub(/\t/,' // ')} -->\n" output += (size == '0') ? draw_belt(c) : draw_planet(c,uwp) output += " <text class='spaceport' x='#{c[0].to_i}' y='#{(c[1] + @side / 2).to_i}'>#{spaceport.strip}</text>\n" output += " <text x='#{c[0].to_i}' y='#{(c[1]+(@side/1.3)).to_i}'>#{uwp.strip}</text>\n" output += " <text x='#{c[0].to_i}' y='#{(c[1]-(@side/2.1)).to_i}'>#{name.strip}</text>\n" unless zone == '..' style = zone + '_zone' output += " <path class='zone' d='M #{(c[0] - curve/2).to_i} #{(c[1] - (curve/1.4)).to_i} a #{curve} #{curve} 0 1 0 20 0' />\n" end output += navy_base(c) if nsg.include?('N') output += scout_base(c) if nsg.include?('S') output += gas_giant(c) if nsg.include?('G') output += consulate(c) if nsg.include?('C') output += pirates(c) if nsg.include?('P') output += stars(c,star) output end |