0% found this document useful (0 votes)
122 views10 pages

21X505 Problem Sheet3

Uploaded by

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

21X505 Problem Sheet3

Uploaded by

chitteshpsg
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 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

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy