0% found this document useful (0 votes)
7 views18 pages

Assignment 2 CG

The document contains C++ code for rendering various types of lines (simple, dotted, dashed, solid) using OpenGL and GLUT. It includes implementations for both DDA (Digital Differential Analyzer) and Bresenham's line algorithms, along with mouse and keyboard event handling for user interaction. The code allows users to draw lines on a graphical window based on mouse clicks and keyboard inputs.

Uploaded by

pkumbarkar100
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)
7 views18 pages

Assignment 2 CG

The document contains C++ code for rendering various types of lines (simple, dotted, dashed, solid) using OpenGL and GLUT. It includes implementations for both DDA (Digital Differential Analyzer) and Bresenham's line algorithms, along with mouse and keyboard event handling for user interaction. The code allows users to draw lines on a graphical window based on mouse clicks and keyboard inputs.

Uploaded by

pkumbarkar100
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/ 18

Assignment No.

DDA:

#include <GL/freeglut.h>

#include <GL/gl.h>

#include <iostream>

using namespace std;

void displaypoint(float x,float y){

glColor3f(1.0,1.0,1.0);

glBegin(GL_POINTS);

glVertex2f(x,y);

glEnd();

void SimpleLine(float xs,float ys,float xe, float ye){

float dx=xe-xs;

float dy=ye-ys;

int steps;

if(abs(dx)>abs(dy)){

steps=abs(dx);

else{

steps=abs(dy);

float xinc=dx/(float)steps;

float yinc=dy/(float)steps;

float x=xs;

float y=ys;

displaypoint(x,y);

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

x=x+xinc;

y=y+yinc;

displaypoint(x,y);

glFlush();
}

void DottedLine(float xs,float ys,float xe, float ye){

float dx=xe-xs;

float dy=ye-ys;

int steps;

if(abs(dx)>abs(dy)){

steps=abs(dx);

else{

steps=abs(dy);

float xinc=dx/(float)steps;

float yinc=dy/(float)steps;

float x=xs;

float y=ys;

displaypoint(x,y);

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

x=x+xinc;

y=y+yinc;

if(i%5==0){

displaypoint(x,y);

glFlush();

void DashedLine(float xs,float ys,float xe, float ye){

float dx=xe-xs;

float dy=ye-ys;

int steps;

if(abs(dx)>abs(dy)){

steps=abs(dx);

}
else{

steps=abs(dy);

float xinc=dx/(float)steps;

float yinc=dy/(float)steps;

float x=xs;

float y=ys;

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

x=x+xinc;

y=y+yinc;

if(i%15!=0){

displaypoint(x,y);

glFlush();

void displaypoint1(float x,float y){

glColor3f(1.0,1.0,1.0);

glPointSize(2);

glBegin(GL_POINTS);

glVertex2f(x,y);

glEnd();

void SolidLine(float xs,float ys,float xe, float ye){

float dx=xe-xs;

float dy=ye-ys;

int steps;

if(abs(dx)>abs(dy)){

steps=abs(dx);

else{

steps=abs(dy);
}

float xinc=dx/(float)steps;

float yinc=dy/(float)steps;

float x=xs;

float y=ys;

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

x=x+xinc;

y=y+yinc;

displaypoint1(x,y);

glFlush();

void renderFunction()

glClearColor(0.0, 0.0, 0.0, 0.0);

glClear(GL_COLOR_BUFFER_BIT);

glOrtho(-250.0, 250.0, -250.0, 250.0, -250.0, 250.0);

SimpleLine(-250,0,250,0);

SimpleLine(0,-250,0,250);

SimpleLine(110,110,220,220);

DottedLine(-110,110,-220,220);

DashedLine(-110,-110,-220,-220);

SolidLine(110,-110,220,-220);

glFlush();

int main(int argc, char** argv)

glutInit(&argc, argv);

glutInitDisplayMode(GLUT_SINGLE);

glutInitWindowSize(500,500);

glutInitWindowPosition(100,100);

glutCreateWindow("DDA Line");

glutDisplayFunc(renderFunction);

glutMainLoop();
return 0;

Bresenham:
#include <GL/freeglut.h>

#include <GL/gl.h>

#include <iostream>

using namespace std;

void displaypoint(float x,float y){

glColor3f(1.0,1.0,1.0);

glBegin(GL_POINTS);

glVertex2f(x,y);

glEnd();

void BLA(int xs, int ys,int xe,int ye){

int p;

int dx=abs(xe-xs);

int dy=abs(ye-ys);

int x=xs;

int y=ys;

int xinc=1;

int yinc=1;

if(xs>xe){

xinc=-1;
}

if(ys>ye){

yinc=-1;

displaypoint(x,y);

if(dx==0){

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

y=y+yinc;

displaypoint(x,y);

return;

if(dy==0){

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

x=x+xinc;

displaypoint(x,y);

return;

if(dx>dy){

p=2*dy-dx;

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

if(p>0){

y=y+yinc;

p=p+2*(dy-dx);

}else{

p=p+2*dy;

x=x+xinc;

displaypoint(x,y);

}else{

p=2*dx-dy;

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

if(p>0){
x=x+xinc;

p=p+2*(dx-dy);

}else{

p=p+2*dx;

y=y+yinc;

displaypoint(x,y);

void dotted_BLA(int xs, int ys,int xe,int ye){

int p;

int dx=abs(xe-xs);

int dy=abs(ye-ys);

int x=xs;

int y=ys;

int xinc=1;

int yinc=1;

if(xs>xe){

xinc=-1;

if(ys>ye){

yinc=-1;

displaypoint(x,y);

if(dx==0){

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

y=y+yinc;

if(i%3==0){

displaypoint(x,y);

return;

if(dy==0){
for(int i=0;i<dx;i++){

x=x+xinc;

if(i%3==0){

displaypoint(x,y);

return;

if(dx>dy){

p=2*dy-dx;

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

if(p>0){

y=y+yinc;

p=p+2*(dy-dx);

}else{

p=p+2*dy;

x=x+xinc;

if(i%3==0){

displaypoint(x,y);

}else{

p=2*dx-dy;

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

if(p>0){

x=x+xinc;

p=p+2*(dx-dy);

}else{

p=p+2*dx;

y=y+yinc;

if(i%3==0){

displaypoint(x,y);
}

void dashed_BLA(int xs, int ys,int xe,int ye){

int p;

int dx=abs(xe-xs);

int dy=abs(ye-ys);

int x=xs;

int y=ys;

int xinc=1;

int yinc=1;

if(xs>xe){

xinc=-1;

if(ys>ye){

yinc=-1;

displaypoint(x,y);

if(dx==0){

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

y=y+yinc;

if(i%15!=0){

displaypoint(x,y);

return;

if(dy==0){

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

x=x+xinc;

if(i%15!=0){

displaypoint(x,y);

}
}

return;

if(dx>dy){

p=2*dy-dx;

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

if(p>0){

y=y+yinc;

p=p+2*(dy-dx);

}else{

p=p+2*dy;

x=x+xinc;

if(i%15!=0){

displaypoint(x,y);

}else{

p=2*dx-dy;

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

if(p>0){

x=x+xinc;

p=p+2*(dx-dy);

}else{

p=p+2*dx;

y=y+yinc;

if(i%15!=0){

displaypoint(x,y);

void displaypoint1(float x,float y){


glColor3f(1.0,1.0,1.0);

glPointSize(2);

glBegin(GL_POINTS);

glVertex2f(x,y);

glEnd();

void solid_BLA(int xs, int ys,int xe,int ye){

int p;

int dx=abs(xe-xs);

int dy=abs(ye-ys);

int x=xs;

int y=ys;

int xinc=1;

int yinc=1;

if(xs>xe){

xinc=-1;

if(ys>ye){

yinc=-1;

displaypoint1(x,y);

if(dx==0){

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

y=y+yinc;

displaypoint1(x,y);

return;

if(dy==0){

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

x=x+xinc;

displaypoint1(x,y);

return;

}
if(dx>dy){

p=2*dy-dx;

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

if(p>0){

y=y+yinc;

p=p+2*(dy-dx);

}else{

p=p+2*dy;

x=x+xinc;

displaypoint1(x,y);

}else{

p=2*dx-dy;

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

if(p>0){

x=x+xinc;

p=p+2*(dx-dy);

}else{

p=p+2*dx;

y=y+yinc;

displaypoint1(x,y);

void renderFunction()

glClearColor(0.0, 0.0, 0.0, 0.0);

glClear(GL_COLOR_BUFFER_BIT);

glOrtho(-250.0, 250.0, -250.0, 250.0, -250.0, 250.0);

BLA(-250,0,250,0);

BLA(0,-250,0,250);

BLA(110,110,220,220);
dotted_BLA(-110,110,-220,220);

dashed_BLA(-110,-110,-220,-220);

solid_BLA(110,-110,220,-220);

glFlush();

int main(int argc, char** argv)

glutInit(&argc, argv);

glutInitDisplayMode(GLUT_SINGLE);

glutInitWindowSize(500,500);

glutInitWindowPosition(100,100);

glutCreateWindow("BLA");

glutDisplayFunc(renderFunction);

glutMainLoop();

return 0;

Mouse And KeyBoard Triggering:

#include <GL/freeglut.h>

#include <GL/gl.h>

#include <iostream>
using namespace std;

float startX, startY, endX, endY;

int ch ;

void displaypoint(float x, float y) {

glColor3f(1.0, 1.0, 1.0);

glBegin(GL_POINTS);

glVertex2f(x, y);

glEnd();

void SimpleLine(float xs, float ys, float xe, float ye) {

float dx = xe - xs;

float dy = ye - ys;

int steps;

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

float xinc = dx / (float)steps;

float yinc = dy / (float)steps;

float x = xs;

float y = ys;

displaypoint(x, y);

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

x += xinc;

y += yinc;

displaypoint(x, y);

glFlush();

void DottedLine(float xs, float ys, float xe, float ye) {

float dx = xe - xs;

float dy = ye - ys;
int steps;

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

float xinc = dx / (float)steps;

float yinc = dy / (float)steps;

float x = xs;

float y = ys;

displaypoint(x, y);

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

x += xinc;

y += yinc;

if (i % 5 == 0) {

displaypoint(x, y);

glFlush();

void DashedLine(float xs, float ys, float xe, float ye) {

float dx = xe - xs;

float dy = ye - ys;

int steps;

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

float xinc = dx / (float)steps;

float yinc = dy / (float)steps;

float x = xs;

float y = ys;

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

x += xinc;

y += yinc;

if (i % 15 != 0) {

displaypoint(x, y);

glFlush();
}

void SolidLine(float xs, float ys, float xe, float ye) {

glLineWidth(2.0);

float dx = xe - xs;

float dy = ye - ys;

int steps;

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

float xinc = dx / (float)steps;

float yinc = dy / (float)steps;

float x = xs;

float y = ys;

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

x += xinc;

y += yinc;

displaypoint(x, y);

glFlush();

void mouseHandler(int button, int state, int x, int y) {

static int pt = 0;

if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) {

if (pt == 0) {

startX = x;

startY = 500 - y;

pt = 1;

else {

endX = x;

endY = 500 - y;

switch (ch) {

case 1:

SimpleLine(startX, startY, endX, endY);

break;

case 2:
DottedLine(startX, startY, endX, endY);

break;

case 3:

DashedLine(startX, startY, endX, endY);

break;

default:

SolidLine(startX, startY, endX, endY);

break;

else if (button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN) {

pt = 0;

glFlush();

void keyboardHandler(unsigned char key, int x, int y) {

switch (key) {

case 's':

ch = 1;

glutMouseFunc(mouseHandler);

break;

case 'd':

ch = 2;

glutMouseFunc(mouseHandler);

break;

case 'D':

ch = 3;

glutMouseFunc(mouseHandler);

break;

default:

ch = 0;

glutMouseFunc(mouseHandler);

break;

}
glutPostRedisplay();

void renderFunction() {

glClearColor(0.0, 0.0, 0.0, 0.0);

glClear(GL_COLOR_BUFFER_BIT);

glOrtho(0.0, 500.0, 0.0, 500.0, -1.0, 1.0);

SimpleLine(0, 250, 500, 250);

SimpleLine(250, 0, 250, 500);

glutKeyboardFunc(keyboardHandler);

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

glutInit(&argc, argv);

glutInitDisplayMode(GLUT_SINGLE);

glutInitWindowSize(500, 500);

glutInitWindowPosition(100, 100);

glutCreateWindow("BLA");

glutDisplayFunc(renderFunction);

glutMouseFunc(mouseHandler);

glutKeyboardFunc(keyboardHandler);

glutMainLoop();

return 0;

You might also like