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

Assignment2

The document contains source code for 3 line drawing algorithms: 1. The DDA line drawing algorithm source code uses different calculations to draw the line based on the slope being greater than, less than, or equal to 1. 2. The Bresenham line drawing algorithm source code uses a decision parameter p to determine whether to plot pixels above or below the line for each step. 3. The Cohen-Sutherland line clipping algorithm source code gets codes for points to determine if they are inside or outside the clipping window, clips the line segments if they are outside, and redraws the clipped line.

Uploaded by

Geerbani Shashi
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views

Assignment2

The document contains source code for 3 line drawing algorithms: 1. The DDA line drawing algorithm source code uses different calculations to draw the line based on the slope being greater than, less than, or equal to 1. 2. The Bresenham line drawing algorithm source code uses a decision parameter p to determine whether to plot pixels above or below the line for each step. 3. The Cohen-Sutherland line clipping algorithm source code gets codes for points to determine if they are inside or outside the clipping window, clips the line segments if they are outside, and redraws the clipped line.

Uploaded by

Geerbani Shashi
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 6

1.

DDA Line Drawing Algorithm Source Code

#include<stdio.h>
#include <GL/gl.h>
#include <GL/glut.h>

float x1,y1,x2,y2,m,i,j;
float dx,dy;
void display(void)
{
glClear (GL_COLOR_BUFFER_BIT);

glEnd();

glColor3f (0.0, 1.0, 0.0);


glBegin(GL_POINTS);

if(m>0 && m<=1)


{
while(x1<=x2 && y1<=y2)
{
x1=x1+1;
y1=y1+m;
glVertex3f(x1/100,y1/100,0.0);
printf("%f %f\n",x1,y1);

}
}
else if(m>1)
{
while(x1<=x2 && y1<=y2)
{
x1=x1+(1/m);
y1=y1+1;
glVertex3f(x1/100,y1/100,0.0);
printf("%f %f\n",x1,y1);
}
}

else if(m>-1 && m<=0)


{
while(x1>=x2 && y1>=y2)
{
x1=x1-1;
y1=y1-m;
glVertex3f(x1/100,y1/100,0.0);
printf("%f %f\n",x1,y1);
}
}
else if(m<-1)

while(x1>=x2 && y1>=y2)


{
x1=x1-(1/m);
y1=y1-1;
glVertex3f(x1/100,y1/100,0.0);
printf("%f %f\n",x1,y1);
}
}

glEnd();

glFlush ();
}
void init (void)
{

glClearColor (0.0, 0.0, 0.0, 0.0);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
}

int main(int argc, char** argv)


{

printf("Enter value of X1 :");


scanf("%f",&x1);
printf("Enter value of y1 :");
scanf("%f",&y1);
printf("Enter value of X2 :");
scanf("%f",&x2);
printf("Enter value of Y2 :");
scanf("%f",&y2);
dx=x2-x1;
dy=y2-y1;
m=dy/dx;

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

-----------------------------------------------------------------------------------
-----------------------------------------
-----------------------------------------------------------------------------------
-----------------------------------------
-----------------------------------------------------------------------------------
-----------------------------------------

2. Bresenham Line Drawing Algorithm Source Code

#include <stdio.h>
#include <GL/gl.h>
#include <GL/glut.h>
float x1,y1,x2,y2,m,i,j,p;
int dx=0,dy=0;
void display(void)
{

glClear (GL_COLOR_BUFFER_BIT);

glEnd();

glColor3f (0.0, 1.0, 0.0);


glBegin(GL_POINTS);
p=(2*dy)-dx;
for(i=x1,j=y1; i<=x2,j<=y2; )
{
if(p>=0)
{
i=i+1;
j=j+1;
if((i>x2)||(j>y2))
{
break;
}
printf("%0.2f %0.2f\n",i,j);
glVertex3f ((i/100), (j/100), 0.0);
p=p+(2*dy)-(2*dx);
}
else if(p<0)
{
i=i+1;
if((i>x2)||(j>y2))
{
break;
}
printf("%0.2f %0.2f\n",i,j);
glVertex3f ((i/100), (j/100), 0.0);
p=p+(2*dy);
}
}
glEnd();

glFlush ();
}
void init (void)
{

glClearColor (0.0, 0.0, 0.0, 0.0);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
}

int main(int argc, char** argv)


{

printf("Enter first point: ");


scanf("%f %f",&x1,&y1);
printf("Enter second point: ");
scanf("%f %f",&x2,&y2);
dx=x2-x1;
dy=y2-y1;
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize (500, 500);
glutInitWindowPosition (100, 100);
glutCreateWindow ("bresenham line drawing algo 2018331502");
init ();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}

-----------------------------------------------------------------------------------
----------------------------------
-----------------------------------------------------------------------------------
----------------------------------
-----------------------------------------------------------------------------------
----------------------------------

3. Cohen Sutherland Line Clipping Algorithm Source Code

#include<windows.h>
#include<GL/glu.h>
#include<GL/glut.h>

GLfloat xMin=-0.5,xMax=0.5,yMin=-0.5,yMax=0.5;
GLfloat x1=-0.8,y1=-0.6,x2=0.7,y2=0.4;

int Left=1,Right=2,Bot=4,Top=8;
int C1,C2;
int Clip_Flag = 0, Flag = 1;;

int Get_Code(GLfloat x,GLfloat y)


{
int Code = 0;
if(x<xMin)
Code = Code | Left;
if(x>xMax)
Code = Code | Right;
if(y<yMin)
Code = Code | Bot;
if(y>yMax)
Code = Code | Top;
return Code;
}

void Clip()
{
int C;
GLfloat x,y;
if(C1)
C = C1;
else
C = C2;
if(C & Left)
{
x = xMin;
y = y1+(y2-y1)*((xMin-x1)/(x2-x1));
}
if(C & Right)
{
x = xMax;
y = y1+(y2-y1)*((xMax-x1)/(x2-x1));
}
if(C & Bot)
{
y = yMin;
x = x1+(x2-x1)*((yMin-y1)/(y2-y1));
}
if(C & Top)
{
y = yMax;
x = x1+(x2-x1)*((yMax-y1)/(y2-y1));
}

if(C == C1)
{
x1 = x;
y1 = y;
}
else
{
x2 = x;
y2 = y;
}
}

void Draw()
{
glClear(GL_COLOR_BUFFER_BIT);

glColor3f(1,1,1);
glBegin(GL_LINE_LOOP);
glVertex2f(xMin,yMin);
glVertex2f(xMax,yMin);
glVertex2f(xMax,yMax);
glVertex2f(xMin,yMax);
glEnd();

glColor3f(1,0,0);
if(Flag == 1)
{
glBegin(GL_LINES);
glVertex2f(x1,y1);
glVertex2f(x2,y2);
glEnd();
}

while(1 & Clip_Flag == 1)


{
C1 = Get_Code(x1,y1);
C2 = Get_Code(x2,y2);
if((C1|C2) == 0)
break;
else if((C1&C2)!=0)
{
Flag = 0;
break;
}
else
Clip();
}
glFlush();
}

void Key(unsigned char ch,int x,int y)


{
Clip_Flag = 1;
glutPostRedisplay();
}

int main(int argC,char *argV[])


{
glutInit(&argC,argV);
glutInitWindowSize(500,500);
glutInitWindowPosition(100,100);
glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
glutCreateWindow("Cohen-Sutherland Algorithm 2018331502");
glutDisplayFunc(Draw);
glutKeyboardFunc(Key);
glutMainLoop();
return 0;
}

-----------------------------------------------------------------------------------
----------------------------------
-----------------------------------------------------------------------------------
----------------------------------
-----------------------------------------------------------------------------------
----------------------------------

You might also like