/* TESTLINE.C -- by Gary N Wilkerson Jr. * * Demonstrates the usage of the Line function * and illustrates one of its discrepencies. */ #include /* Required for int86() functions */ #include /* Required for keyboard functions */ /* Video display definitions */ #define TEXT_MODE 0x03 #define VGA256 0x13 /* Function prototypes */ void Line(int X1, int Y1, int X2, int Y2, unsigned char C); void SetVideoMode(int mode); void SetPixel(int X, int Y, unsigned char C); /* Create a pointer to video memory */ unsigned char far *videoMem = (unsigned char far *)0xA0000000L; /* Our main program */ int main(void) { while (kbhit()) getch(); /* Clear keyboard */ SetVideoMode(VGA256); /* Set VGA mode 13h */ Line( 20, 20, 20, 100, 1); /* Line A */ Line( 20, 140, 80, 60, 2); /* Line B */ Line( 90, 20, 150, 20, 4); /* Line C */ Line(110, 60, 150, 80, 4); /* Line D */ while (!kbhit()); /* Wait for a key */ SetVideoMode(TEXT_MODE); /* Return to text mode */ while (kbhit()) getch(); /* Clear keyboard */ return 0; /* Return without errors */ } /* SetVideoMode function */ void SetVideoMode(int mode) { union REGS regs; /* Create register variables */ regs.x.ax = mode; /* Set video mode */ /* Call interrupt to complete the task */ int86(0x10, ®s, ®s); } /* SetPixel function */ void SetPixel(int X, int Y, unsigned char C) { videoMem[Y * 320 + X] = C; } /* Line function: * * Draws a line given 2 (X,Y) coordinates and a color attribute. */ void Line(int X1, int Y1, int X2, int Y2, unsigned char C) { int i; /* Loop counter and temp variable */ float deltaX, deltaY; /* 2 deltas */ float slope, currY; /* slope and current Y coordinate */ if (X2 == X1) { /* determine if line is vertical */ /* If so, draw vertical line */ if (Y1 > Y2) { /* smallest value first please */ i = Y1; Y1 = Y2; Y2 = i; } /* draw vertically from Y1 to Y2 */ for (i = Y1; i <= Y2; i++) SetPixel(X1, i, C); } else { /* determine slope, if not vertical */ if (X1 > X2) { /* smallest value first please */ i = X1; X1 = X2; X2 = i; i = Y1; Y1 = Y2; Y2 = i; } /* get the two deltas and the slope */ deltaX = X2 - X1; deltaY = Y2 - Y1; slope = deltaY / deltaX; /* draw the line */ currY = Y1; for (i = X1; i <= X2; i++) { SetPixel(i, (int)currY, C); currY += slope; } } }