LAb 2 Comp Graphics
LAb 2 Comp Graphics
Institute of Engineering
Thapathali Campus, Thapathali
Submitted By:
Submitted To:
Department of Electronics and Computer Engineering
BLA ALGORITHM:
Step 1: Start
Step 2: Take the coordinates of the initial point (x0, y0) and the final point (x1, y1).
Step 3: Find the difference dx = x1-x0; dy= y1-y0;
Step 4: Determine the initial decision parameter.
Case 1: If dx ≥ dy, initialize p0=2dy−dxp0 = 2dy - dxp0=2dy−dx
Case 2: If dx < dy, initialize p0=2dx−dyp0 = 2dx - dyp0=2dx−dy
Step 5: Set the starting point to (x0, y0)
Step 6: Iterate
For dx≥dy:
If pk<0: Increment x, keep y.
If pk≥0: Increment both x and y.
Update pk as:
pk=pk+2dypk = pk + 2dy (if pk < 0)
pk=pk+2dy−2dxpk = pk + 2dy - 2dx (if pk ≥ 0)
SOURCE CODE:
#include <stdio.h> } else {
#include <graphics.h> y--;
#include <stdlib.h> }
int main() { pk += 2 * dy - 2 * dx;
int x0, y0, x1, y1, dx, dy, pk, x, y; }
int gd = DETECT, gm; delay(50);
}
printf("Enter x1 and y1: "); } else {
scanf("%d %d", &x0, &y0); pk = 2 * dx - dy;
printf("Enter x2 and y2: "); x = x0;
scanf("%d %d", &x1, &y1); y = y0;
dx = abs(x1 - x0);
dy = abs(y1 - y0); while (x != x1 || y != y1) {
initgraph(&gd, &gm, NULL); putpixel(x, y, WHITE);
setcolor(WHITE);
outtextxy(10, 10, "Bibek "); if (y1 > y0) {
y++;
if (dx >= dy) { } else {
pk = 2 * dy - dx; y--;
x = x0; }
y = y0;
if (pk < 0) {
while (x != x1 || y != y1) { pk += 2 * dx;
putpixel(x, y, WHITE); } else {
if (x1 > x0) {
if (x1 > x0) { x++;
x++; } else {
} else { x--;
x--; }
} pk += 2 * dx - 2 * dy;
}
if (pk < 0) { delay(50);
pk += 2 * dy; }
} else { }
if (y1 > y0) { delay(5000);
y++; closegraph();
return 0; }
OUTPUT:
CONCLUSION:
The Bresenham’s Line Algorithm was successfully implemented to draw a line on the screen. Its
efficiency over the DDA algorithm was observed as it only used integer arithmetic, making it suitable
for real-time applications in computer graphics. To enhance the visual experience, we introduced a
10-millisecond delay in drawing each pixel. This delay allowed us to observe the line being
constructed step by step, creating an animation effect as the line gradually extended from (x1, y1) to
(x2, y2).