I can't seem to draw a circle using glut... anybody knows how to do it... thanx in advance. I think i know the logic and that is to split the circle into individual small lines and outputting them...
I can't seem to draw a circle using glut... anybody knows how to do it... thanx in advance. I think i know the logic and that is to split the circle into individual small lines and outputting them...
My original code is this
I feel the codes a little weird at the circle area. the axes are all right.PHP Code:
void function(void){
float calculate (float);
float x,y;
int i;
glClearColor(1.0, 0.0, 1.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT);
/*Draw axes*/
glColor3f(1.0,1.0,1.0); //black color
glLineWidth(5); //5 pixels
/*Perform drawing of axes*/
glBegin(GL_LINES);
glVertex2f(0,1);
glVertex2f(0,-1);
glVertex2f(1,0);
glVertex2f(-1,0);
glEnd();
/*Draw Circle*/
glColor3f(0.0,0.0,1.0);
glBegin(GL_LINES);
glLineWidth(3);
for(i=0;i<=500;i++) //no of short lines to be drawn
{
x = radius + (i-1)*0.002;
y= calculate(x);
/*scale*/
x= x * 2 - 1;
y= y * 2 -1;
glVertex2f(x,y);
x = radius + i*0.002;
y= calculate(x);
x= x * 2 - 1;
y= y * 2 -1;
glVertex2f(x,y);
}
glEnd();
glFlush();
}
float calculate(float x)
{
return radius - x; //pow(x,2) + pow(y,2) = pow(r,2)
//y = r - x
}
Last edited by bennyho03; 10-17-2004 at 11:13 AM.
if you want to do it without floating point calculations look into Bressenhams Circle Drawing Algorithm
Code:float x,y; float radius = 0.5f; glBegin(GL_LINES); glColor3f(0.0f,0.0f,0.0f); x = (float)radius * cos(359 * PI/180.0f); y = (float)radius * sin(359 * PI/180.0f); for(int j = 0; j < 360; j++) { glVertex2f(x,y); x = (float)radius * cos(j * PI/180.0f); y = (float)radius * sin(j * PI/180.0f); glVertex2f(x,y); } glEnd();
thanx for the help but there is a another question though. Why is there a 'f' at the end of? Thanxfloat radius = 0.5f
That is because (at least what I have heard) otherwise the number would default to double and you would (possibly) get a warning. I am not sure though since I dont program in C, I am a C++ guy so I am not too familiar with C standard.
maybe your reasoning is right since double is long float. Thanx anyway