gpt4 book ai didi

c - Bresenham 的线图代码

转载 作者:行者123 更新时间:2023-12-01 14:01:11 25 4
gpt4 key购买 nike

我正在尝试使用 Bresenham 算法在 C 语言中画一条线。我在 Windows 7 的 dosbox 中使用 turbo C++ 来实现此代码。编译时我没有收到任何错误,但是当我运行代码时程序在获得 2 个坐标后终止。请帮助..

编译信息如下.. enter image description here

目录路径如下enter image description here

我的代码..

  # include <stdio.h>
# include <conio.h>
# include <graphics.h>

void main()
{
int dx,dy,x,y,p,x1,y1,x2,y2;
int gd,gm;

clrscr();

printf("\n\n\tEnter the co-ordinates of first point : ");
scanf("%d %d",&x1,&y1);
printf("\n\n\tEnter the co-ordinates of second point : ");
scanf("%d %d",&x2,&y2);

dx = (x2 - x1);
dy = (y2 - y1);

p = 2 * (dy) - (dx);

x = x1;
y = y1;

detectgraph(&gd,&gm);
initgraph(&gd,&gm,"e:\\tc\\bgi");
putpixel(x,y,WHITE);

while(x <= x2)
{
if(p < 0)
{
x=x+1;
y=y;
p = p + 2 * (dy);
}
else
{
x=x+1;
y=y+1;
p = p + 2 * (dy - dx);
}
putpixel(x,y,WHITE);
}
getch();
closegraph();
}

最佳答案

OP 应该发布使用过的输入。

发布的示例代码不起作用 x1 > x2也不y1 > y2 .这是一组会突然停止例程的输入。要修复,dxdy应该以绝对值和增量为准x & y步骤需要独立+1 -1 .

输入3,4而不是 3 4 (逗号与空格)也会打乱例程。

在while循环中,推荐if(p <= 0) .

OP 的“...代码程序在获得 2 个坐标后终止。”不够详细,因为代码当然应该在获得 2 个坐标后的某个时间终止。但是 OP 没有详细说明它过早终止的地方。

关于c - Bresenham 的线图代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18761457/

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