gpt4 book ai didi

c++ - 画线c++中的中点算法

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:19:19 25 4
gpt4 key购买 nike

此中点算法在 c++ win32 中不起作用。它不画任何东西或只画水平线。我犯了什么错误?

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);
}

}

最佳答案

你的 d 永远不会 <0。修改 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);

关于c++ - 画线c++中的中点算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15253583/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com