# Re: Circle Drawing (#166)

**URL:** <https://www.ruby-forum.com/t/re-circle-drawing-166/140541>\
**Category:** Ruby\
**Created:** [June 16, 2008, 12:44am UTC](https://www.ruby-forum.com/t/re-circle-drawing-166/140541 "2008-06-16T00:44:27Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![dpitman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a9adbd/32.png) [@dpitman](https://www.ruby-forum.com/u/dpitman)\
**Post date:** [June 16, 2008, 12:44am UTC](https://www.ruby-forum.com/t/re-circle-drawing-166/140541/1 "2008-06-16T00:44:27Z")

</div>

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

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

Cheers,  
Dave

# [http://pastie.org/214895](http://pastie.org/214895)

# 

# Ruby Q. 166 Circles

# David P.

# 

# sample output

# 

# 0000000000000–|||||||||||||||++++++

!!!

# 00000000000-----|||||||||||||++++++++

!!!

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

!!!

# 

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

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

# 00000000-------------------++++\\\\\\\\\!!!!

# 000000000-----------------++++\\\\\\\\\\!!!!

# 000000-----------------+++\\\\\\\\\\\\ !!!

# 0 -----------------+++\\\\\\\\\\\===== !

# ------------- \\\\\\\\\\\\=======

# XXXX--------- \\\\\\\\\\\=========

# XXXXXXXXXX- \\\\\\\\\\\===========

# XXXXXXXXXXXXX ////\\\\\\\\\\\===========\*

# XXXXXXXXXXXXX ////////\\\\\\\\\\=============\*

# XXXXXXXXXXXXXXX //////////\\\\\\\\\==============\*\*

# XXXXXXXXXXXXX /////////////\\\\\\\=================\*

# XXXXXXXXXXXXX //////////////\\\\\\=================\*\*

# XXXXXXXXX /////////////////// =======================\*\*\*

# XXXXXXX ///////////////////// =======================\*\*

# /////////////////// _=====================_\*\*

# /////////////////// _ **=================** _\*\*

# ///////////////// _ **===============** _\*\*

# /////////////// _ **=========** _\*\*

# ///////////// **=** \*\*

# / \*\*\*\*\*\*\*\*\*\*\*\*\*

# \*

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 \<= r** 2  
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](http://uk.docs.yahoo.com/nowyoucan.html)
