Last active
June 19, 2018 17:34
-
-
Save takehiko/0c228e883ca1a00ef2d724f3f46659e8 to your computer and use it in GitHub Desktop.
Drawing "inome", a traditional Japanese design in the shape of a heart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# inome-drawer.rb : drawing "inome", a traditional Japanese design in the shape of a heart | |
# by takehikom | |
# See also: | |
# http://shoujuin.boo.jp/?page_id=466 | |
# http://d.hatena.ne.jp/takehikom/20180619/1529420399 | |
class InomeDrawer | |
def initialize(h = {}) | |
@unit = (h[:unit] || 52).to_f | |
@margin = (h[:margin] || 5).to_f | |
@image_filename = h[:filename] || "inome.png" | |
@background = h[:background] || "cyan" | |
# @background = h[:background] || "none" | |
@stroke = h[:foreground] || "red" | |
@strokewidth = (h[:strokewidth] || 2).to_f | |
@draw_type = h[:drawtype] | |
@omit_grid = h[:omitgrid] | |
init_length | |
end | |
attr_reader :unit, :margin | |
def start | |
case @draw_type | |
when :lineandcircle | |
create_inome_by_line_and_circle | |
when :path | |
create_inome_by_path | |
else | |
# create_inome_by_line_and_circle | |
create_inome_by_path | |
end | |
end | |
def init_length | |
@width = @unit * 7 + @margin * 2 | |
@height = @unit * 6 + @margin * 2 | |
@radius = @unit * 5 / 3 | |
@radius2 = @radius * Math.sqrt(2.0) * 0.5 | |
end | |
def unit=(u) | |
@unit = u | |
init_length | |
end | |
def margin=(m) | |
@margin = m | |
init_length | |
end | |
def create_inome_by_path | |
init_command | |
add_grid if !@omit_grid | |
@command += " -stroke #{@stroke} -strokewidth #{@strokewidth} -fill none" | |
@command += " -draw \"translate #{@margin},#{@margin} path \'#{inome_path}\'\"" | |
close_command | |
puts @command | |
system @command | |
end | |
def create_inome_by_line_and_circle | |
init_command | |
add_grid if !@omit_grid | |
# 左右の円 | |
@command += " -stroke #{@stroke} -strokewidth #{@strokewidth} -fill none" | |
x1 = @margin + @unit * 0.5 + @radius2 | |
y1 = @margin + @unit * 3 - @radius2 | |
x2, y2 = x1, y1 - @radius | |
@command += " -draw \"circle #{[x1, y1, x2, y2].join(',')}\"" | |
x1 = @width - @margin - @unit * 0.5 - @radius2 | |
x2 = x1 | |
@command += " -draw \"circle #{[x1, y1, x2, y2].join(',')}\"" | |
# 直線(下部) | |
x1, y1 = @margin + @unit * 3.5, @height - @margin | |
x2, y2 = x1 - @unit * 3.0, y1 - @unit * 3.0 | |
@command += " -draw \"line #{[x1, y1, x2, y2].join(',')}\"" | |
x2, y2 = x1 + @unit * 3.0, y1 - @unit * 3.0 | |
@command += " -draw \"line #{[x1, y1, x2, y2].join(',')}\"" | |
# 直線(上部) | |
x3 = @margin + @unit * 0.5 + @radius2 * 2 | |
y3 = @margin + @unit * 3 - @radius2 * 2 | |
x4 = @margin + @unit * 3.5 | |
y4 = y3 + (x4 - x3) | |
@command += " -draw \"line #{[x3, y3, x4, y4].join(',')}\"" | |
x3 = @width - (@margin + @unit * 0.5 + @radius2 * 2) | |
@command += " -draw \"line #{[x3, y3, x4, y4].join(',')}\"" | |
close_command | |
puts @command | |
system @command | |
end | |
def init_command | |
@command = "convert -size #{@width}x#{@height} xc:#{@background}" | |
end | |
def add_grid | |
# 格子(座標確認用) | |
@command += " -stroke blue -strokewidth 1 -fill none" | |
0.upto(6) do |i| | |
x1, y1 = @margin, @margin + @unit * i | |
x2, y2 = @width - @margin, y1 | |
@command += " -draw \"line #{[x1, y1, x2, y2].join(',')}\"" | |
end | |
0.upto(7) do |i| | |
x1, y1 = @margin + @unit * i, @margin | |
x2, y2 = x1, @height - @margin | |
@command += " -draw \"line #{[x1, y1, x2, y2].join(',')}\"" | |
end | |
end | |
def close_command | |
@command += " -quality 92 #{@image_filename}" | |
end | |
def inome_path | |
# 猪目の形状 | |
x1 = @unit * 3.5 | |
y1 = @unit * 6 | |
@path = "M #{x1},#{y1}" | |
x2 = x1 + @unit * 3 | |
y2 = y1 - @unit * 3 | |
@path += " L #{x2},#{y2}" | |
r3 = @radius2 | |
x3 = x2 - r3 * 2 | |
y3 = y2 - r3 * 2 | |
@path += " A #{r3},#{r3},0,0,0,#{x3},#{y3}" | |
x4 = x1 | |
y4 = y3 + (x3 - x4) | |
@path += " L #{x4},#{y4}" | |
x5 = x4 - (x3 - x4) | |
y5 = y3 | |
@path += " L #{x5},#{y5}" | |
x6 = x1 - @unit * 3 | |
y6 = y2 | |
@path += " A #{r3},#{r3},0,0,0,#{x6},#{y6}" | |
@path += " Z" | |
@path | |
end | |
end | |
if __FILE__ == $0 | |
InomeDrawer.new.start | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment