Professional Documents
Culture Documents
Lab Report 1 and 2, Computer Graphics BCA 5th Sem
Lab Report 1 and 2, Computer Graphics BCA 5th Sem
Theory:
DDA algorithm is an incremental method used for line drawing. It works by calculating the
intermediate points between two given points along the line path. The algorithm computes the
slope of the line and increments the coordinates by the slope at each step, thereby drawing the
line from the starting point to the endpoint.
Algorithm:
1. Calculate the change in x-coordinate (∆x) and y-coordinate (∆y) between the endpoints.
2. Determine the number of steps required for drawing the line (which is maximum of ∆x
and ∆y).
3. Calculate the incremental change in x (∆x_inc) and y (∆y_inc) for each step.
4. Start from the initial point and at each step, increment the coordinates by (∆x_inc,
∆y_inc) until the final point is reached.
Source Code
#include <stdio.h>
#include <graphics.h>
int dx = x2 - x1;
int dy = y2 - y1;
putpixel(x, y, WHITE); // You may need to adjust this to match your graphics library
x += xIncrement;
y += yIncrement;
int main() {
getch();
closegraph();
return 0;
}
Output
Theory:
Bresenham's algorithm works by incrementally plotting points along the line path between two
endpoints, selecting the closest pixel to the ideal line path. It ensures that the line drawn is as
close as possible to the ideal line while maintaining consistency in slope.
Algorithm:
Source Code
#include <stdio.h>
#include <graphics.h>
int x, y, d;
dx ^= dy;
dy ^= dx;
dx ^= dy;
x0 ^= y0;
y0 ^= x0;
x0 ^= y0;
x1 ^= y1;
y1 ^= x1;
x1 ^= y1;
x0 ^= x1;
x1 ^= x0;
x0 ^= x1;
y0 ^= y1;
y1 ^= y0;
y0 ^= y1;
inc_up = -1;
inc_down = -1;
} else {
inc_up = 1;
inc_down = 0;
d = 2 * dy - dx;
y = y0;
putpixel(x, y, WHITE);
if (d > 0) {
y += inc_up;
d -= 2 * dx;
d += 2 * dy;
}
int main() {
getch();
closegraph();
return 0;
Output