15,921,837 members
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

## 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

## 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:
C++
```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.

Maciej Los 5-Aug-13 15:35pm
+5!
nv3 5-Aug-13 18:24pm
Thank you!

## 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 ;)

CPallini 5-Aug-13 9:36am
5.
Maciej Los 5-Aug-13 15:34pm
Thank you, Carlo ;)

## Solution 4

try:
C#
```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');
}
}```

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.