0% found this document useful (0 votes)
6 views

DDA.cpp

This document is a C++ program that utilizes OpenGL and GLUT to implement various line drawing algorithms, including DDA, dotted, dashed, and solid lines. It provides a menu-driven interface for users to select different line types to be drawn on the screen. The program initializes the OpenGL environment, sets up the display, and handles user input through a right-click menu.

Uploaded by

rudrashete512204
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)
6 views

DDA.cpp

This document is a C++ program that utilizes OpenGL and GLUT to implement various line drawing algorithms, including DDA, dotted, dashed, and solid lines. It provides a menu-driven interface for users to select different line types to be drawn on the screen. The program initializes the OpenGL environment, sets up the display, and handles user input through a right-click menu.

Uploaded by

rudrashete512204
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/ 14

#include <GL/glut.

h>

#include<cmath>

#include <iostream>

Using namespace std;

Int choice =0;

Void myInit(void){

glClearColor(1.0,1.0,1.0,1.0);

glColor3f(0.0f,0.0f,0.0f);

glMatrixMode(GL_PROJECTION);

glLoadIdentity();

gluOrtho2D(-320.0,320.0,-240.0,240.0);

Float sign(float x){

If(x<0){

Return-1;

Else if(x==0){

Return 1;

Else{

Return 1;

Void ddaLine(int x1, int y1, int x2, int y2) {

If(x1==x2&&y1==y2){

glPointSize(2.0);
glBegin(GL_POINTS);

glVertex2i(x1, y1);

glEnd();

glFlush();

cout<<”Error – Origin and Destination are same”<<endl;

return;

Int dx = x2 – x1;

Int dy = y2 – y1;

Int length;

If(abs(dx)>abs(dy)){

Length=dx;

Else{

Length=dy;

Float DX = dx / float(length);

Float DY = dy / float(length);

Float x = x1;

Float y = y1;

Sign(DX);

Sign(DY);

glPointSize(2.0);

glBegin(GL_POINTS);
for (int I = 0; I <= length; i++) {

glColor3f(0.0, 0.0, 0.0);

glVertex2i(round(x), round(y));

x +=(DX);

y +=(DY);

glEnd();

glFlush();

Void SolidddaLine(int x1, int y1, int x2, int y2) {

If(x1==x2&&y1==y2){

glPointSize(5.0);

glBegin(GL_POINTS);

glVertex2i(x1, y1);

glEnd();

glFlush();

cout<<”Error – Origin and Destination are same”<<endl;

return;

Int dx = x2 – x1;
Int dy = y2 – y1;

Int length;

If(abs(dx)>abs(dy)){

Length=dx;

Else{

Length=dy;

Float DX = dx / float(length);

Float DY = dy / float(length);

Float x = x1;

Float y = y1;

Sign(DX);

Sign(DY);

glPointSize(5.0);

glBegin(GL_POINTS);

for (int I = 0; I <= length; i++) {

glColor3f(0.0, 0.0, 0.0);

glVertex2i(round(x), round(y));

x +=(DX);

y +=(DY);
}

glEnd();

glFlush();

Void dotted_ddaLine(int x1, int y1, int x2, int y2) {

If(x1==x2&&y1==y2){

glPointSize(2.0);

glBegin(GL_POINTS);

glVertex2i(x1, y1);

glEnd();

glFlush();

cout<<”Error – Origin and Destination are same”<<endl;

return;

Int dx = x2 – x1;

Int dy = y2 – y1;

Int length;

If(abs(dx)>abs(dy)){

Length=dx;

Else{

Length=dy;

Float DX = dx / float(length);
Float DY = dy / float(length);

Float x = x1;

Float y = y1;

Sign(DX);

Sign(DY);

glBegin(GL_POINTS);

for (int I = 0; I <= length; i++) {

if(int(x)%2==0&&int(y)%2==0){

glColor3f(0.0, 0.0, 0.0);

glVertex2i(round(x), round(y));

}else{

glColor3f(1.0, 1.0, 1.0);

glVertex2i(round(x), round(y));

X +=(DX);

Y +=(DY);

glEnd();

glFlush();

Void dashed_ddaLine(int x1, int y1, int x2, int y2) {

If(x1==x2&&y1==y2){
glPointSize(2.0);

glBegin(GL_POINTS);

glVertex2i(x1, y1);

glEnd();

glFlush();

cout<<”Error – Origin and Destination are same”<<endl;

return;

Int dx = x2 – x1;

Int dy = y2 – y1;

Int length;

If(abs(dx)>abs(dy)){

Length=dx;

Else{

Length=dy;

Float DX = dx / float(length);

Float DY = dy / float(length);

Float x = x1;

Float y = y1;

Sign(DX);

Sign(DY);

glPointSize(2.0);

glBegin(GL_POINTS);
int counter = 0;

for (int I = 0; I <= length; i++) {

if(counter<10){

glColor3f(0.0, 0.0, 0.0);

glVertex2i(round(x), round(y));

counter++;

}else if(counter>=10&&counter<20){

glColor3f(1.0, 1.0, 1.0);

glVertex2i(round(x), round(y));

counter++;

}else{

Counter=0;

X +=(DX);

Y +=(DY);

glEnd();

glFlush();

Void centreDottedddaLine(int x1, int y1, int x2, int y2) {

If(x1==x2&&y1==y2){

glPointSize(2.0);

glBegin(GL_POINTS);

glVertex2i(x1, y1);
glEnd();

glFlush();

cout<<”Error – Origin and Destination are same”<<endl;

return;

Int dx = x2 – x1;

Int dy = y2 – y1;

Int length;

If(abs(dx)>abs(dy)){

Length=dx;

Else{

Length=dy;

Float DX = dx / float(length);

Float DY = dy / float(length);

Float x = x1;

Float y = y1;

Sign(DX);

Sign(DY);

glPointSize(2.0);

glBegin(GL_POINTS);

int counter = 0;

for (int I = 0; I <= length; i++) {

if(counter<10||counter==15){
glColor3f(0.0, 0.0, 0.0);

glVertex2i(round(x), round(y));

counter++;

}else if(counter>=10&&counter<20&&counter!=15){

glColor3f(1.0, 1.0, 1.0);

glVertex2i(round(x), round(y));

counter++;

}else{

Counter=0;

X +=(DX);

Y +=(DY);

glEnd();

glFlush();

Void plotAxis(){

ddaLine(-320,0,320,0);

ddaLine(0,-240,0,240);

Void display() {

glClear(GL_COLOR_BUFFER_BIT);

/*ddaLine(100, 75,200, 150);

Dotted_ddaLine(-320, -50, -20, -50);


Dashed_ddaLine(-200, 100, -50, 100);

SolidddaLine(50, 50, 100, 50);

centreDottedddaLine(-250,-50,50,-250);

ddaLine(25, -150, 275, -150);//boat starts here

ddaLine(50, -200, 250, -200);

ddaLine(50, -200, 25, -150);

ddaLine(250, -200, 275, -150);

ddaLine(150, -150, 150, -50);

ddaLine(150, -112, 225, -112);

ddaLine(150, -50, 225, -112);//boat ends here */

plotAxis();

switch(choice){

case 1:

ddaLine(100, 75,200, 150);

break;

case 2:

dotted_ddaLine(-320, -50, -20, -50);

break;

case 3:

dashed_ddaLine(-200, 100, -50, 100);

break;

case 4:

SolidddaLine(50, 50, 100, 50);

Break;

Case 5:

centreDottedddaLine(-250,-50,50,-250);
break;

case 6:

ddaLine(25, -150, 275, -150);//boat starts here

ddaLine(50, -200, 250, -200);

ddaLine(50, -200, 25, -150);

ddaLine(250, -200, 275, -150);

ddaLine(150, -150, 150, -50);

ddaLine(150, -112, 225, -112);

ddaLine(150, -50, 225, -112);//boat ends here

break;

glFlush();

glutSwapBuffers();

Void processMenuEvents(int option) {

Switch (option) {

Case 1:

Choice = 1;

Break;

Case 2:

Choice = 2;

Break;

Case 3:
Choice =3;

Break;

Case 4:

Choice=4;

Break;

Case 5:

Choice=5;

Break;

Case 6:

Choice=6;

Break;

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

glutInit(&argc, argv);

glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);

glutInitWindowSize(640, 480);

glutCreateWindow(“DDA Line Drawing”);

myInit();

glutCreateMenu(processMenuEvents);

glutAddMenuEntry(“DDA Line”, 1);

glutAddMenuEntry(“Dotted DDA Line”, 2);

glutAddMenuEntry(“Dashed DDA Line”, 3);

glutAddMenuEntry(“Solid DDA Line”, 4);


glutAddMenuEntry(“Centre Dotted DDA Line”, 5);

glutAddMenuEntry(“Boat”, 6);

glutAttachMenu(GLUT_RIGHT_BUTTON);

glutDisplayFunc(display);

glutMainLoop();

return 0;

You might also like