How to draw a line on a computer screen

## How to draw a line on a computer screen

Although drawing a two-dimensional line is a functional already duplicated by C's lineto() function, it is important to the graphics programmer to understand how a line is drawn, and so we will study how to do it using only the single-pixel drawing function, putpixel().

To make a line, you must first determine the slope. This is how steep the line is; In pixel terms, it is how much one axis changes whenever you add 1 to the other axis.

To determine the slope, first determine the differences for the two axes. For example, if the first point's x value is 10 and the second point's x value is 30, then the difference for x is 20. Once you have the two differences, divide the x difference by the y difference. The result is the slope, or how much x changes when you add 1 to y.

As an example, imagine a line which extends at a 45 degree angle, like this:

```       /
/
/
/
/
/
/
/
```

(Assuming you're not viewing this in 40-column monospace text mode, that line probably isn't really 45 degrees. But that's okay.) Further suppose that the x axis goes from 10 to 30, and that the y axis does as well. Difference for both axes is 20. 20 divided by 20 is, obviously, 1, meaning that x increases by 1 when you add 1 to y.

A slightly more complicated example now... A line of 22.5 degrees.

```   /
|
/
|
/
|
/
```

(Yes, it does look horrible.) Anyway, now imagine that x extends from 10 to 30 again, and y goes from 10 to 50. x difference is 20. y difference is 40. 20 / 40 = 0.5, meaning that x increases by 0.5 when you add 1 to y.

Based on what we've learned here, we can make a program to plot a course between two set points on the screen. Such a program might look something like this one right here:

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

main()
{

/* The two lines below just initialize the graphics mode. */

double x1, y1, x2, y2, x, y;

/* Prompt for the coordinates of each end of the line... */

printf("x1? ");
scanf("%lf", &x1);
printf("y1? ");
scanf("%lf", &y1);
printf("x2? ");
scanf("%lf", &x2);
printf("y2? ");
scanf("%lf", &y2);

int gdriver=VGA, gmode=2;
initgraph(&gdriver, &gmode, "C:\\TC\\BGI");

double xdiff, ydiff;

/* If x1 is smaller than x2, subtract x1 from x2 to get xdiff, and vice
versa. Same goes for ydiff. */

if (x1 < x2) {xdiff = x2 - x1;}
if (x1 > x2) {xdiff = x1 - x2;}
if (y1 < y2) {ydiff = y2 - y1;}
if (y1 > y2) {ydiff = y1 - y2;}

double m = xdiff / ydiff; //m is the slope, how much x changes when you ++ y

x = x1;

/* If y1 is less than y2, keep adding to y.
If y2 is less than y1, keep subtracting from y. */
if (y1 < y2) {

for (y = y1; y < y2; y = y + 1)
{
putpixel (x, y, 9);
/* If x1 is smaller than x2, keep adding the slope to x.
If x2 is smaller than x1, keep subtracting the slope from x. */
if (x1 < x2) {
x = x + m; }
else {x = x - m; }
}}

else
for (y = y1; y > y2; y = y - 1)
{
putpixel (x, y, 9);
if (x1 < x2) {
x = x + m; }
else {x = x - m; }
}

printf("%f",m);

}
```

Back to Project 3D

Back to the main page