Created
June 4, 2010 14:47
-
-
Save j1n3l0/425502 to your computer and use it in GitHub Desktop.
This file contains 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
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.