21X505 Problem Sheet3

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

PSG COLLEGE OF TECHNOLOGY

DEPARTMENT OF APPLIED MATHEMATICS AND COMPUTATIONAL SCIENCES


BSc COMPUTER SYSTEMS AND DESIGN V SEM
21X505 COMPUTER GRAPHICS LABORATORY
PROBLEM SHEET 3 12.7.2024

1. Implement positive slope line function using Digital Differential


Analyzer (DDA) algorithm to draw a line between (1, 2) to (10, 20).

#include<windows.h>
#include<stdlib.h>
#include <GL/glut.h>
#include <cmath>

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


int dx = x1 - x0;
int dy = y1 - y0;
int steps;
float x_inc, y_inc, x = x0, y = y0;

if(std::abs(dx) > std::abs(dy))


steps = std::abs(dx);
else
steps = std::abs(dy);

x_inc = dx / (float)steps;
y_inc = dy / (float)steps;

glPointSize(2.0);
glColor3f(1.0, 9.0, 0.0);

glBegin(GL_POINTS);
for(int i = 0; i <= steps; i++) {
glVertex2i((int)x, (int)y);
x += x_inc;
y += y_inc;
}
glEnd();
}

void display() {
glClear(GL_COLOR_BUFFER_BIT);

glBegin(GL_LINES);
glVertex3f(0.0f,100.0f,100.0f);
glVertex3f(0.0f,-100.0f,-100.0f);
glEnd();

glBegin(GL_LINES);
glVertex3f(100.0f,0.0f,100.0f);
glVertex3f(-100.0f,0.0f,-100.0f);
glEnd();

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-100, 100, -100, 100);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

dda_line(1, 2, 10, 20);

glFlush();
}

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


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

2. Consider the line from (10, 4) to (20, 12), use DDA line algorithm to
rasterize this line with negative slope. Evaluate and tabulate all the steps
involved.
#include <windows.h>
#include <GL/glut.h>
#include <cmath>

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


int dx = x1 - x0;
int dy = y1 - y0;
int steps;
float x_inc, y_inc, x = x0, y = y0;

if(std::abs(dx) > std::abs(dy))


steps = std::abs(dx);
else
steps = std::abs(dy);

x_inc = dx / (float)steps;
y_inc = -(dy / (float)steps);

glPointSize(2.0);
glColor3f(1.0, 9.0, 0.0);

glBegin(GL_POINTS);

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


glVertex2i((int)x, (int)y);
x += x_inc;
y += y_inc;
}

glEnd();
}

void display() {
glClear(GL_COLOR_BUFFER_BIT);

glBegin(GL_LINES);
glVertex3f(0.0f,100.0f,100.0f);
glVertex3f(0.0f,-100.0f,-100.0f);
glEnd();

glBegin(GL_LINES);
glVertex3f(100.0f,0.0f,100.0f);
glVertex3f(-100.0f,0.0f,-100.0f);
glEnd();

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-100, 100, -100, 100);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

dda_line(10, 4, 20, 12);

glFlush();
}

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


glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(800, 600);
glutInitWindowPosition(100, 100);
glutCreateWindow("DDA Line with Negative Slope");
glutDisplayFunc(display);
glutMainLoop();
return 0;
}

3. If a line is drawn from (2, 3) to (6, 15) with use of DDA. How many
points will be needed to generate such line?

#include<windows.h>
#include<stdlib.h>
#include <GL/glut.h>
#include <cmath>
#include <iostream>

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


int dx = x1 - x0;
int dy = y1 - y0;
int steps;
float x_inc, y_inc, x = x0, y = y0;

if(std::abs(dx) > std::abs(dy))


steps = std::abs(dx);
else
steps = std::abs(dy);

x_inc = dx / (float)steps;
y_inc = dy / (float)steps;

glPointSize(2.0);
glColor3f(1.0, 9.0, 0.0);

int count=0;
glBegin(GL_POINTS);
for(int i = 0; i <= steps; i++) {
glVertex2i((int)x, (int)y);
x += x_inc;
y += y_inc;
std::cout<<round(x)<<" , "<<round(y)<<std::endl;
count+=1;
}
std::cout<<"we need "<<count<<" points to generate this line"<<
std::endl;
glEnd();
}

void display() {
glClear(GL_COLOR_BUFFER_BIT);

glBegin(GL_LINES);
glVertex3f(0.0f,100.0f,100.0f);
glVertex3f(0.0f,-100.0f,-100.0f);
glEnd();

glBegin(GL_LINES);
glVertex3f(100.0f,0.0f,100.0f);
glVertex3f(-100.0f,0.0f,-100.0f);
glEnd();

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-50, 50, -50, 50);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

dda_line(2,3,6,15);

glFlush();
}

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


glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500, 500);
glutInitWindowPosition(100, 100);
glutCreateWindow("DDA Line");
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
4. A line has a starting point (1,7) and ending point (11,17). Apply the
Digital Differential Analyzer algorithm to plot a line.

#include<windows.h>
#include<stdlib.h>
#include <GL/glut.h>
#include <cmath>
#include <iostream>

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


int dx = x1 - x0;
int dy = y1 - y0;
int steps;
float x_inc, y_inc, x = x0, y = y0;

if(std::abs(dx) > std::abs(dy))


steps = std::abs(dx);
else
steps = std::abs(dy);

x_inc = dx / (float)steps;
y_inc = dy / (float)steps;

glPointSize(2.0);
glColor3f(1.0, 9.0, 0.0);
int count=0;
glBegin(GL_POINTS);
for(int i = 0; i <= steps; i++) {
glVertex2i((int)x, (int)y);
x += x_inc;
y += y_inc;
std::cout<<round(x)<<" , "<<round(y)<<std::endl;
count+=1;
}
std::cout<<"we need "<<count<<" points to generate this line"<<
std::endl;
glEnd();
}

void display() {
glClear(GL_COLOR_BUFFER_BIT);

glBegin(GL_LINES);
glVertex3f(0.0f,100.0f,100.0f);
glVertex3f(0.0f,-100.0f,-100.0f);
glEnd();

glBegin(GL_LINES);
glVertex3f(100.0f,0.0f,100.0f);
glVertex3f(-100.0f,0.0f,-100.0f);
glEnd();
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-100, 100, -100, 100);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

dda_line(1,7,11,17);

glFlush();
}

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


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

You might also like