Using classes and objects, bresenham circle drawing algorithm

I need the c++ program of bresenham circle drawing algorithm, written using classes and objects.

I did in Qt. Will it work?

void MainWindow::drawlineLow(int x0, int y0, int x1, int y1)
{
int x,y,D;
int dx=x1-x0;
int dy=y1-y0;
int yi=grid;
if(dy<0)
{
    yi=-yi;
    dy=-dy;
}
D=2*dy-dx;
y=y0;
for(x=x0;x<(x1-(x1%grid)+grid);x+=grid)
{
    point(x,y);
    if(D>0)
    {
        y+=yi;
        D=D-2*dx;
    }
    D+=2*dy;
}
}
void MainWindow::drawlineHigh(int x0, int y0, int x1, int y1)
{
int x,y,D;
int dx=x1-x0;
int dy=y1-y0;
int xi=grid;
if(dx<0)
{
    xi=-xi;
    dx=-dx;
}
D=2*dx-dy;
x=x0;
for(y=y0;y<(y1-(y1%grid)+grid);y+=grid)
{
    point(x,y);
    if(D>0)
    {
        x+=xi;
        D=D-2*dy;
    }
    D=D+2*dx;
}

}

void MainWindow::drawline(int x0, int y0, int x1, int y1)
{
if(abs(y1-y0)<abs(x1-x0))
{
    if(x0>x1)
        drawlineLow(x1,y1,x0,y0);
    else drawlineLow(x0,y0,x1,y1);
}
else{
    if(y0>y1)
        drawlineHigh(x1,y1,x0,y0);
    else drawlineHigh(x0,y0,x1,y1);
}
}

Ofcourse It will not work if you merely copy paste. This consists of algorithm. Just do changes accordingly.

1 Like