Page 51 - DCAP313_LAB_ON_COMPUTER_GRAPHICS
P. 51
Unit 3: Implementing Line Algorithm
This algorithm will continue to increase the x value (and decrement) as long as is greater than Notes
zero. It illuminates only one pixel per row on the line. We are assuming that ∆y = !0 which is
valid since we are only allowing for the non-horizontal edges of the trapezoids that we want
to rasterizing. It should also be mentioned that the above algorithm is written for the case
where m is positive. If m = 0, then € = − (1 − (x 1 − i)) and is never incremented. In this case, we
have a vertical line, and the x value will never need to be incremented. If m < 0, then we must
decrement i in the algorithm, and add |m| to ε.
3.2.5 C Code Line Drawing Algorithm
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd = DETECT, gm;
int dx, dy, p, end;
fl o a t x 1 , x 2 , y 1 , y 2 , x, y;
initgraph(&gd, &gm, “c:\tc\bgi”);
printf(“Enter Value of X 1 :”);
scanf(“%f”, &x 1 );
printf(“Enter Value of Y 1 :”);
scanf(“%f”, &y 1 );
printf(“Enter Value of X 2 :”);
scanf(“%f”, &x 2 );
printf(“Enter Value of Y 2 :”);
scanf(“%f”, &y 2 );
dx = abs(x 1 – x 2 );
dy = abs(y 1 – y 2 );
p = 2 * dy – dx;
if(x 1 > x 2 )
{
x = x 2 ;
y = y 2 ;
e n d = x 1 ;
}
else
{
x = x 1 ;
y = y 1 ;
e n d = x 2 ;
}
putpixel(x, y, 10);
while(x < end)
{
x = x + 1 ;
i f ( p < 0 )
LOVELY PROFESSIONAL UNIVERSITY 45