How to divide this circle into 2/3 quadrants and fill each quadrant with a color?.

How to divide this circle into 2/3 quadrants and fill each quadrant with a color?.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
  #include<graphics.h>
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
void main()
{
int gdriver=DETECT,gmode;
initgraph(&gdriver,&gmode,"c:\\turboc3\\bgi");
setcolor(YELLOW);
setfillstyle(SOLID_FILL,RED);
circle(200,200,100);
floodfill(200,200,YELLOW);

getch();
closegraph();


}
Hi new programmer, the answer I can give you is something that uses array, functions and, in the best case scenario, object-oriented programming (makes it easier, it's not actually required at all). I haven't ever used graphics.h, but lately I've been obsessed with circles in console, therefore I can present something you can do (it's generic, not necessarily for console. It can also be used for bitmap images).
Now, since I am not sure what you mean by "2/3 quadrants" (at first I thought it meant that a quadrant=2 thirds of a circle, but it didn't make sense to me), and since I suppose it refers to "2 or 3 quadrants", then I will answer accordingly.

1. Determine the circle's radius.
1.1. If you want the circle to be in the top left corner, then ignore 1.2.
1.2. Determine the point around which you want to draw the circle (x and y coordinates).
2. Using circle defining algorithms found on the internet (i use midpoint circle algorithm), you can create your circle and (i recommend doing this) put it into an array or, even better, in a std::string object.
3. I start with the middle, rightmost point, but you can start anywhere
(I highly recommend you start in one of the four quadrant intersections, and here are the 4 equations:
top point:
x=center dot's x coordinate
y=center dot's y coordinate-radius
bottom point:
x=center dot's x coordinate
y=center dot's y coordinate+radius
left point:
x=center dot's x coordinate-radius
y=center dot's y coordinate
right point:
x=center dot's x coordinate+radius
y=center dot's y coordinate)
4. You can use equations to project lines from the center, through a dot and on the circle (but I haven't really written the whole thing down), or this (it's generic and not found in libraries):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
//start with the rightmost point
int start_x=center_x+radius;
int start_y=center_y;
int final_x=start_x;
int final_y=start_y
//circle can be any array, and you only have to mention, inside the function, what a point means in
//that array
int numberOfDots=functionGetNumberOfDots(circle);
//since a circle has 360 degrees, then a point is (360/the number of points) degrees.
float angleRatio=360./float(numberOfPoints);
//this is for 3 equal quadrants, each having a third degrees of a full circle
float quadrant=120.;
for (float initialAngle=0; initialAngle<quadrant; initialAngle+=angleRatio)
{
    //this moves the dot along the circle's circumference
    moveCounterClockwise(final_x, final_y);
}
//now that you have the arc (quadrant) of the circle, draw 2 lines: from center to start point and
//from center to final point
line(circle, center_x, center_y, start_x, start_y);
line(circle, center_x, center_y, final_x, final_y);
fillSector(circle);
drawSector(circle, color);

You can use this "pseudocode prototype" or whatever it could be called, and you should be all right.
@Troaat

Don't use float or double in a for loop, these values are not stored exactly. Stick with using integral types only.

It's easy to work out how times to loop as an integer.
I'm not sure of the implementation of your graphics class, so it's hard to provide concrete code.
Point is just a generic class with members x and y.
arc_start and arc_end are calculated using the parametric equations of a circle. http://jwilson.coe.uga.edu/EMAT6680Fa05/Parveen/Assignment%2010/parametric_equations.htm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
const Point center{ 200, 200 };
const int radius{ 100 }, num_sectors{ 4 };
const double theta{ to_radians( 360.0 / num_sectors ) };
// starts at 3 o'clock, goes anti-clockwise
for( int i{}; i < num_sectors; i++ ) {
    double start{ i % num_sectors * theta }, 
        end{ (i + 1) % num_sectors * theta };
    Point arc_start{
        radius * cos( start ) + center.x,
        radius * sin( start ) + center.y
    }, arc_end{
        radius * cos( end ) + center.x,
        radius * sin( end ) + center.y
    };
}
@TheIdeasMan
Yea, I've seen that. Do you know why it happens?
When I wanted to create a stopwatch, I incremented by .01 seconds or .001, and it went nuts.
What's its deal?
@Troaat

They are stored as binary fractions. Have a look on a search engine near you.
@Troat
Look specifically for "IEEE754 binary32 floating point". This is float, usually. (double is usually represented using IEEE754's binary64 format.)

This paper does a good job of explaining:
https://ece.uwaterloo.ca/~dwharder/NumericalAnalysis/02Numerics/Double/paper.pdf

You should probably use <chrono> to measure time -- the library manipulates rational numbers only and is unit-consistent (typesafe) in a way that plain floating-point numbers aren't.

Also there's this famous algorithm that approximates 1/sqrt(x):
1
2
3
4
5
6
7
8
float Q_rsqrt(float x) {
  float xhalf = 0.5f * x;
  int i = *(int*)&x;         
  i = 0x5f3759df - (i >> 1);  
  x = *(float*)&i;
  x = x*(1.5f-(xhalf*x*x));
  return x;
}

http://h14s.p5r.org/2012/09/0x5f3759df.html has an excellent explanation of how it works. Challenge: figure it out without cheating?

Last edited on
Topic archived. No new replies allowed.