You are on page 1of 7

Lab 1- WAP to draw a line using DDA Algorithm

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>

void drawLineDDA(int x1, int y1, int x2, int y2) {

int dx = x2 - x1;

int dy = y2 - y1;

int steps = abs(dx) > abs(dy) ? abs(dx) : abs(dy);

float xIncrement = dx / (float)steps;

float yIncrement = dy / (float)steps;

float x = x1, y = y1;

for (int i = 0; i <= steps; i++) {

putpixel(x, y, WHITE); // You may need to adjust this to match your graphics library

x += xIncrement;
y += yIncrement;

int main() {

int gd = DETECT, gm;

initgraph(&gd, &gm, "");

int x1 = 100, y1 = 100, x2 = 400, y2 = 300;

drawLineDDA(x1, y1, x2, y2);

getch();

closegraph();

return 0;

}
Output

Lab 2- WAP to draw a line using Bresenham’s Line Drawing Algorithm

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:

1. Input two endpoints (x0, y0) and (x1, y1).

2. Calculate the differences between the two endpoints: dx = x1 - x0 and dy = y1 - y0.

3. Calculate the decision parameter d as 2 * dy - dx.

4. For each x from x0 to x1:

• Plot the pixel at (x, y).


• If d >= 0, increment y by 1 and update d as d + 2 * dy - 2 * dx.

• Otherwise, update d as d + 2 * dy.

Source Code
#include <stdio.h>

#include <graphics.h>

void drawLineBresenham(int x0, int y0, int x1, int y1) {

int dx = abs(x1 - x0);

int dy = abs(y1 - y0);

int slope_sign = ((x1 - x0) * (y1 - y0)) > 0 ? 1 : -1;

int x, y, d;

int inc_up, inc_down;

if (dy > dx) {

dx ^= dy;

dy ^= dx;

dx ^= dy;

x0 ^= y0;

y0 ^= x0;

x0 ^= y0;

x1 ^= y1;

y1 ^= x1;

x1 ^= y1;

if (x0 > x1) {

x0 ^= x1;
x1 ^= x0;

x0 ^= x1;

y0 ^= y1;

y1 ^= y0;

y0 ^= y1;

if (y0 > y1) {

inc_up = -1;

inc_down = -1;

} else {

inc_up = 1;

inc_down = 0;

d = 2 * dy - dx;

y = y0;

for (x = x0; x <= x1; x++) {

putpixel(x, y, WHITE);

delay(10); // Adjust delay as necessary for visualization

if (d > 0) {

y += inc_up;

d -= 2 * dx;

d += 2 * dy;

}
int main() {

int gd = DETECT, gm;

initgraph(&gd, &gm, NULL);

int x0, y0, x1, y1;

printf("Enter endpoints (x0, y0) and (x1, y1): ");

scanf("%d %d %d %d", &x0, &y0, &x1, &y1);

drawLineBresenham(x0, y0, x1, y1);

getch();

closegraph();

return 0;

Output

You might also like