PDA

View Full Version : Drawing a circle "filled" using GL_LINES



anthony1777
07-20-2016, 01:23 PM
I'm trying to draw a Circle that will be filled using GL_LINES.
Im able to draw a Circle using sin and cos but that doesn't fill it, only give me a frame.

I had an idea to draw just a bunch of horizontal lines (enough to make a cricle), but I wasn't sure how to approach this. (Trying to make a gradient as well). Here's what I have for the circle the way it is now.



typedef struct
{
float x;
float y;
}CIRCLE;

CIRCLE circle;

void createcircle (int k, int r, int h) {
glBegin(GL_LINES);
for (int i = 0; i < 180; i++)
{
circle.x = r * cos(i) - h;
circle.y = r * sin(i) + k;
glVertex3f(circle.x + k,circle.y - h,0);

circle.x = r * cos(i + 0.1) - h;
circle.y = r * sin(i + 0.1) + k;
glVertex3f(circle.x + k,circle.y - h,0);
}
glEnd();
}

john_connor
07-20-2016, 01:53 PM
try GL_POLYGON instead of GL_LINES



I had an idea to draw just a bunch of horizontal lines (enough to make a cricle), but I wasn't sure how to approach this.


how about this:
1. calculate the next y-coordinate: y = (r^2 - x^2)^(1/2)
2. do this in a for loop: start: y = -r; end: y = r; step: y += r/10 (for 10 horizontal lines)
3. you have y, now calculate x tha same way: x = (r^2 - y^2)^(1/2)
4. create 2 vertices: (-x|y) and (x|y)

class_OpenGL
07-30-2016, 07:55 PM
Hello, you can also use GL_TRIANGLE_FAN to draw a circle. This is the code:


void drawCircle(float r, float x, float y) {
float i = 0.0f;

glBegin(GL_TRIANGLE_FAN);

glVertex2f(x, y); // Center
for(i = 0.0f; i <= 360; i++)
glVertex2f(r*cos(M_PI * i / 180.0) + x, r*sin(M_PI * i / 180.0) + y);

glEnd();
}