Thread: Drawing a circle using glut...

  1. #1
    Registered User
    Join Date
    Aug 2004
    Posts
    21

    Drawing a circle using glut...

    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...

  2. #2
    Registered User
    Join Date
    Aug 2004
    Posts
    21
    My original code is this
    PHP Code:
    void function(void){
        
    float calculate (float);

        
    float x,y;
        
    int i;
        
    glClearColor(1.00.01.00.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
        
    {
            
    radius + (i-1)*0.002;
            
    ycalculate(x);
            
    /*scale*/
            
    x1;
            
    y-1;

            
    glVertex2f(x,y);

            
    radius i*0.002;
            
    ycalculate(x);
            
    x1;
            
    y-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

    I feel the codes a little weird at the circle area. the axes are all right.
    Last edited by bennyho03; 10-17-2004 at 11:13 AM.

  3. #3
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    if you want to do it without floating point calculations look into Bressenhams Circle Drawing Algorithm

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    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();

  5. #5
    Registered User
    Join Date
    Aug 2004
    Posts
    21
    thanx for the help but there is a another question though. Why is there a 'f' at the end of
    float radius = 0.5f
    ? Thanx

  6. #6
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    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.

  7. #7
    Registered User
    Join Date
    Aug 2004
    Posts
    21
    maybe your reasoning is right since double is long float. Thanx anyway

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Drawing a circle with ASCII character
    By bnkslo in forum C Programming
    Replies: 15
    Last Post: 04-03-2008, 12:56 PM
  2. drawing circle to console
    By golom in forum C Programming
    Replies: 2
    Last Post: 12-20-2006, 10:57 AM
  3. Drawing circle into a bitmap file
    By brokensail in forum C++ Programming
    Replies: 2
    Last Post: 10-30-2006, 01:26 AM
  4. drawing circle in VGA mode
    By GanglyLamb in forum C Programming
    Replies: 1
    Last Post: 12-19-2002, 11:46 AM
  5. Drawing a circle in a video captured frame
    By skyhigh in forum C Programming
    Replies: 2
    Last Post: 12-05-2001, 01:00 AM