Hello all,
on an assignment my professor has asked us to "paint" a few different shapes on a canvas. (square, rectangle, circle, ellipse)
to keep it simple,
I had no problem breezing through the square and rectangle.
to help you understand, here's a bit of code from one of the shapes:

for (leftBoundColumn_c=leftBoundColumn; leftBoundColumn_c <= (leftBoundColumn+bound_Width_Height)-1; leftBoundColumn_c++)
    for (leftBoundRow_c=leftBoundRow; leftBoundRow_c <= (leftBoundRow +bound_Width_Height)-1; leftBoundRow_c++)
        bigCanvas.getDot (leftBoundColumn_c, leftBoundRow_c).setRedLevel (amountOfRed);
        bigCanvas.getDot (leftBoundColumn_c, leftBoundRow_c).setBlueLevel (amountOfBlue);
        bigCanvas.getDot (leftBoundColumn_c, leftBoundRow_c).setGreenLevel (amountOfGreen);


(leftBoundColumn and leftBoundRow identify the upper left hand point in the shapes, bound_Width_Height is the user's desired height and width of the square. )

I'm having trouble drawing both the ellipse and circle.
i can't conceptually grasp the idea behind using nested for loops to "paint" out these shapes, although this is what my professor demands. (aka no vectors/arrays)
I know (x-a)^2+(y-b)^2=r^2 must fit in there somewhere, but I've no earthly idea where. think someone can point me in the right direction?
thanks ahead of time.

7 Years
Discussion Span
Last Post by firstPerson

Can you use polar coordinate, or this equation of the circle ?

for theta = 0 to theta < 360 degrees; ++theta{
x = cos(theta) * radius
y = sin(theta) * radius
//center* is where the circle should be centered at
drawAndFillPoint(x - centerX , y - centerY)

at firstPerson,
thank you very much for your quick response!
however my professor may be looking for something a bit more... basic. I'm sure your code is spot on, but polar coordinates may spark unwanted attention to my program, as my professor doesn't expect us (to be sharp enough) to consider such techniques.
Sadly, my arsenal is VERY limited: I can (essentially) only use FOR loops and basic functions... I suppose i can use any equation of a circle that i want, but again PCS may be a bit too far out of place.
This must be aggravating, making such an easy job so complex, but again thank you very much!


All right if you must.

You have the equation of the circle : (x-a)^2+(y-b)^2=r^2

Lets say its centered at the origin for now, so the equation now
becomes x^2 + y^2 = r^2.

Lets say is a unit circle, so the equation becomes :

x^2 + y^2 = 1

Now to be easier lets do this in a two step process.

since x^2 + y^2 = 1


y^2 = 1 - x^2


y = plus/minus sqrt( 1 - x^2 )

The plus/minus determine whether the circle is the top of the bottom half.

So the top half of the circle is y = + sqrt(1 - x^2) and the bottom half
of the circle is y = - sqrt(1 - x^2)

So our 2 step process is to draw the top half first and then the bottom
half. And you can just make "1-x^2" , "r - x^2" where r is the radius.
Now can you convert the 2 function into 2 different 1 for loop algorithm?

Edited by firstPerson: n/a


actually, my issue is in converting +/-sqrt(r-x^2) into compilable for loops...
even still, you've helped wonders.


I think a circle is tough (I'm not totally familiar with the graphics lib you are using) but probably doable with putting one (dot, star etc) on the top line, on the next putting two spaced 1 away from the center dot on either side and one down,then putting 2 spaced equally apart on the next line but on the same level (since if you keep the "slope" equal to 1 you'll have a diamond.

*  * *
                                * *           * *
                             *                         *

It looks like the devil on this scale (ain't discretization grand!!) but for many dots it might look ok. And you can control the painting with something like

for (over the lines of the figure)
   { coord = center - linenumber 
      coord = center + linenumber   
      if(preset condition, say even or odd lines)

You could get the slope of the line as a function of where you are (from firstPerson's post and some elementary calculus).
I know you wanted an actual loop but give it a try yourself and be creative. The ellipse would just be the circle with a different slope.

Edited by jonsca: n/a


I don't know if you need precision with floats
but I will use floats, but you can easily just change its type if needed.

For y = -sqrt( r - x^2) you can do this :

const unsigned short R = 5;
//our boundaries of our graph
const unsigned int MIN = 0;
const unsigned int MAX = 10;
float y = 0.0f;
float x = 0.0f;
const float STEP = 0.1f;
//warninig : floats are slow in a loop,
//but for now we don't care about speed, right?
for(x = MAX;  x < MAX; x+= STEP){
y = - sqrt( R - x*x );

Assuming your plotGraph plots points, that should plot the bottom
half of the circle. Now you can either do the same this in another for loop for the top circle or make new variables and do it inside that loop.

This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.