Attempt To Make It Round…..

This time my hands moved faster searching for the solution.While searching I encountered the words ‘Console Ratio’- The Ratio of console/terminal’s width and height. The c++ program or code for drawing a circle,as discussed previously, always ended up in displaying an Ellipse. Why did this happen?? Why does a circle always appear as an ellipse? This was because the console window’s entries aren’t square (i.e. one character occupies a non-square space).And, thus, when we try drawing a square using the following c++ code, it doesn’t appear to be a square but a rectangle.
Code for displaying a square (although we get a rectangle) :
for( int i=0; i<=10; i++)
{
for(int j=0; j<=10;j++)

{
cout<< ‘*’
}
cout<<endl;
}

In a similar way, when I tried drawing a circle, it appeared as an ellipse. So, this time I needed to introduce the value of this ratio into my program. Now, The question was- how to get the height and width of terminal window.
I studied further and found the following tput commands to find the approximate height and width of terminal window.
$tput cols
It gives the number of columns terminal can display i.e. the approx. width of the terminal window.
$tput lines
It gives the number of lines/rows terminal can display i.e. the approx. height of the terminal window.
These two commands resulted in the values 81 and 24 when used in the terminal(in std. size of terminal, not maximized).

Using this ratio, Following was the new program I wrote with its output shown in the screenshot

// To illustrate a circle
#include<iostream>
using namespace std;

int main()
{
int circle_radius;
cout<< ” Enter the Radius”;
cin>> circle_radius;
float pr = 81/24; // pr is the aspected pixel ratio which is almost equal to 2
float a = circle_radius; // if a=b in an ellipse then the equation of ellipse becomes equation of a circle
float b = circle_radius;

for (int y = -circle_radius; y <= circle_radius; y++)
{
for (int x = -circle_radius*pr; x <= circle_radius*pr; x++)
{
float d = (x/(pr*a))*(x/(pr*a)) + (y/b)*(y/b); // equation of circle, as a=b…the x coordinate is divided by 2 i.e the aspected ratio
if (d >0.97 && d<1.03) // approximation
{
cout << “*”;
}
else
{
cout << ” “;
}
}
cout << endl;
}
return 0;
}

 

Image

 

Err…Is this a circle?? Ummm..No..I guess or May be, Yes, we can call it a circle that is horizontally elongated :p In an attempt to make it round, I made it ‘Extra’ Round :p What progress I did as I made this was that, It looked a little better than The previous output(discussed in previous blog) although this can be entitled to A JOURNEY FROM A VERTICALLY ELONGATED TO A HORIZONTALLY ELONGATED CIRCLE :p
I decided to show this to Sir. Again, each one of us, with our not-so-perfect codes/programs (though better, this time) went for the presentation(about our tasks) we were required to give to all the members.
Disappointment Again ! 🙁 As expected, Sir wasn’t much convinced again. The only thing that made me smile that day was the fact that I was appreciated For adding those tput commands(that were new to them) into the knowledge-repositories of all members. Coming back to our task, Sir gave us 7 more days to correct our programs. Mind it, this time he wanted ‘correct’ programs..no slight improvements..no better result..but the actual result, as it should be. The spotlight, Now, Shifted to that ‘petty’ CIRCLE. The other three ‘jealous’ curves didn’t get that attention that this petty circle took away :p and we all were now supposed to work on this only. In addition to this, we were also asked to implement Bresenhem’s Algorithm using C++…and…we were set for our ‘Entry-to-GD’ MISSION, again.

Leave a Reply

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

%d bloggers like this: