3

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..

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();
}
genpfault
  • 51,148
  • 11
  • 85
  • 139
Lucy
  • 1,812
  • 15
  • 55
  • 93
  • 5
    Unless you're working on embedded systems, `void main()` is wrong, wrong, wrong. `int main (void)`, for h**k's sake! – verbose Sep 12 '13 at 10:15
  • Can you please exactly specify the error and the line number. – Michel Keijzers Sep 12 '13 at 10:16
  • I'm not getting an error message as such while compiling or running..only that when i enter the values of 1st co-ordinates the program gets terminated abruptly... – Lucy Sep 12 '13 at 10:22
  • Use `Alt + F5` to see if any error msg was generated, but you were not able to see it because console window closed. – 0xF1 Sep 12 '13 at 11:50

4 Answers4

1

OP should post input that was used.

The posted sample code does not work is x1 > x2 nor y1 > y2. This is one set of input that would stop the routine abruptly. To fix, the dx and dy should be based on the absolute value and the incremental x & y steps need to be independently +1 or -1.

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

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

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

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
  • i checked the code it does work for 3, 4, 7 and 8th octant but not for other 4, just create some mess. –  Jun 02 '16 at 18:40
0

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.

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);
stefan_ah
  • 54
  • 4
  • i have added screenshot in my question..do i still need to make the changes you mentioned?? – Lucy Sep 12 '13 at 10:28
0

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

detectgraph(&gd,&gm);
initgraph(&gd,&gm,"C:\\TURBOC3\\bgi");
putpixel(x,y,WHITE);
double-beep
  • 5,031
  • 17
  • 33
  • 41
Lucy
  • 1,812
  • 15
  • 55
  • 93
0

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

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);
ashumeow
  • 141
  • 1
  • 10