The Struggle For Drawing That 'Petty' Circle…

Drawing A Circle…seems easy..Isn’t it??
I thought the same, but I was proved wrong as I continued searching and thinking upon it. It wasn’t that simple as it appeared to be, may be, because we were not supposed to use the built-in functions that the Graphics library of C++ contain.
From the very day I got my task, it was as if some wizard has uttered a Magic Spell and generated some Attractive force between me & my laptop/mobile that made me stick to it, every minute & second.
I searched every corner of the web (wondering, if there exists some corner of the web :p) and made every bit of my mind work & finally came up with a result that didn’t look like a perfect circle but would work (as I thought :p )
The following is the code accompanied with the screenshot for the same….

//To illustrate circle
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
cout << "Enter the Radius r Of Circle With Equation x^2 + y^2 = r^2";
cout<< endl;
int r;
cin>>r;
for(int i =0; i<=2*r; i++)
{
for (int j=0; j<= 2*r; j++)
{
float d = sqrt((i-r)*(i-r)+(j-r)*(j-r));// d is the distance to the centre
if (d>r-0.5 && d< r +0.5) // comparing d with r
{
cout<< "*";
}
else
{
cout<< " ";
}
}
cout<< endl;
}
return 0;
}

Screenshot from 2014-03-17 21:49:24

The result looked like an ellipse along the vertical axis or a circle that is vertically elongated.
All four of us did our respective tasks, though not perfect, we still showed them to Sir. As expected he wasn’t much convinced. He gave his reviews, guided us and further gave us the task of correcting our programs( for me, the task,now, was to make the circle look like a ‘circle’ :p ). Further, we were loaded with an additional task of studying and understanding Bresenham’s algorithm. This wasn’t the end. We were given a graph paper ( which at first, seemed scary :p ) & were asked to plot a circle pixel-wise using the above said algorithm.
From there the struggle began again…..

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: