0% found this document useful (0 votes)
5 views5 pages

Computer Graphics Assignment

The document presents two algorithms for line drawing in computer graphics: the Digital Differential Analyzer (DDA) and Bresenham's Line Algorithm. Each algorithm is implemented in C using OpenGL, with functions to initialize the graphics environment, draw lines, and display the results. Example coordinates for drawing lines are provided in both implementations.

Uploaded by

Paul Phineas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views5 pages

Computer Graphics Assignment

The document presents two algorithms for line drawing in computer graphics: the Digital Differential Analyzer (DDA) and Bresenham's Line Algorithm. Each algorithm is implemented in C using OpenGL, with functions to initialize the graphics environment, draw lines, and display the results. Example coordinates for drawing lines are provided in both implementations.

Uploaded by

Paul Phineas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

COMPUTER GRAPHICS.

PAUL PHINEAS MACHANDA,


E020-01-0914/2020.
ASSIGNMENT.
1. Digital Differential Analyzer (DDA) Algorithm
#include <GL/glut.h>

void init(void) {
glClearColor(1.0, 1.0, 1.0, 0.0); // Set background color to white
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0.0, 300.0, 0.0, 300.0); // Set orthographic projection (adjust as needed)
}

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


int dx = x2 - x1;
int dy = y2 - y1;

float x = x1;
float y = y1;

// Determine the longer difference


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

float xinc = (float)dx / steps;


float yinc = (float)dy / steps;

// Draw each point along the line


for (int i = 0; i <= steps; i++) {
glBegin(GL_POINTS);
glVertex2f(x, y);
glEnd();
x += xinc;
y += yinc;
}
}

void display(void) {
glClear(GL_COLOR_BUFFER_BIT);

// Replace these with your desired line coordinates


DDA(50, 100, 200, 250); // Example line

glFlush();
}

int main(int argc, char** argv) {


glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500, 500);
glutInitWindowPosition(100, 100);
glutCreateWindow("DDA Line Drawing");
init();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}

2. Bresenham's Line Algorithm


#include <GL/glut.h>
void init(void) {
glClearColor(1.0, 1.0, 1.0, 0.0); // Set background color to white
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0.0, 300.0, 0.0, 300.0); // Set orthographic projection (adjust as needed)
}

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


int dx = x2 - x1;
int dy = y2 - y1;
int incx = 1;
int incy = 1;

if (dx < 0) {
dx = -dx;
incx = -1;
}

if (dy < 0) {
dy = -dy;
incy = -1;
}

int x = x1;
int y = y1;

if (dx > dy) {


int e = 2 * dy - dx;
for (int i = 0; i <= dx; i++) {
glBegin(GL_POINTS);
glVertex2i(x, y);
glEnd();
if (e >= 0) {
y += incy;
e += 2 * incy - 2 * dx;
} else {
e += 2 * incy;
}
x += incx;
}
} else {
int e = 2 * dx - dy;
for (int i = 0; i <= dy; i++) {
glBegin(GL_POINTS);
glVertex2i(x, y);
glEnd();
if (e >= 0) {
x += incx;
e += 2 * incx - 2 * dy;
} else {
e += 2 * incx;
}
y += incy;
}
}
}

void display(void) {
glClear(GL_COLOR_BUFFER_BIT);

// Replace these with your desired line coordinates


bresenhamLine(50, 100, 200, 250); // Example line

glFlush();
}

int main(int argc, char** argv) {


glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500, 500);
glutInitWindowPosition(100, 100);
glutCreateWindow("Bresenham Line Drawing");
init();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}

You might also like