# Circle in C programming

Page 1 of 1

## 5 Replies - 273 Views - Last Post: 14 October 2019 - 07:44 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=417551&amp;s=9fd306d90ef870384f2b4d31a0ccb062&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 FreddoMayano

Reputation: -2
• Posts: 32
• Joined: 11-October 19

# Circle in C programming

Posted 13 October 2019 - 02:31 PM

Hi guys, I have a problem that I need to find a solution for it, which it's : writing a function that draws a circle (base on radious and mid-coordinates) .. any help how can I start ? I have no idea how can I start with solving the problem , but yeah I know the circle is symmetric so every quarter for instance is repeating itself ..
Is This A Good Question/Topic? 0

## Replies To: Circle in C programming

### #2 modi123_1

• Suitor #2

Reputation: 15359
• Posts: 61,599
• Joined: 12-June 08

## Re: Circle in C programming

Posted 13 October 2019 - 02:41 PM

What have you thought about? Attempted? Considered?

### #3 Skydiver

• Code herder

Reputation: 7139
• Posts: 24,245
• Joined: 05-May 12

## Re: Circle in C programming

Posted 13 October 2019 - 02:41 PM

Recall your algebra. What equation determines the points on the circle. Or recall your trigonometry regarding circles.

### #4 german-one

Reputation: 23
• Posts: 60
• Joined: 05-August 18

## Re: Circle in C programming

Posted 13 October 2019 - 02:53 PM

You can't "draw" anything in pure C. The output of C is text-based. That means you can write characters into a text-based window (console or terminal).
You could "draw" a circle in ASCII-Arts. That is, you can use a character like an asterisk and write it to the approximated position of the calculated circle.
Two things you have to consider in this case:
- You need to know the posion of all asterisks in a line before you write the line.
- Depending on the used font the height and the width of a character might be different. It's most likely that it looks like an oval rather than a circle in the end.

### #5 baavgai

• Dreaming Coder

Reputation: 7501
• Posts: 15,544
• Joined: 16-October 07

## Re: Circle in C programming

Posted 14 October 2019 - 04:50 AM

This looked like fun...

This is how I started:
```#define RADIUS 10

for(int i=0; i<=90; i++) {
double x = // your code here;
double y = // your code here;
printf("%d %f, %f, %f\n", i, rad, x, y);
}

```

Results:
```0 0.000000, 10.000000, 0.000000
1 0.017453, 9.998477, 0.174524
2 0.034907, 9.993908, 0.348995
3 0.052360, 9.986295, 0.523360
4 0.069813, 9.975641, 0.697565
5 0.087266, 9.961947, 0.871557
...

```

Once I was sure my math was right, I tried a plot.
```const int SIZE = RADIUS + 1;
bool plane[SIZE * SIZE] = {false}; // this little trick only works in real C, not C++
for(int i=0; i<=90; i++) {
plane[y * SIZE + x] = true;
}
// the plot, I'll leave this as a freebie
for(int y=0; y<SIZE; y++) {
for(int x=0; x<SIZE; x++) {
printf("%s", plane[y * SIZE + x] ? " * " : " . ");
}
printf("\n");
}

```

Results:
``` .  .  .  .  .  .  .  .  .  *  *
.  .  .  .  .  .  .  .  .  *  .
.  .  .  .  .  .  .  .  .  *  .
.  .  .  .  .  .  .  .  .  *  .
.  .  .  .  .  .  .  .  *  *  .
.  .  .  .  .  .  .  .  *  .  .
.  .  .  .  .  .  .  *  .  .  .
.  .  .  .  .  .  *  *  .  .  .
.  .  .  .  *  *  .  .  .  .  .
*  *  *  *  *  .  .  .  .  .  .
*  .  .  .  .  .  .  .  .  .  .

```

I now have one arc. The other three are basically mirrors. First pass looks like:
```const int QUAD_SIZE = (RADIUS + 1);
const int SIZE = QUAD_SIZE * 2;
bool plane[SIZE * SIZE] = {false};
// the reset is basically the same, you just have to worry out your simple plot transforms.

```

Results:
``` .  .  .  .  .  .  .  .  .  *  *  .  .  .  .  .  .  .  .  .  .  .
.  .  .  .  .  .  .  .  .  *  .  .  .  .  .  .  .  .  .  .  .  .
.  .  .  .  .  .  .  .  .  *  .  .  .  .  .  .  .  .  .  .  .  .
.  .  .  .  .  .  .  .  .  *  .  .  .  .  .  .  .  .  .  .  .  .
.  .  .  .  .  .  .  .  *  *  .  .  .  .  .  .  .  .  .  .  .  .
.  .  .  .  .  .  .  .  *  .  .  .  .  .  .  .  .  .  .  .  .  .
.  .  .  .  .  .  .  *  .  .  .  .  .  .  .  .  .  .  .  .  .  .
.  .  .  .  .  .  *  *  .  .  .  .  .  .  .  .  .  .  .  .  .  .
.  .  .  .  *  *  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
*  *  *  *  *  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
*  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .

```

So, you worry out how to offset the quadrant:
``` .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  *  *
.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  *  .
.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  *  .
.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  *  .
.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  *  *  .
.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  *  .  .
.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  *  .  .  .
.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  *  *  .  .  .
.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  *  *  .  .  .  .  .
.  .  .  .  .  .  .  .  .  .  .  *  *  *  *  *  .  .  .  .  .  .
.  .  .  .  .  .  .  .  .  .  .  *  .  .  .  .  .  .  .  .  .  .

```

Once you have that, you can start mirroring. e.g.
``` .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
.  *  *  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  *  *
.  .  *  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  *  .
.  .  *  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  *  .
.  .  *  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  *  .
.  .  *  *  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  *  *  .
.  .  .  *  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  *  .  .
.  .  .  .  *  .  .  .  .  .  .  .  .  .  .  .  .  .  *  .  .  .
.  .  .  .  *  *  .  .  .  .  .  .  .  .  .  .  .  *  *  .  .  .
.  .  .  .  .  .  *  *  .  .  .  .  .  .  .  *  *  .  .  .  .  .
.  .  .  .  .  .  .  *  *  *  *  *  *  *  *  *  .  .  .  .  .  .
.  .  .  .  .  .  .  .  .  .  .  *  .  .  .  .  .  .  .  .  .  .

```

The final product ain't pretty, but it is a circle within the confines of big pixels and small real estate. This idea scales up into a graphic output, once you've chosen some kind of graphics you want to use.

### #6 FreddoMayano

Reputation: -2
• Posts: 32
• Joined: 11-October 19

## Re: Circle in C programming

Posted 14 October 2019 - 07:44 AM

thanks , got the idea!