Click here to Skip to main content
14,303,603 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hello, Thank you for read my question. I'm appreciated with your concern.

I'm trying to drawing a text based circle without drawing header file or external class.
but include only "math.h" or any equivalent header/class like has "pi" value or "cosine/sine" function stuffs.

My goal is make a text based circle like next.

0000000000000000000000001
0000000000000000000001000
0000000000000000001000000
0000000000000010000000000
0000000000100000000000000
0000000010000000000000000
0000001000000000000000000
0000100000000000000000000
0100000000000000000000000
1000000000000000000000000

I need a circle curve equation code for C based programming language.
I tried to make a curve with using **"y=Cos(pi*x)"** but unfortunately it's not a plane circle curve.

Hence I'd like to know the circle curve equation example code in C based programming language.

Thank you!
Posted
Rate this:
Please Sign up or sign in to vote.

Solution 1

try playing with : X*X + Y*Y = const
which is the simple circle equation; naturally for any 0 < X < sqrt(const) you'll have 2 Y solutions, positive and negative, which is what you want
   
Rate this:
Please Sign up or sign in to vote.

Solution 2

What you probably mean is a parameterized circle equation: Let variable t run from 0 to 2*PI in small increments and then calculate:
double x, y;
x = cos (t) * r;
y = sin (t) * r;

This will give you a set of coordinated for a circle around the origin with radius r.

If you want to print the circle in the way you showed in your question then consider that the space taken up by each print character is different in x and y direction. Hence you have to compensate this by applying a correction factor in the x or y calculation. Otherwise the circle will appear as an ellipse on your chart.
   
Comments
Maciej Los 5-Aug-13 15:35pm
   
+5!
nv3 5-Aug-13 18:24pm
   
Thank you!
Rate this:
Please Sign up or sign in to vote.

Solution 3

If i understand you well, you need to create 2 dimension array of chars (filled with 0 - zero), then to replace some chars with other (1 - one) to imitate a circle... The size of array should be calculated based on r (radius).

Have a look here: http://cboard.cprogramming.com/c-programming/101213-drawing-circle-ascii-character.html[^], here: http://en.wikipedia.org/wiki/Midpoint_circle_algorithm[^] and here: http://code-heaven.blogspot.com/2009/05/c-program-for-bresenham-circle-drawing.html[^]

Simple replace SetPixel/DrawPixel function with your own to replace char in a 2D-array of chars ;)
   
Comments
CPallini 5-Aug-13 9:36am
   
5.
Maciej Los 5-Aug-13 15:34pm
   
Thank you, Carlo ;)
Rate this:
Please Sign up or sign in to vote.

Solution 4

try:
class Program
 {
   const int N = 20;
   static void Main()
   {
     char [, ] circle = new char[2*N+1, 2*N+1];
     double phi, dphi = 0.5 / N;

     phi = 0.0;

     for (int y = 0; y < 2*N+1; y++)
       for (int x = 0; x < 2*N+1; x++)
         circle[y, x] = '0';

     while (phi <= 2*Math.PI)
     {
       int x = (int) Math.Round( N + N * Math.Cos(phi));
       int y = (int)Math.Round(N + N * Math.Sin(phi));
       circle[y, x] = '1';
       phi += dphi;
     }

     for (int y = 0; y < 2*N+1; y++)
     {
       for (int x = 0; x < 2*N+1; x++)
         Console.Write(circle[y, x]);
       Console.Write('\n');
     }
   }
   
Comments
Maciej Los 5-Aug-13 8:20am
   
Good job, Carlo!
+5!
This is what i was trying to explain in few word ;)
CPallini 5-Aug-13 9:36am
   
Thank you, I followed your recipe. :-)
Maciej Los 5-Aug-13 15:34pm
   
;)
Naz_Firdouse 5-Aug-13 9:05am
   
+5
CPallini 5-Aug-13 9:36am
   
Thank you.
   
Thank you! It was so great helpful to me!
CPallini 6-Aug-13 8:38am
   
You are welcome.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100