5 Replies - 273 Views - Last Post: 14 October 2019 - 07:44 AM Rate Topic: -----

#1 FreddoMayano   User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • 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   User is online

  • Suitor #2
  • member icon



Reputation: 15359
  • View blog
  • 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?
Was This Post Helpful? 0
  • +
  • -

#3 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7139
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#4 german-one   User is offline

  • D.I.C Head
  • member icon

Reputation: 23
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#5 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7501
  • View blog
  • 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 rad = // your code here;
    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++) {
    // your calcs
    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.
Was This Post Helpful? 1
  • +
  • -

#6 FreddoMayano   User is offline

  • New D.I.C Head

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

Re: Circle in C programming

Posted 14 October 2019 - 07:44 AM

thanks , got the idea!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1