Skip to content

Instantly share code, notes, and snippets.

@takehiko
Created December 23, 2013 20:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save takehiko/8104172 to your computer and use it in GitHub Desktop.
Save takehiko/8104172 to your computer and use it in GitHub Desktop.
"Hub Calculation Board" Generator. See also: http://jsfiddle.net/takehikom/MmXpJ/ http://jsfiddle.net/takehikom/NsCAL/
#!/usr/bin/env ruby
# -*- coding: utf-8 -*-
# hubcalc.rb : "Hub Calculation Board" Generator
# by takehikom (http://d.hatena.ne.jp/takehikom/)
$svg_skel = DATA.read
module HubCalc
class Generator
def initialize(opt = Hash.new)
@center = opt[:center] || "5"
@outside = opt[:outside] || [0, 1, 2, 3, 4]
@filename = opt[:filename] || "hubcalc.svg"
end
def start
@code = ""
# draw spokes
n_spoke = @outside.length
raise if n_spoke == 0
n_spoke.times do |i|
angle = 2.0 * Math::PI * i / n_spoke
x = 200.0 + 150.0 * Math.sin(angle)
y = 200.0 - 150.0 * Math.cos(angle)
add_code('<line x1="200" y1="200" x2="%g" y2="%g"/>' % [x, y])
add_code('<circle cx="%g" cy="%g" r="40"/>' % [x, y])
add_code('<text x="%g" y="%g" class="smalltext">%s</text>' % [x, y + 17, @outside[i].to_s])
end
# draw center
add_code('<circle cx="200" cy="200" r="60"/>')
add_code('<text x="200" y="222" class="largetext">%s</text>' % @center.to_s)
# save as svg
svg = $svg_skel.dup
svg["<!--content-->"] = @code
open(@filename, "w") do |f_out|
f_out.print svg
end
end
private
def add_code(line, indent_level = 1)
@code += " " * indent_level
@code += line
@code += "\n"
end
end
end
if __FILE__ == $0
opt = { # [isbn:9784284202251], p.18
:filename => "hubcalc.svg",
:center => "5×",
:outside => "94728563".split(//)
}
HubCalc::Generator.new(opt).start
# HubCalc::Generator.new(:filename => "hubcalc2.svg").start
end
__END__
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg xmlns="http://www.w3.org/2000/svg" width="400" height="400" id="hubcalc">
<style type="text/css" >
<![CDATA[
#bg { fill:#e0ffe0; stroke:none }
circle { stroke:none; fill:#99ccff; }
line { stroke:#99ccff; stroke-width:4; }
text { font-family:sans-serif; font-style:normal; font-weight:normal; fill:#000000; stroke:none; text-anchor:middle; }
.largetext { font-size:65px; }
.smalltext { font-size:50px; }
]]>
</style>
<rect x="0" y="0" width="400" height="400" id="bg"/>
<!--content--></svg>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment