# RMagick and circle problem

**URL:** <https://www.ruby-forum.com/t/rmagick-and-circle-problem/129583>\
**Category:** Ruby\
**Created:** [February 19, 2008, 9:22pm UTC](https://www.ruby-forum.com/t/rmagick-and-circle-problem/129583 "2008-02-19T21:22:36Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![nephish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/8e7dd6/32.png) [@nephish](https://www.ruby-forum.com/u/nephish)\
**Post date:** [February 19, 2008, 9:22pm UTC](https://www.ruby-forum.com/t/rmagick-and-circle-problem/129583/1 "2008-02-19T21:22:36Z")

</div>

Hello all, i am back with this one.

my problem is that i am trying to draw a line on a circle that will  
indicate a certain angle.  
kinda like the hands on a clock. I have a line image that i rotate  
around and then superimpose upon the circle image that i draw.

The problem is that when the line image is rotated, it is shorter than  
the circle.

here is the code.  
#!/usr/bin/ruby

require ‘RMagick’

def gps\_image®

# fetches the image file

gps\_image = Magick::Image.read(‘line\_stop.png’).first  
gps\_image.background\_color = ‘none’

angle = 45  
gps\_image.rotate!(angle)

factor = (r.to\_i \* 2)  
gps\_image.crop\_resized!(factor, factor, Magick::CenterGravity)  
gps\_image.write(“site.site\_name.png”)

return gps\_image  
end

def circle\_image  
r = 45  
#start with a bare canvas (square 2 times the radius of circle) the  
extra two pixels is to not cut any of the border  
canvas = Magick::Image.new((2 \* r) +2, (2\*r) +2,  
Magick::HatchFill.new(‘transparent’, ‘transparent’))

# draw the circle

gc = Magick::Draw.new  
gc.stroke(‘red’)  
gc.stroke\_width(1)

# fill in the inside

color = ‘blue’  
gc.fill(color)  
gc.fill\_opacity(0.3)  
gc.circle(r, r, 0, r)

# put our circle on the canvas

image = gc.draw(canvas)

# place the line for gps circles

gps\_image = gps\_image®  
canvas.composite!(gps\_image, 0, 0, Magick::OverCompositeOp)

# write the image out as a file

canvas.write(“circle.png”)  
end

circle\_image()

Everything is ok if the image is 90, 180, 270, or 0 degrees, but the  
more i stray toward 45 degrees or so, the smaller the line comes out  
on the image.

the image used here ( line\_stop.png) is just a transparent image with  
a line drawn on it in the middle. ( my clock hand )

thanks for any tips.

shawn

---

<div class="post-metadata">

**Author:** ![nephish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/8e7dd6/32.png) [@nephish](https://www.ruby-forum.com/u/nephish)\
**Post date:** [February 20, 2008, 12:57am UTC](https://www.ruby-forum.com/t/rmagick-and-circle-problem/129583/2 "2008-02-20T00:57:08Z")

</div>

shawn bright wrote:

> here is the code.  
> angle = 45  
> r = 45  
> color = ‘blue’
> 
> the image used here ( line\_stop.png) is just a transparent image with  
> a line drawn on it in the middle. ( my clock hand )
> 
> thanks for any tips.
> 
> shawn

Looks like you’re expecting the image produced by rotate! to be the same  
size as the input image. The rotate! method doesn’t just rotate the  
line, it rotates the entire image. When you rotate the image by some  
angle that’s not a multiple of 90 degrees, the resulting image is going  
to be wider than the input image. The resize part of crop\_resized  
shrinks the image to fit.

Instead of rotating the clock hand and then compositing the resulting  
image over the circle image, it might be simpler to use Draw’s composite  
method to composite the clock hand over the circle with a suitable  
rotation. Since rotation always rotates around the origin, use translate  
to move the origin to the center of the circle. Here’s an example.

require ‘RMagick’

HAND\_WIDTH = 5

def draw\_hand(w)  
img = Magick::Image.new(w, HAND\_WIDTH) {self.background\_color =  
“none”}  
gc = Magick::Draw.new  
gc.stroke\_linecap “round”  
gc.stroke “blue”  
gc.stroke\_width HAND\_WIDTH  
gc.line 0, HAND\_WIDTH/2.0, w-(HAND\_WIDTH\*3)/4, HAND\_WIDTH/2.0  
gc.draw img  
img  
end

hand = draw\_hand(100)

def clock(side, hand, angle)  
img = Magick::Image.new(side+5, side+5)  
gc = Magick::Draw.new  
gc.stroke “red”  
gc.fill “none”  
gc.stroke\_width 5  
gc.circle side/2+2.5, side/2+2.5, side, side/2  
gc.translate((side+2)/2, (side+2)/2)  
gc.rotate angle  
gc.composite 0, 0, 0, 0, hand  
gc.draw img  
img  
end

clock = Magick::ImageList.new  
8.times do |n|  
clock \<\< clock(200, hand, n\*45)  
end  
clock.delay = 25  
clock.animate
