From: Bill Kelly Date: 2008-06-16T05:30:29+09:00 Subject: Re: [QUIZ][SOLUTION] Circle Drawing (#166) My solution follows.... Regards, Bill # Ruby Quiz #166 # # This draws a circle of the specified radius, modified # by an optional aspect ratio and thickness factor. # # implementation notes: # - for the fun of it, i forbade use of sqrt() and trancendentals # - the circle is not drawn into a buffer before being printed # - some values are empirically derived (thickness factor, in parciular) # # bugs: # - the thickness factor causes bloat in small circles ARGV.length >= 1 or abort("usage: #$0 radius [aspect] [thickness]") radius = ARGV.shift.to_f radius > 0 or abort("please provide radius of circle") aspect = ARGV.shift.to_f aspect > 0 or aspect = 1.0 thick = ARGV.shift.to_f thick > 0 or thick = 1.0 hradius = (radius * aspect).ceil + (thick/2.0).round vradius = radius.ceil + (thick/2.0).round def get_radius_ch(rsq, dsq, tfactor) (rsq - dsq).abs <= tfactor ? "*" : " " end tfactor = (thick * 4.0) + 2.5 rsq = radius**2 (-vradius).upto(vradius) do |y| (-hradius).upto(hradius) do |x| print(get_radius_ch(rsq, (x * (1.0/aspect))**2 + y**2, tfactor)) end puts end # example: radius 7.0, aspect 1.0, thickness 1.0 # # $ ruby 166_circle.rb 7 1 1 # # ***** # ** ** # * * # * * # * * # * * # * * # * * # * * # * * # * * # * * # * * # ** ** # ***** # # # example: radius 10.0, aspect 2.0, thickness 5.0 # # $ ruby 166_circle.rb 10 2 5 # # # ***** # ******************* # ************************* # ******** ******** # ******* ******* # ****** ****** # ***** ***** # ***** ***** # ***** ***** # **** **** # ***** ***** # ***** ***** # ***** ***** # **** **** # ***** ***** # ***** ***** # ***** ***** # ****** ****** # ******* ******* # ******** ******** # ************************* # ******************* # ***** #