# Re: Circle Drawing (#166)

Quick solution. Draws filled circles, corrected for non-square pixels.
Can take non-integer x,y and radius.

Here http://pastie.org/214895
or below (not sure how long pastie keeps the content)

Cheers,
Dave

!!!

!!!

# 0000000000------|||||||||||||+++++++++ \

!!!

000000000--------|||||||||||++++++\\\\\\!!!!

# *

class Canvas
def initialize aspect_ratio=1.6
@rows= []
@y_multiplier= 1/aspect_ratio
yield self if block_given?
end

def set_pixel x, y, char
x, y = x.round, (@y_multiplier*y).round
(@rows[y]||=[])[x]=char unless (x<0 || y<0)
end

def to_s
@rows.map do |row|
row.nil? ? ‘’ : row.map{|c|c||=’ '}.join
end.join(“\n”)
end

# decided to try a method that doesn’t require trig functions

def draw_circle x, y, r, fill_char=‘#’
(x-r).round.upto((x+r).round) do |col|
(y-r).round.upto(y+r) do |row|
if (col-x)**2+(row-y)2 <= r2
set_pixel col, row, fill_char
end
end # row
end # col
end # def
end

canvas= Canvas.new do |c|
%w[! . + * X 0 = / \ - |].each do |char|
c.draw_circle rand(70), rand(30), 4+rand(10), char
end
end
puts canvas

``````  __________________________________________________________
``````

Sent from Yahoo! Mail.
A Smarter Email http://uk.docs.yahoo.com/nowyoucan.html