Skip to content

Instantly share code, notes, and snippets.

@ikatake
Created December 30, 2017 05:55
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 ikatake/b2c45c8f2c8d0f3a9dbcc2f4f36502fc to your computer and use it in GitHub Desktop.
Save ikatake/b2c45c8f2c8d0f3a9dbcc2f4f36502fc to your computer and use it in GitHub Desktop.
rmagick annotate on arc
require 'rmagick'
iclude Math
def annotate_on_arc(img, str, wc, r, x0, y0, theta0, font, fill, stroke, size)
# img rmagick draw Macick::Image object
# str annotate string
# wc width of one charator[px]
# r, x0 ,y0 radius and center point[px] of circle(arc)
# theta0 angle at center of string[rad]
# font, size font familiy and font size[pt?]
# fill, stroke color of font fill and stroke
draw = Magick::Draw.new
l = wc * str.length # l is length of string[px]
angle_string = l / r # [rad]
angle_charactor = wc / r #[rad]
charr = str.split("")
ii = 0
for ch in charr do
theta_c0 = theta0 + angle_string * 0.5 - ii * angle_charactor
#theta_c0 is angle of charactor annotate angle that used in calc x, y
theta_cc = theta_c0 - 0.5 * angle_charactor
#theta_cc is angle of charactor center angle that used in rotation
x = x0 + r * cos(theta_c0)
y = y0 - r * sin(theta_c0)
ch = ch.gsub(/^@/, '\@')
draw.annotate(img, wc, wc, x, y, ch) do
self.font = font
self.fill = fill
self.stroke = stroke
self.pointsize = size
self.gravity = Magick::NorthWestGravity
self.rotation = -1.0 * (theta_cc * 180 / PI - 90)
end
ii += 1;
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment