Question

I am using this Bresenham's algorithm to draw circle. This code works, but I need filled circle, not only border.

This is my code:

void setPixel(int x,int y)  {  
    glBegin(GL_POINTS); 
    glColor3f(0.0f, 1.0f, 0.0f);
    glPointSize (20.0);
    glVertex2i(x,y);
    glEnd();
}

void BresenhamKrug(int xCenter, int yCenter,int r){ //napravite parametarski poziv
int x=0,y=r;
int d=3-(2*r);

glColor3f(0.5,0.3,1.); 


while(x<=y)
{
    setPixel(xCenter+x,yCenter+y);  setPixel(xCenter+y,yCenter+x);
    setPixel(xCenter-x,yCenter+y);  setPixel(xCenter+y,yCenter-x); 
    setPixel(xCenter-x,yCenter-y);  setPixel(xCenter-y,yCenter-x); 
    setPixel(xCenter+x,yCenter-y);  setPixel(xCenter-y,yCenter+x); 

    if (d<0)
        d += (4*x)+6;
    else{
          d += (4*(x-y))+10;
          y--;
    }

    x++;
}
glFlush();
}

Is there any way to do that ?

Was it helpful?

Solution

You may save your points in an array and draw a polygon instead, or triangle fans.

glBegin(GL_POLYGON);

or:

void BresenhamKrug(int xCenter, int yCenter,int r){ //napravite parametarski poziv
    int x=0,y=r;
    int d=3-(2*r);

    glColor3f(0.5,0.3,1.); 

    glBegin(GL_POLYGON);

    while(x<=y)
    {
        glVertex2i(xCenter+x,yCenter+y);  glVertex2i(xCenter+y,yCenter+x);
        glVertex2i(xCenter-x,yCenter+y);  glVertex2i(xCenter+y,yCenter-x); 
        glVertex2i(xCenter-x,yCenter-y);  glVertex2i(xCenter-y,yCenter-x); 
        glVertex2i(xCenter+x,yCenter-y);  glVertex2i(xCenter-y,yCenter+x); 

        if (d<0)
            d += (4*x)+6;
        else{
              d += (4*(x-y))+10;
              y--;
        }

        x++;
    }
    glEnd();
    glFlush();
}

but not sure a simple circle equation would not have been sufficient..drawing circle, rasterization being handled by openGL.

OTHER TIPS

The simplest solution is to modify the algorithm so that instead of plotting pixels around the edge, you instead connect the points with lines.

void drawLine(const float x1, const float y1, const float x2, const float y2) {
    const bool steep = (fabs(y2 - y1) > fabs(x2 - x1));
    if(steep) {
        std::swap(x1, y1);
        std::swap(x2, y2);
    }

    if(x1 > x2) {
        std::swap(x1, x2);
        std::swap(y1, y2);
    }

    const float dx = x2 - x1;
    const float dy = fabs(y2 - y1);

    float error = dx / 2.0f;
    const int ystep = (y1 < y2) ? 1 : -1;
    int y = (int)y1;

    const int maxX = (int)x2;

    for(int x=(int)x1; x<maxX; x++) {
        if(steep) {
            setPixel(y,x);
        }
        else {
            setPixel(x,y);
        }

        error -= dy;
        if(error < 0) {
            y += ystep;
            error += dx;
        }
    }
}

void BresenhamKrug(int xCenter, int yCenter,int r){ //napravite parametarski poziv
    int x=0,y=r;
    int d=3-(2*r);
    glColor3f(0.5,0.3,1.);
    while(x<=y) {
        drawLine(xCenter+x,yCenter+y,  xCenter+y,yCenter+x);
        drawLine(xCenter-x,yCenter+y,  xCenter+y,yCenter-x); 
        drawLine(xCenter-x,yCenter-y,  xCenter-y,yCenter-x); 
        drawLine(xCenter+x,yCenter-y,  xCenter-y,yCenter+x); 
        if (d<0)
            d += (4*x)+6;
        else{
            d += (4*(x-y))+10;
            y--;
        }
        x++;
    }
    glFlush();
}

For all intents and purposes, Bresenham's line algorithm is probably the most appropriate for this.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top