[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: [QUIZ] Circle Drawing (#166

David and Sharon Phillips

6/15/2008 10:43:00 PM

Quick solution. Draws filled circles, corrected for non-square pixels. Can =
take non-integer x,y and radius.=0A=0AHere http://pastie.org/2... be=
low (not sure how long pastie keeps the content)=0A=0ACheers,=0ADave=0A=0A#=
http://pastie.org/... =0A# Ruby Quiz 166 Circles=0A# David Phillips=
=0A#=0A# sample output=0A# =0A# 0000000000000--|||||||||||||||++++++ =
!!!!!!!!!!!!!!!!!!!!!!!!!=0A# 00000000000-----|||||||||||||++++++++ =
!!!!!!!!!!!!!!!!!!!!!!!=0A# 0000000000------|||||||||||||+++++++++ =
\ !!!!!!!!!!!!!!!!!!!!!!!=0A# 000000000--------|||||||||||++++++\\\\=
\\\\\\\\\!!!!!!!!!!!!!!!!!!!!!!!=0A# 000000000----------|||||||++++++\\\\\=
\\\\\\\\\\\\!!!!!!!!!!!!!!!!!!!!=0A# 00000000-------------------++++\\\\\\=
\\\\\\\\\\\\\!!!!!!!!!!!!!!!!!=0A# 000000000-----------------++++\\\\\\\\\=
\\\\\\\\\\\\!!!!!!!!!!!!!!!=0A# 000000-----------------+++\\\\\\\\\\\\\=
\\\\\\\\\\ !!!!!!!!!=0A# 0 -----------------+++\\\\\\\\\\\\\\\\\\\=
\\\\=3D=3D=3D=3D=3D !=0A# ------------- \\\\\\\\\\\\\\\\\\\\=
\\\\\=3D=3D=3D=3D=3D=3D=3D=0A# XXXX--------- \\\\\\\\\\\\\\=
\\\\\\\\\=3D=3D=3D=3D=3D=3D=3D=3D=3D=0A# XXXXXXXXXX- \\\\=
\\\\\\\\\\\\\\\\\\\=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=0A# XXXXXXXXXXX=
XX ////\\\\\\\\\\\\\\\\\\\\\\\=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D*=0A# =
XXXXXXXXXXXXX ////////\\\\\\\\\\\\\\\\\\\\\=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D*=0A# XXXXXXXXXXXXXXX //////////\\\\\\\\\\\\\\\\\\\=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D**=0A# XXXXXXXXXXXXX ////////=
/////\\\\\\\\\\\\\\\=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D*=0A=
# XXXXXXXXXXXXX //////////////\\\\\\\\\\\\\=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D**=0A# XXXXXXXXX /////////////////// =
\=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D***=
=0A# XXXXXXX ///////////////////// =3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D**=0A# /////=
////////////// *=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D***=0A# /////////////////// ***=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D*****=0A# ///////=
////////// ***=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D*****=0A# =
/////////////// *****=3D=3D=3D=3D=3D=3D=3D=3D=3D**=
*****=0A# ///////////// ********=3D*********=
*=0A# / *************=0A# =
*=0A=0Aclass Canvas=0A =
def initialize aspect_ratio=3D1.6=0A @rows=3D []=0A @y_multiplier=3D =
1/aspect_ratio=0A yield self if block_given?=0A end=0A =0A def set_pi=
xel x, y, char=0A x, y =3D x.round, (@y_multiplier*y).round=0A (@rows=
[y]||=3D[])[x]=3Dchar unless (x<0 || y<0)=0A end=0A =0A def to_s=0A @=
rows.map do |row|=0A row.nil? ? '' : row.map{|c|c||=3D' '}.join=0A =
end.join("\n")=0A end=0A =0A # decided to try a method that doesn't requ=
ire trig functions=0A def draw_circle x, y, r, fill_char=3D'#'=0A (x-r)=
round.upto((x+r).round) do |col|=0A (y-r).round.upto(y+r) do |row|=0A=
if (col-x)**2+(row-y)**2 <=3D r**2=0A set_pixel col, row, =
fill_char=0A end=0A end # row=0A end # col=0A end # def=0Ae=
nd=0A=0A=0Acanvas=3D Canvas.new do |c|=0A %w[! . + * X 0 =3D / \\ - |].eac=
h do |char|=0A c.draw_circle rand(70), rand(30), 4+rand(10), char=0A en=
d=0Aend=0Aputs canvas =0A=0A=0A ______________________________________=
____________________=0ASent from Yahoo! Mail.=0AA Smarter Email ht...
ocs.yahoo.com/nowyoucan.html