From: Sandro Paganotti Date: 2008-06-17T18:09:09+09:00 Subject: Re: [QUIZ] Circle Drawing (#166) ------=_Part_15674_31724723.1213693810398 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline Here's mine # initial values r = ARGV[0].to_i; k = 360.0/(Math::PI*2) # if r < 1 write error and exit puts "USAGE: circle.rb radius [with radius >= 1]"; exit if r < 1 # get some points and map them on a matrix m = Array.new(r).collect{|e|Array.new(r).fill(" ")} j = [0.0,90.0,*(1...p=((vr=(r-1))*2)).to_a.collect{|e| e*(90.0/p.to_f) }].each{|a| vl=[Math.sin(a/k),Math.cos(a/k)].collect{|c| (c*vr).round } m[vl[0]][vl[1]]="#" } s = m.collect{|a| w=a.join(""); w.reverse+w }.join("\n") # print the result puts s.reverse+"\n"+s On Mon, Jun 16, 2008 at 3:54 PM, Jon Garvin wrote: > Here's mine before I delved into trying to add the aspect ratio feature. > > > class Circle > def initialize(radius) > @radius = radius.to_i > end > > def draw > (0..@radius*2).each do |x| > (0..@radius*2).each do |y| > print distance_from_center(x,y).round == @radius ? '#' : '.' > end > puts > end > end > > def distance_from_center(x,y) > a = calc_side(x) > b = calc_side(y) > return Math.sqrt(a**2 + b**2) > end > > def calc_side(z) > z < @radius ? (@radius - z) : (z - @radius) > end > end > > Circle.new(ARGV.shift).draw > > -- > > http://www.5valleys.com/ > > http://www.workingwithrails.com/person/8078 > > > -- Go outside! The graphics are amazing! ------=_Part_15674_31724723.1213693810398--