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
#
#
# *****
# *******************
# *************************
# ******** ********
# ******* *******
# ****** ******
# ***** *****
# ***** *****
# ***** *****
# **** ****
# ***** *****
# ***** *****
# ***** *****
# **** ****
# ***** *****
# ***** *****
# ***** *****
# ****** ******
# ******* *******
# ******** ********
# *************************
# *******************
# *****
#



.



Relevant Pages

  • Re: Pi in non-euclidiean spaces
    ... different on curved surfaces. ... For a surface that isn't flat, this ratio will not be ... For example, on a sphere of radius R, for a circle of radius r ...
    (sci.math)
  • Re: "Strong typing vs. strong testing" [OT]
    ... the ratio of the circumference of a circle to twice its radius; ... the complex logarithm of -1 divided by the negative of the complex square ...
    (comp.lang.python)
  • Re: A newcomer here
    ... >>> With constant crust thickness and density, ... >>> increases as the radius increases. ... > if the shell thickness is constant, that ratio won't be. ... shell will have to get thinner for the same surface gravity at larger radii. ...
    (rec.arts.sf.composition)
  • Re: [QUIZ] Circle Drawing (#166)
    ... Given a radius, draw an ASCII circle. ... ruby circle.rb 7 ... Note that most fonts do not have a square aspect ratio, ...
    (comp.lang.ruby)
  • Re: Pi in non-euclidiean spaces
    ... Whereas pi is defined as the ratio of the circumference of a circle to ... ratio of lengths of a semicircle to radius .. ... rotation angle unit in a flat plane. ...
    (sci.math)