如何用c#编写bresenham算法?
我是这样写的,但只适用于 50% 的情况。有人能告诉我出了什么问题吗?
public void Bresenham(int x1,int y1,int x2,int y2,Color c)
{
double dx = x2 - x1;
double dy = y2 - y1;
double d = 2*dy-dx; //aux variable
double p1 = 2 * dy ;
double p2 = 2 * (dy - dx);
int x = x1;
int y = y1;
int xend;
c = kolor;
if (x1 > x2)
{
x = x2;
y = y2;
xend = x1;
}
else
{
x = x1;
y = y1;
xend = x2;
}
bitmapa.SetPixel(x, y,c);
try
{
while (x < xend)
{
x++;
if (d < 0)
{
d += p1;
}
else
{
d += p2;
y += 1;
}
bitmapa.SetPixel(x, y, c);
}
}
谢谢:)
I wrote like this but it works only in 50% cases. Can someone tell what's wrong?
public void Bresenham(int x1,int y1,int x2,int y2,Color c)
{
double dx = x2 - x1;
double dy = y2 - y1;
double d = 2*dy-dx; //aux variable
double p1 = 2 * dy ;
double p2 = 2 * (dy - dx);
int x = x1;
int y = y1;
int xend;
c = kolor;
if (x1 > x2)
{
x = x2;
y = y2;
xend = x1;
}
else
{
x = x1;
y = y1;
xend = x2;
}
bitmapa.SetPixel(x, y,c);
try
{
while (x < xend)
{
x++;
if (d < 0)
{
d += p1;
}
else
{
d += p2;
y += 1;
}
bitmapa.SetPixel(x, y, c);
}
}
Thanks:)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
第一次拍摄时,您错过了应该像现在处理 Y 一样处理其他坐标的情况。现在,您可以处理 DY < 时的情况。 DX,您还应该处理 DX < 的情况。 DY,即直线的斜率不同。
要理解我的意思,请查看
steep
此处。实际上,你的算法只能在 1/4 的情况下起作用。
At first shot, you are missing a case when other coordinate should be handled like now your are handling Y. You now handle the case when DY < DX, you should also handle case when DX < DY, i.e. slope of the line is different.
To understand what I'm saying, look
steep
here.And actually, your algorithm will work only in 1/4 cases.