Skip to content

Instantly share code, notes, and snippets.

@j1n3l0
Created June 4, 2010 14:47
Show Gist options
  • Save j1n3l0/425502 to your computer and use it in GitHub Desktop.
Save j1n3l0/425502 to your computer and use it in GitHub Desktop.
require "rubygems"
require "RMagick"
#
# draw an arrow at the point
#
def draw_arrow( image, point, direction="south" )
arrow = Magick::Draw.new
# make the value of "point" the center (origin)
arrow.translate( point.first, point.last )
# rotate based on the direction
case direction
when "north" then arrow.rotate( 0)
when "east" then arrow.rotate( 90)
when "south" then arrow.rotate(180)
when "west" then arrow.rotate(270)
else raise "Not a valid direction: #{direction}"
end
# calculate the arrow dimensions
tail_height = 0.100 * image.rows
arm_height = 0.050 * image.rows
arm_width = 0.025 * image.columns
# draw the arrow
# NOTE
# We are always drawing a south-facing arrow, the "direction"
# takes care of rotating it to point in the right ... direction
arrow.stroke( "black" )
arrow.stroke_width(2.5)
arrow.line( 0, tail_height, 0, 0 ) # line going down
arrow.line( arm_width, arm_height, 0, 0 ) # line from the right
arrow.line( -arm_width, arm_height, 0, 0 ) # line from the left
arrow.draw( image )
return image
end
[ "north", "east", "south", "west" ].each do |direction|
draw_arrow( Magick::Image.new(200,100), [100,50], direction ).write("./arrow/#{direction}.png")
end
@j1n3l0
Copy link
Author

j1n3l0 commented Jun 4, 2010

This is drawing a rather sensible looking arrow. The length of the arrows is fixed at the moment but at least the fundamental reason for writing this method (pointing the arrow in any -- one of four -- direction) is being accomplished.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment