C语言 Bresenham 的线描代码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18761457/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 07:25:54  来源:igfitidea点击:

Bresenham's line drawing Code

cvector-graphicsturbo-c++bresenhamdosbox

提问by Lucy

I'm trying to draw a line in C language using Bresenham's algorithm.I'm using turbo C++ in dosbox for windows 7 to implement this code.While compiling i'm not getting any error but when i run the code the programs terminates after obtaining the 2 co-ordinates.Please Help..

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

the message on compiling is as follows.. enter image description here

编译消息如下.. 在此处输入图片说明

the directories path is as followsenter image description here

目录路径如下在此处输入图片说明

My code..

我的代码..

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

回答by chux - Reinstate Monica

OP should post input that was used.

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

The posted sample code does not work is x1> x2nor y1> y2. This is one set of input that would stop the routine abruptly. To fix, the dxand dyshould be based on the absolute value and the incremental x& ysteps need to be independently +1or-1.

发布的示例代码不起作用x1>x2y1> y2。这是一组会突然停止例程的输入。要修复,dxdy应该基于绝对值,增量xy步骤需要独立+1-1

An input of 3,4instead of 3 4(comma vs. whitespace) will also mess up the routine.

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

In the while loop, recommend if(p <= 0).

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

OP's "... code the programs terminates after obtaining the 2 co-ordinates." is not detailed enough, for of course the code shouldterminate sometime after obtaining the 2 co-ordinates. But OP does not detail where it terminates too early.

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

回答by Lucy

A way to rectify the problem is by changing the path in the initgraph function according to the address mentioned in the screenshot.

解决问题的一种方法是根据屏幕截图中提到的地址更改initgraph函数中的路径。

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

回答by ashumeow

Nice program. But, you haven't initialized any loop as well as lines coded in while-loop were partially incorrect. Here is my try:-

不错的节目。但是,您还没有初始化任何循环以及在 while 循环中编码的行部分不正确。这是我的尝试:-

i = 1; // loop initialization
do {
    putpixel(x, y, 15);
    while(p >= 0) {
        y = y + 1;
        p = p - (2 * dx);
    }
    x = x + 1;
    p = p + (2 * dy);
    i = i + 1;
}
while(i <= dx);

回答by stefan_ah

This is a typical perfect point to start the debugger and go through the code step-by-step, watching any variables. If a debugger is unavailable, printf debugging to the console is a backup alternative.

这是启动调试器并逐步执行代码、观察任何变量的典型完美点。如果调试器不可用,printf 调试到控制台是一个备用选择。

A first tip is to check that these lines do not generate an error/exception:

第一个提示是检查这些行是否不会产生错误/异常:

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