QT 驗證改進後Bresenham演算法

qq_24855259發表於2016-10-18
void LoginDialog::paintEvent(QPaintEvent *)
{

    int x0,  y0, xEnd, yEnd;
        QPainter painter(this);
       //引數為:畫刷,線寬,畫筆風格,畫筆端點,畫筆連線風格
        QPen pen(Qt::green, 5, Qt::DotLine, Qt::RoundCap, Qt::RoundJoin);
        painter.setPen(pen);

        x0= ui->x->text().toInt();
        y0=ui->y->text().toInt();
        xEnd=ui->x1->text().toInt();
        yEnd=ui->y2->text().toInt();
        int x,y,dx,dy,e;
        float k;
        dx=xEnd-x0;
        dy=yEnd-y0;
        k=(float)dy/dx;
        e=-dx;x=x0;y=y0;
        if(k>=0||k<=1){
        while(x<=xEnd){
                painter.drawPoint(x+100,y+100);
                x++;
                e=e+2*dy;
                if(e>0){
                    y++;
                    e=e-2*dx;
                }
            }

}
        else if(k>=1){
            while(y<=yEnd){
                    painter.drawPoint(x+100,y+100);
                    y++;
                    e=e+2*dx;
                    if(e>0){
                        x++;
                        e=e-2*dy;
                    }
                }
        }
}

執行結果示例


            

相關文章