简体   繁体   English

画线c ++中的中点算法

[英]Midpoint Algorithm in drawing line c++

This midpoint algorithm in c++ win32 doesn't work. C ++ Win32中的中点算法不起作用。 It draws nothing or only horizontal lines. 它什么也没画,只画水平线。 What error(s) am I making? 我犯什么错误?

void midPoint(HDC hdc)
{

    double dx = end.getXPoint()-start.getXPoint();
    double dy = end.getYPoint()-start.getYPoint();

    double x = start.getXPoint();
    double y = start.getYPoint();
    SetPixel(hdc,x,y,color);

    double d = dx - (dy/2);
    double d1 = dx;
    double d2 = abs(dx-dy);

    while(x < end.getXPoint())
    {
    d = abs(((( y+0.5)-start.getYPoint())*dx) - (((x+1)-start.getXPoint())*dy));

    if(d < 0)
    {
        x = x+1;
        y = y+1;
    }
    else
    {
        x = x+1;
    }
    SetPixel(hdc,x,y,color);
    }

}

Your d is never <0. 您的d永远不会小于0。 Revise the formula for d, especially the ( and ). 修改d的公式,尤其是(和)。

d = abs(((( y+0.5)-start.getYPoint())*dx) - (((x+1)-start.getXPoint())*dy));

    if(d < 0)
    {
        x = x+1;
        y = y+1;   // never executed
    }
    else
    {
        x = x+1;   // horizontal line
    }
    SetPixel(hdc,x,y,color);

当您在d的计算中采用abs值时,它永远不会小于0。因此X的值会单独增加,而您将获得水平线。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM