You are on page 1of 71

www.Vidyarthiplus.

com

Aravindan P 30308205012

IMPLEMENTATION OF DDA LINE ALGORITHM

DESCRIPTION: Digital Differential Analyzer (DDA) is used for linear interpolation of variables over an interval between start and end point of a line. Simplest implementation the DDA algorithm interpolates values in interval [(xstart, ystart), (xend, yend)] by computing for each xi the equations xi = xi1+1, yi = yi1 + y/x, Where x = xend xstart and y = yend ystart.

CODING:

#include <graphics.h> #include <stdlib.h> #include <stdio.h> #include <conio.h>

/* include the necessary header files*/

void draw(int xa,int ya,int xb,int yb); void main() { int xa,ya,xb,yb; clrscr(); printf("Line DDA algorithm"); printf("\n Enter the value of xa, ya:"); scanf("%d%d",&xa,&ya); printf("\n Enter the value of xb, yb:"); scanf("%d%d",&xb,&yb); draw(xa,ya,xb,yb); } void draw(int xa,int ya,int xb,int yb) { int xin,yin,x,y,dx,dy,steps,k; int gdriver=DETECT,gmode,errorcode; initgraph(&gdriver,&gmode, "c:\\tc\\bgi") errorcode=graphresult(); /* request auto detection */ /* initialize graphics and local variables */ /* read result of initialization */ /* an error occurred */ [ ] www.Vidyarthiplus.com

www.Vidyarthiplus.com

Aravindan P 30308205012

if (errorcode!=grOk) { printf("Graphics error: %s\n", grapherrormsg(errorcode)); printf("Press any key to halt:"); getch(); exit(1); } dx=xb-xa; dy=yb-ya; if(abs(dx)>abs(dy)) { steps=abs(dx); } else { steps=abs(dy); } xin=dx/steps; yin=dy/steps; x=xa; y=ya; putpixel(x,y,1); /* draw the first pixel for the line*/ /* for each value of the condition variable, */ /* if the condition is satisfied */ /* calculate the value of the condition variable*/

for(k=1;k<=steps;k++) { x=x+xin; y=y+yin; putpixel(x,y,1); } getch(); closegraph(); }

/* calculate the values of (x,y) and draw the pixel*/

/* clean up */

[ ] www.Vidyarthiplus.com

www.Vidyarthiplus.com

Aravindan P 30308205012

OUTPUT:

Line DDA algorithm Enter the value of xa, ya: 400 260 Enter the value of xb, yb: 456 23

RESULT: Thus the DDA line drawing algorithm was successfully executed and the output is drawn and verified.

[ ] www.Vidyarthiplus.com

www.Vidyarthiplus.com

Aravindan P 30308205012

IMPLEMENTATION OF BRESENHAMS LINE ALGORITHM

DESCRIPTION: The Bresenham line algorithm is an algorithm which determines which points in an n-dimensional raster should be plotted in order to form a close approximation to a straight line between two given points. The endpoints of the line are the pixels at (x0, y0) and (x1, y1), where the first coordinate of the pair is the column and the second is the row.

CODING:

#include <graphics.h> #include <stdlib.h> #include <stdio.h> #include <conio.h>

/* include the necessary header files*/

void draw(int xa, int ya, int xb, int yb); void main() { int xa, ya, xb, yb; clrscr(); printf("Bresenhnams algorithm"); /* get the coordinates of the line*/

printf("\n Enter the value of xa, ya:"); scanf("%d%d",&xa,&ya); printf("\n Enter the value of xb, yb:"); scanf("%d%d",&xb,&yb); draw(xa,ya,xb,yb); } void draw(int xa, int ya, int xb, int yb) { int x,y,dx,dy,xend,p; /* request auto detection */ /* initialize graphics and local variables */ /* read result of initialization */

int gdriver=DETECT,gmode,errorcode; initgraph(&gdriver,&gmode,"c:\\tc\\bgi"); errorcode=graphresult();

/* an error occurred */ [ ] www.Vidyarthiplus.com

www.Vidyarthiplus.com

Aravindan P 30308205012

if(errorcode!=grOk) { printf("Graphics error: %s\n", grapherrormsg(errorcode)); printf("Press any key to halt:"); getch(); exit(1); } dx=xb-xa; dy=yb-ya; p=2*dy-dx; if(xa>xb) { x=xb; y=yb; xend=xa; } else if(xb>xa) { x=xa; y=ya; xend=xb; } putpixel(x,y,1); while(x<xend) { x=x+1; if(p<0) { p=p+2*dy; } else { [ ] www.Vidyarthiplus.com /* draw the pixel on the screen*/ /* depending on the control condition draw the pixels*/ /* assign the values for (x,y)*/ /* calculate the value of the condition variable*/ /* depending on the position of the coordinates*/

www.Vidyarthiplus.com

Aravindan P 30308205012

y=y+1; p=p+2*dy; } putpixel(x,y,1); } getch(); closegraph(); } /* clean up */

OUTPUT: Bresenhams algorithm Enter the value of xa, ya: 150 150 Enter the value of xb, yb: 15 150

RESULT: Thus the Bresenhams line drawing algorithm was successfully executed and the output is drawn and verified.

[ ] www.Vidyarthiplus.com

www.Vidyarthiplus.com

Aravindan P 30308205012

IMPLEMENTATION OF MIDPOINT CIRCLE ALGORITHM

DESCRIPTION: The MidPoint Circle Algorithm is an algorithm used to determine the points needed for drawing a circle. The algorithm is a variant of Bresenham's line algorithm, and is thus sometimes known as Bresenham's circle algorithm. Which starts accordingly with the circle equation x2 + y2 = r2. And with the center of the circle is located at (0, 0)

CODING:

#include<stdio.h> /* include the necessary header files*/ #include<conio.h> #include<math.h> #include<graphics.h> main() { int gd=DETECT,gin; int xcenter,ycenter,radius; int p,x,y,twox,twoy; /*request auto detect*/

initgraph(&gd,&gin,"C:\\tc\\bgi"); x=0; printf("\nEnter the radius value:"); scanf("%d",&radius); printf("Enter the center values:"); scanf("%d %d",&xcenter,&ycenter); plotpoints(xcenter,ycenter,x,y); /* call the plotpoints function*/ y=radius; p=1-radius; twox=2*x; twoy=2*y; printf("\np\tx\ty\t2x\t2y\n"); printf("\n%d\t%d\t%d\t%d\t%d\n",p,x,y,twox,twoy); [ ] www.Vidyarthiplus.com /* get the value of the radius and center values*/

www.Vidyarthiplus.com

Aravindan P 30308205012

while(x<y) /* in the conditional loop compute the value of the x and y values*/ { if(p<0) x=x+1; else { x=x+1; y=y-1; } if(p<0) p=p+2*x+1; else p=p+2*(x-y)+1; twox=2*x; twoy=2*y; printf("\n%d\t%d\t%d\t%d\t%d\n",p,x,y,twox,twoy); plotpoints(xcenter,ycenter,x,y); } getch(); return 0; } int plotpoints(int xcenter, int ycenter,int x,int y) /* plot the points of the circle as per the procedure*/ { putpixel(xcenter+x,ycenter+y,1); putpixel(xcenter-x,ycenter+y,1); putpixel(xcenter+x,ycenter-y,1); putpixel(xcenter-x,ycenter-y,1); putpixel(xcenter+y,ycenter+x,1); putpixel(xcenter-y,ycenter+x,1); putpixel(xcenter+y,ycenter-x,1); putpixel(xcenter-y,ycenter-x,1); } [ ] www.Vidyarthiplus.com

www.Vidyarthiplus.com

Aravindan P 30308205012

OUTPUT:

Enter the Radius value: 10 Enter the Centre values: 120 120

RESULT: Thus the Midpoint circle algorithm was successfully executed and the output is drawn and verified.

[ ] www.Vidyarthiplus.com

www.Vidyarthiplus.com

Aravindan P 30308205012

IMPLEMENTATION OF MIDPOINT ELLIPSE ALGORITHM

DESCRIPTION: The Midpoint Ellipse Algorithm is a method for drawing ellipses in computer graphics this method is modified from Bresenhams which starts accordingly with the ellipse equation b2x2 + a2y2 a2b2 = 0 where a is the horizontal radius and b is the vertical radius CODING:

#include<stdio.h> #include<conio.h> #include<graphics.h> #include<math.h> #include<stdlib.h>

/* include the necessary header files*/

void plotpoints(int,int,int,int); void main() { int gd=DETECT,gm; int xcenter,ycenter,rx,ry; int p,x,y,px,py,rx1,ry1,rx2,ry2; initgraph(&gd,&gm,"C:\\TC\\BGI"); printf("\n Enter the radius :"); scanf("%d %d",&rx,&ry); printf("\n Enter the xcenter and ycenter values :"); scanf("%d %d",&xcenter,&ycenter); ry1=ry*ry; rx1=rx*rx; ry2=2*ry1; rx2=2*rx1; /* Region 1 */ x=0; y=ry; [ ] www.Vidyarthiplus.com /* request auto detect*/ /* get the radius and the center values*/

www.Vidyarthiplus.com

Aravindan P 30308205012

plotpoints(xcenter,ycenter,x,y); p=(ry1-rx1*ry+(0.25*rx1)); px=0; py=rx2*y; printf("\nx\ty\tp\tpx\tpy\n");

/* for the first region calculate the condition parameter*/

printf("\n%d\t%d\t%d\t%d\t%d",x,y,p,px,py); while(px<py) { x=x+1; px=px+ry2; if(p>=0) { y=y-1; py=py-rx2; p=p+ry1+px-py; } else p=p+ry1+px; plotpoints(xcenter,ycenter,x,y); /* call the plotpoints function*/ /* if this condition is true, compute values of x and y*/

printf("\n%d\t%d\t%d\t%d\t%d",x,y,p,px,py); } /* Region 2 */ printf("\n%d\t%d\t%d\t%d\t%d",x,y,p,px,py); printf("\n\nRegion 2\n"); printf("\nx\ty\tp\tpx\tpy\n"); /* for region 2 recalculate the condition variables*/ p=(ry1*(x+0.5)*(x+0.5)+rx1*(y-1)*(y-1)-rx1*ry1); while(y>0) { y=y-1; py=py-rx2; if(p<=0) { [ ] www.Vidyarthiplus.com

www.Vidyarthiplus.com

Aravindan P 30308205012

x=x+1; px=px+ry2; } if(p>0) p=p+rx1-py; else p=p+rx1-py+px; plotpoints(xcenter,ycenter,x,y); /* draw the pixels for region 2*/

printf("\n%d\t%d\t%d\t%d\t%d",x,y,p,px,py);

getch(); closegraph(); } void plotpoints(int xcenter,int ycenter,int x,int y) { putpixel(xcenter+x,ycenter+y,6); putpixel(xcenter-x,ycenter+y,6); putpixel(xcenter+x,ycenter-y,6); putpixel(xcenter-x,ycenter-y,6); } /* plot the points of the circle as per the procedure*/

OUTPUT:

Enter the radius: 10 30 Enter the xcenter and ycenter values: 310 155

[ ] www.Vidyarthiplus.com

www.Vidyarthiplus.com

Aravindan P 30308205012

RESULT: Thus the Midpoint Ellipse algorithm was successfully executed and the output is drawn and verified.

[ ] www.Vidyarthiplus.com

www.Vidyarthiplus.com

Aravindan P 30308205012

IMPLEMENTATION OF TWO DIMENSIONAL TRANSFORMATIONS

DESCRIPTION: A transformation is any operation on a point in space (x, y) that maps the point's coordinates into a new set of coordinates (x1, y1).The Two Dimensional transformations has five operations such as Translation, Rotation, Reflection, Scaling and Shearing. CODING: #include<graphics.h> #include<stdlib.h> #include<stdio.h> #include<conio.h> int x1,x2,x3,y1,y2,y3,t,tx,sx,sy,shx,shy,ch; float rx1,rx2,rx3,ry1,ry2,ry3; float ang,theta; int main(void) { int gdriver = DETECT, gmode, errorcode; initgraph(&gdriver, &gmode,"C:\\TC\\BGI"); /* request for auto detection*/ errorcode = graphresult(); if(errorcode != grOk) /* if error occours*/ { printf("Graphics error: %s\n", grapherrormsg(errorcode)); printf("Press any key to halt:"); getch(); exit(1); } else { do{ printf("\n1.Translation\n2.Reflection\n3.Rotation\n4.Scaling\n5.Shearing\n"); printf("\nEnter Your choice"); /* get the choice from the user*/ scanf("%d",&ch); switch(ch) { case 1: printf("\n Enter all coordinates values :"); /* get the coordinate values*/ scanf("%d %d %d %d %d %d",&x1,&y1,&x2,&y2,&x3,&y3); printf("\n Before Translation "); line(x1,y1,x2,y2); line(x2,y2,x3,y3); line(x3,y3,x1,y1); printf("\n Enter the value tranlsation factor :"); [ ] www.Vidyarthiplus.com

www.Vidyarthiplus.com

Aravindan P 30308205012

scanf("%d",&tx); /* get the value for the translation factor*/ printf("\n After Translation\n "); line(x1+tx,y1,x2+tx,y2); /* draw the new translated image*/ line(x2+tx,y2,x3+tx,y3); line(x3+tx,y3,x1+tx,y1); break; case 2: printf("\n Enter all coordinates values :"); scanf("%d %d %d %d %d %d",&x1,&y1,&x2,&y2,&x3,&y3); printf("\n Before Reflection "); /* draw the image before reflection*/ line(x1,y1,x2,y2); line(x2,y2,x3,y3); line(x3,y3,x1,y1); t=abs(y1-y3); /* find the value of the reflection factor*/ printf("\n After Reflection "); line(x1,y1+10+(2*t),x2,y2+10); /* draw the reflected object*/ line(x2,y2+10,x3,y3+10); line(x3,y3+10,x1,y1+10+(2*t)); break; case 3: printf("\n Enter all coordinates values :"); scanf("%d %d %d %d %d %d",&x1,&y1,&x2,&y2,&x3,&y3); printf("\n Before Rotation "); /* get the original coordinates*/ line(x1,y1,x2,y2); line(x2,y2,x3,y3); line(x3,y3,x1,y1); printf("\n Enter the rotation angle :"); /* get the angle for rotation*/ scanf("%f",&ang); theta=((ang*3.14)/180); /* convert the given angle*/ rx1=x1*cos(theta)-y1*sin(theta); rx2=x2*cos(theta)-y2*sin(theta); rx3=x3*cos(theta)-y3*sin(theta); ry1=x1*sin(theta)+y1*cos(theta); ry2=x2*sin(theta)+y2*cos(theta); ry3=x3*sin(theta)+y3*cos(theta); printf("\n After Rotation "); /* draw the rotated image*/ line(rx1,ry1,rx2,ry2); line(rx2,ry2,rx3,ry3); line(rx3,ry3,rx1,ry1); break; case 4: printf("\n Enter all coordinates values :"); scanf("%d %d %d %d %d %d",&x1,&y1,&x2,&y2,&x3,&y3); printf("\n Before Scaling "); /* get the scale factor*/ line(x1,y1,x2,y2); line(x2,y2,x3,y3); line(x3,y3,x1,y1); printf("\n Enter the Scale factor :"); [ ] www.Vidyarthiplus.com

www.Vidyarthiplus.com

Aravindan P 30308205012

scanf("%d %d",&sx,&sy); printf("\n After Scaling "); /* draw the object after scaling*/ line(x1+sx,y1+sy,x2+sx,y2+sy); line(x2+sx,y2+sy,x3+sx,y3+sy); line(x3+sx,y3+sy,x1+sx,y1+sy); break; case 5: printf("\n Enter all coordinates values :"); scanf("%d %d %d %d %d %d",&x1,&y1,&x2,&y2,&x3,&y3); printf("\n Before Shearing "); /* get the values for shearing*/ line(x1,y1,x2,y2); line(x2,y2,x3,y3); line(x3,y3,x1,y1); printf("\n Enter 0 for x-axis and 1 for y-axis: "); scanf("%d",&ch); if(ch==0) { printf("\n Enter the x-SHEAR (^.^) Value: "); scanf("%d",&shx); x1=x1+shx*y1; x2=x2+shx*y2; x3=x3+shx*y3; } else { printf("\n Enter the y-SHEAR (^.^) Value: "); scanf("%d",&shy); y1=y1+shy*x1; y2=y2+shy*x2; y3=y3+shy*x3; } printf("\n After Shearing "); line(x1,y1,x2,y2); /* draw the final object after shearing*/ line(x2,y2,x3,y3); line(x3,y3,x1,y1); break; default: exit(0); break; } } while(ch!=0); } getch(); closegraph(); /* close the graph*/ return 0; } [ ] www.Vidyarthiplus.com

www.Vidyarthiplus.com

Aravindan P 30308205012

OUTPUT: 1. 2. 3. 4. 5. Translation Reflection Rotation Scaling Shearing

Enter Your choice 1 Enter all coordinates values Before Translation 213 236 253 321 256 214

Enter the value translation vector 32 After Translation

1. 2. 3. 4. 5.

Translation Reflection Rotation Scaling Shearing

Enter Your choice 2 Enter all coordinates values 213 236 253 321 256 214

[ ] www.Vidyarthiplus.com

www.Vidyarthiplus.com

Aravindan P 30308205012

Before

After Reflection

1. 2. 3. 4. 5.

Translation Reflection Rotation Scaling Shearing

Enter Your choice 3 Enter all coordinates values Before Rotation 213 236 253 321 256 214

Enter the rotation angle 20 After Rotation

[ ] www.Vidyarthiplus.com

www.Vidyarthiplus.com

Aravindan P 30308205012

1. 2. 3. 4. 5.

Translation Reflection Rotation Scaling Shearing

Enter Your choice 4 Enter all coordinates values Before Scaling 213 236 253 321 256 214

Enter the scale factor 10 5 After Scaling

1. 2. 3. 4. 5.

Translation Reflection Rotation Scaling Shearing

Enter Your choice 4 Enter all coordinates values Before Shearing Enter 0 for x axis and 1 for y axis 0 213 236 253 321 256 214

[ ] www.Vidyarthiplus.com

www.Vidyarthiplus.com

Aravindan P 30308205012

Enter the x-shear value 1 After Shearing

Before Shearing Enter 0 for x axis and 1 for y axis 1

Enter the y-shear value 1

RESULT: Thus the Two dimensional transformations were successfully executed and the output is transformed, drawn and verified.

[ ] www.Vidyarthiplus.com

www.Vidyarthiplus.com

Aravindan P 30308205012

IMPLEMENTATION OF TWO DIMENSIONAL COMPOSITE TRANSFORMATIONS

DESCRIPTION: A transformation is any operation on a point in space (x, y) that maps the point's coordinates into a new set of coordinates (x1, y1).The Two Dimensional Composite transformation represent a sequence of transformations as a single matrix which has the order of operations as Translation, Rotation, Scaling, Shearing, Reflection. CODING:

#include <graphics.h> /* include the necessary header files*/ #include <stdlib.h> #include <stdio.h> #include <conio.h> #include <math.h> int xa,xb,xc,ya,yb,yc,y1a,y1b,y1c,x1a,x1b,x1c,x2a,x2b,x2c,y2a,y2b,y2c; int x3a,x3b,x3c,y3a,y3b,y3c,x4a,x4b,x4c,y4a,y4b,y4c,x5a,x5b,x5c,y5a,y5b,y5c; int tx,shx,t,ch,shy; float ang,theta,sx,sy; int main(void) { int gdriver = DETECT, gmode, errorcode; initgraph(&gdriver, &gmode,"C:\\TC\\BGI"); /* request for auto detection*/ printf("\n\t\t\t 2D Composite Transformations"); printf("\n\n Enter all coordinates values :"); scanf("%d %d %d %d %d %d",&xa,&ya,&xb,&yb,&xc,&yc); printf("\n\n The original Image"); /* get the coordinates for the original image*/ line(xa,ya,xb,yb); /* draw the original image*/ line(xb,yb,xc,yc); line(xc,yc,xa,ya); printf("\n\n Enter the value tranlsation factor :"); /* get the translation factor*/ scanf("%d",&tx); [ ] www.Vidyarthiplus.com

www.Vidyarthiplus.com

Aravindan P 30308205012

printf("\n\n After Translation "); x1a=xa+tx; x1b=xb+tx; x1c=xc+tx; y1a=ya; y1b=yb; y1c=yc; line(x1a,y1a,x1b,y1b); /* image after translation*/ line(x1b,y1b,x1c,y1c); line(x1c,y1c,x1a,y1a); delay(1); printf("\n\n Next Operation is Rotation"); printf("\n\n Enter the rotation angle :"); /* get the angle of rotation*/ scanf("%f",&ang); theta=((ang*3.14)/180); /* convert the angle*/ x2a=x1a*cos(theta)-y1a*sin(theta); y2a=x1a*sin(theta)+y1a*cos(theta); x2b=x1b*cos(theta)-y1b*sin(theta); y2b=x1b*sin(theta)+y1b*cos(theta); x2c=x1c*cos(theta)-y1c*sin(theta); y2c=x1c*sin(theta)+y1c*cos(theta); printf("\n\n After Rotation "); /* the rotated object*/ line(x2a,y2a,x2b,y2b); line(x2b,y2b,x2c,y2c); line(x2c,y2c,x2a,y2a); delay(1); printf("\n\n Next Operation is Scaling"); /* get the scale factor*/ printf("\n\n Enter the Scale factor :"); scanf("%f %f",&sx,&sy); x3a=x2a+sx; /* modify the objects coordinates based on the scale factor*/ y3a=y2a+sy; x3b=x2b+sx; [ ] www.Vidyarthiplus.com

www.Vidyarthiplus.com

Aravindan P 30308205012

y3b=y2b+sy; x3c=x2c+sx; y3c=y2c+sy; printf("\n\n After Scaling "); line(x3a,y3a,x3b,y3b); line(x3b,y3b,x3c,y3c); line(x3c,y3c,x3a,y3a); delay(1); printf("\n\n Next Operation is Shearing"); printf("\n\n Enter 1 for x-axis \n 2 for y-axis: "); /* get the choice of shearing in the x or y axis*/ scanf("%d",&ch); if(ch==1) /* get the shear value*/ { printf("\n\n Enter the x-SHEAR (^.^) Value: "); scanf("%d",&shx); } else { printf("\n\n Enter the y-SHEAR (^.^) Value: "); scanf("%d",&shy); } if(ch==1) { x3a=x3a+shx*y3a; y4a=y3a; x3b=x3a+shx*y3a; y4b=y3b; x3c=x3a+shx*y3a; y4c=y3c; } else { [ ] www.Vidyarthiplus.com

www.Vidyarthiplus.com

Aravindan P 30308205012

x4a=x3a; y3a=y3a+shy*x3a; x4b=x3b; y3b=y3b+shy*x3b; x4c=x3c; y3c=y3c+shy*x3c; } printf("\n\n After Shearing "); /* draw the final object after shearing*/ line(x3a,y3a,x3b,y3b); line(x3b,y3b,x3c,y3c); line(x3c,y3c,x3a,y3a); delay(1); printf("\n\n Next Operation is Reflection"); t=abs(y3a-y3c); /* calculate the value for reflection*/ x5a=x3a; x5b=x3b; x5c=x3c; y5a=y3a+10+(2*t); y5b=y3b+10; y5c=y3c+10; printf("\n\n After Reflection "); /* the final object after all the transformations*/ line(x5a,y5a,x5b,y5b); line(x5b,y5b,x5c,y5c); line(x5c,y5c,x5a,y5a); getch(); closegraph(); return 0; }

[ ] www.Vidyarthiplus.com

www.Vidyarthiplus.com

Aravindan P 30308205012

OUTPUT: 2D Composite Transformations Enter all coordinates values The original Image 213 236 253 321 256 214

Enter the value translation vector 32 After Translation

Next Operation is Rotation Enter the rotation angle 20 After Rotation

Next Operation is Scaling Enter the scale factor 10 5

[ ] www.Vidyarthiplus.com

www.Vidyarthiplus.com

Aravindan P 30308205012

After Scaling

Next Operation is Shearing Enter 0 for x axis and 1 for y axis Enter the x-shear value 1 After Shearing 0

Enter 0 for x axis and 1 for y axis Enter the y-shear value 1

Next Operation is Reflection After Reflection

[ ] www.Vidyarthiplus.com

www.Vidyarthiplus.com

Aravindan P 30308205012

RESULT: Thus the Two dimensional Composite transformations were successfully executed and the output is transformed, drawn and verified.

[ ] www.Vidyarthiplus.com

www.Vidyarthiplus.com

Aravindan P 30308205012

IMPLEMENTATION OF LINE, CIRCLE AND ELLIPSE ATTRIBUTES

DESCRIPTION: Output primitives have geometric and non-geometric attributes. Geometric attributes, such as the character height, affect the size and shape of a primitive, whereas non-geometric attributes are qualities such as colour, line style, etc. The output primitives Such as Line, Circle and Ellipse are associated with set of attributes such as Line (color and Line Style), Cicrle (Color) and Ellipse (Color and Patterns). CODING:

#include<graphics.h> /* include the necessary header files*/ #include<stdlib.h> #include<stdio.h> #include<conio.h> int main(void) { /* select a driver and mode that supports */ /* multiple drawing colors.*/ int gdriver=EGA,DETECT,gmode=EGAHI,errorcode; int color,maxcolor,x,y,s,ch,ch1,ch2,i; int midx,midy; int radius=100; int xradius=100,yradius=50; char msg[80]; char *lname[]={"Solid Line", "Dotted Line", "Center Line", "Dashed Line", "Usebit Line"}; /* initialize graphics and local variables */ initgraph(&gdriver,&gmode,"c:\\tc\\bgi"); /* read result of initialization */ errorcode=graphresult(); if (errorcode!=grOk) /* an error occurred */ { printf("Graphics error: %s\n", grapherrormsg(errorcode)); [ ] www.Vidyarthiplus.com

www.Vidyarthiplus.com

Aravindan P 30308205012

printf("Press any key to halt:"); getch(); exit(1); /* terminate with an error code */ } do{ printf("\n1.Line\n2.Circle\n3.Ellipse\n"); /* get the user choice*/ printf("\nEnter Your choice\n"); scanf("%d",&ch); switch(ch) { case 1: printf("Attribute: 1.Color 2.Style:\n"); scanf("%d",&ch1); switch(ch1) { case 1: maxcolor=getmaxcolor(); /* use predefined methods to change the color*/ x=getmaxx()/2; y=getmaxy()/2; for(color=1;color<=maxcolor;color++) { cleardevice(); setcolor(color); line(100,100,100,300); sprintf(msg,"Color:%d",color); outtextxy(x,y,msg); getch(); } closegraph(); break; case 2: initgraph(&gdriver,&gmode,"c:\\tc\\bgi"); [ ] www.Vidyarthiplus.com

www.Vidyarthiplus.com

Aravindan P 30308205012

for(s=0;s<5;s++) { setlinestyle(s,1,1); /* pre defined method for linestyle*/ line(20,20+s*50,120,120+s*50); outtextxy(125,120+s*50,lname[s]); } getch(); closegraph(); break; } break; case 2: initgraph(&gdriver,&gmode,"c:\\tc\\bgi"); midx=getmaxx()/2; midy=getmaxy()/2; maxcolor=getmaxcolor(); /* draw circles of different colors*/ for(color=1;color<=maxcolor;color++) { cleardevice(); setcolor(color); /* draw the circle */ circle(midx,midy,radius); /* clean up */ getch(); } closegraph(); break; case 3: printf("\n1.pattern 2.colour\n"); /* get choice for color or style of eclipse*/ printf("\nEnter your choice:\n"); scanf("%d",&ch2); switch(ch2) [ ] www.Vidyarthiplus.com

www.Vidyarthiplus.com

Aravindan P 30308205012

{ case 1: /* initialize graphics and local variables */ initgraph(&gdriver,&gmode,"c:\\tc\\bgi"); midx=getmaxx()/2; midy=getmaxy()/2; /* loop through the fill patterns */ for(i=EMPTY_FILL;i<USER_FILL;i++) { /* set fill pattern */ setfillstyle(i,getmaxcolor()); /* draw a filled ellipse */ fillellipse(midx, midy, xradius, yradius); getch(); } closegraph(); break; case 2: initgraph(&gdriver,&gmode,"c:\\tc\\bgi"); maxcolor=getmaxcolor(); for(color=1;color<=maxcolor;color++) { cleardevice(); setcolor(color); ellipse(100,200,0,360,xradius,yradius); getch(); } /* clean up */ closegraph(); break; } default: [ ] www.Vidyarthiplus.com

www.Vidyarthiplus.com

Aravindan P 30308205012

//exit(0); break; } } while(ch==3); return 0; }

OUTPUT: Line Color

Line Style

[ ] www.Vidyarthiplus.com

www.Vidyarthiplus.com

Aravindan P 30308205012

Circle Color

Ellipse Pattern

Ellipse Color

[ ] www.Vidyarthiplus.com

www.Vidyarthiplus.com

Aravindan P 30308205012

RESULT: Thus the attributes were successfully applied to Line, circle and ellipse and the output is drawn and verified.

[ ] www.Vidyarthiplus.com

www.Vidyarthiplus.com

Aravindan P 30308205012

IMPLEMENTATION OF COHEN SUTHERLAND LINE CLIPPING ALGORITHM

DESCRIPTION: The Cohen Sutherland Algorithm is a line clipping algorithm which quickly detects and dispenses with two common and trivial cases. To clip a line, we need to consider only its endpoints. If both endpoints of a line lie inside the window, the entire line lies inside the window. It is trivially accepted and needs no clipping. On the other hand, if both endpoints of a line lie entirely to one side of the window, the line must lie entirely outside of the window. It is trivially rejected and needs to be neither clipped nor displayed. CODING:

#include<stdio.h> #include<graphics.h> #include<conio.h>

/* include the necessary header files*/

typedef unsigned int outcode; enum {TOP=0x1,BOTTOM=0x2,RIGHT=0x4,LEFT=0x8}; void lineclip(x0,y0,x1,y1,xwmin,ywmin,xwmax,ywmax) float x0,y0,x1,y1,xwmin,ywmin,xwmax,ywmax; { int gd,gm; outcode code0,code1,codeout; int accept=0,done=0; code0=calcode(x0,y0,xwmin,ywmin,xwmax,ywmax); /* initialize the values*/ code1=calcode(x1,y1,xwmin,ywmin,xwmax,ywmax); do { if(!(code0|code1)) /*vary the condition variables value based on the values*/ { accept=1;done=1; } else if(code0&code1)done=1; [ ] www.Vidyarthiplus.com

www.Vidyarthiplus.com

Aravindan P 30308205012

else { float x,y; codeout=code0?code0:code1; if(codeout&TOP) /* now, decide the position of the object on the clipping window*/ { x=x0+(x1-x0)*(ywmax-y0)/(y1-y0); y=ywmax; } else if(codeout&BOTTOM) { x=x0+(x1-x0)*(ywmin-y0)/(y1-y0); y=ywmin; } else if (codeout&RIGHT) { y=y0+(y1-y0)*(xwmax-x0)/(x1-x0); x=xwmax; } else { y=y0+(y1-y0)*(xwmin-x0)/(x1-x0); x=xwmin; } if(codeout==code0) { x0=x;y0=y; code0=calcode(x0,y0,xwmin,ywmin,xwmax,ywmax); } else [ ] www.Vidyarthiplus.com

www.Vidyarthiplus.com

Aravindan P 30308205012

{ x1=x;y1=y; code1=calcode(x1,y1,xwmin,ywmin,xwmax,ywmax); } } } while(done==0); if(accept) line(x0,y0,x1,y1); rectangle(xwmin,ywmin,xwmax,ywmax); /* draw the clipping window*/ getch(); } int calcode(x,y,xwmin,ywmin,xwmax,ywmax) float x,y,xwmin,ywmin,xwmax,ywmax; { int code=0; /* assign the values of the clipped image*/ if(y>ywmax) code|=TOP; else if(y<ywmin) code|=BOTTOM; else if(x>xwmax) code|=RIGHT; else if (x<xwmin) code|=LEFT; return(code); } void main() { float x2,y2,x1,y1,xwmin,ywmin,xwmax,ywmax; int gd=DETECT,gm; clrscr(); initgraph(&gd,&gm,"C:\\tc\\bgi"); /* request auto detection*/ [ ] www.Vidyarthiplus.com

www.Vidyarthiplus.com

Aravindan P 30308205012

printf("\n\n\tEnter the co-ordinates of Line :"); /* get the coordinates of the line*/ printf("\n\n\tX1 Y1 : "); scanf("%f %f",&x1,&y1); printf("\n\n\tX2 Y2 : "); scanf("%f %f",&x2,&y2); printf("\n\tEnter the co_ordinates of window :\n "); /* get the coordinates of the clipping window*/ printf("\n\txwmin , ywmin : "); scanf("%f %f",&xwmin,&ywmin); printf("\n\txwmax , ywmax : "); scanf("%f %f",&xwmax,&ywmax); clrscr(); line(x1,y1,x2,y2); rectangle(xwmin,ywmin,xwmax,ywmax); getch(); clrscr(); lineclip(x1,y1,x2,y2,xwmin,ywmin,xwmax,ywmax );/* call the clipping function*/ getch(); closegraph(); }

OUTPUT:

X1, Y1 =120 240 X2, Y2 =350 500 Xwmin, Ywmin= 200 200 Xwmax, Ywmax= 350 350

[ ] www.Vidyarthiplus.com

www.Vidyarthiplus.com

Aravindan P 30308205012

Before Clipping

After Clipping

RESULT: Thus the Cohen Sutherland line clipping algorithm was successfully executed and the output is drawn and verified.

[ ] www.Vidyarthiplus.com

www.Vidyarthiplus.com

Aravindan P 30308205012

IMPLEMENTATION OF SUTHERLAND HODGEMAN POLYGON CLIPPING ALGORITHM

DESCRIPTION: The Sutherland Hodgeman Algorithm is used for clipping polygons. It works by extending each line of the convex clip polygon in turn and selecting only vertices from the subject polygon that is on the visible side. This algorithm performs a clipping of a polygon against each window edge in turn. It accepts an ordered sequence of vertices v1, v2, v3... vn and puts out a set of vertices defining the clipped polygon.

CODING:

#include<stdio.h> #include<graphics.h> #include<conio.h> #include<math.h> #include<process.h> #define TRUE 1 #define FALSE 0

/* include the necessary header files*/

typedef unsigned int outcode; outcode CompOutCode(float x,float y); /* create an user defined function for the output*/ enum { TOP=0x1, BOTTOM=0x2, RIGHT=0x4, LEFT=0x8 }; float xmin,xmax,ymin,ymax; void clip(float x0,float y0,float x1,float y1) /* define the clipping function*/ { outcode outcode0,outcode1,outcodeOut; int accept=FALSE,done=FALSE; outcode0=CompOutCode(x0,y0); /* call the user defined function*/ [ ] www.Vidyarthiplus.com

www.Vidyarthiplus.com

Aravindan P 30308205012

outcode1=CompOutCode(x1,y1); do /* assign the values for the condition variables*/ { if(!(outcode0|outcode1)) { accept=TRUE; done=TRUE; } else if(outcode0&outcode1) done=TRUE; else { float x,y; outcodeOut=outcode0?outcode0:outcode1; /* use the tertiary operator to assign values*/ if(outcodeOut&TOP) /* reassign the value of x and y */ { x=x0+(x1-x0)*(ymax-y0)/(y1-y0); y=ymax; } else if(outcodeOut&BOTTOM) { x=x0+(x1-x0)*(ymin-y0)/(y1-y0); y=ymin; } else if(outcodeOut&RIGHT) { y=y0+(y1-y0)*(xmax-x0)/(x1-x0); x=xmax; } [ ] www.Vidyarthiplus.com

www.Vidyarthiplus.com

Aravindan P 30308205012

else { y=y0+(y1-y0)*(xmin-x0)/(x1-x0); x=xmin; } if(outcodeOut==outcode0) { x0=x; y0=y; outcode0=CompOutCode(x0,y0); } else { x1=x; y1=y; outcode1=CompOutCode(x1,y1); } } } while(done==FALSE); if(accept) line(x0,y0,x1,y1); outtextxy(150,20,"POLYGON AFTER CLIPPING"); rectangle(xmin,ymin,xmax,ymax); /* draw the clipping window*/ } outcode CompOutCode(float x,float y) /* define the output function*/ { outcode code=0; if(y>ymax) code|=TOP; else if(y<ymin) [ ] www.Vidyarthiplus.com

www.Vidyarthiplus.com

Aravindan P 30308205012

code|=BOTTOM; if(x>xmax) code|=RIGHT; else if(x<xmin) code|=LEFT; return code; } void main() { float x1,y1,x2,y2; /* request auto detection */ int gdriver=DETECT,gmode,n,poly[14],i; clrscr( ); printf("Enter the no of sides of polygon:"); /* get the sides of the polygon*/ scanf("%d",&n); printf("\nEnter the coordinates of polygon\n"); for(i=0;i<2*n;i++) { scanf("%d",&poly[i]); } poly[2*n]=poly[0]; poly[2*n+1]=poly[1]; printf("Enter the rectangular coordinates of clipping window\n"); scanf("%f%f%f%f",&xmin,&ymin,&xmax,&ymax); /* get the coordinates of the clipping window*/ /* initialize graphics and local variables */ initgraph(&gdriver, &gmode, "c:\\tc\\bgi"); outtextxy(150,20,"POLYGON BEFORE CLIPPING"); drawpoly(n+1,poly); rectangle(xmin,ymin,xmax,ymax); getch(); cleardevice(); [ ] www.Vidyarthiplus.com

www.Vidyarthiplus.com

Aravindan P 30308205012

for(i=0;i<n;i++) clip(poly[2*i],poly[(2*i)+1],poly[(2*i)+2],poly[(2*i)+3]); /* call the clipping function*/ getch(); restorecrtmode(); }

OUTPUT:

Enter the Sides of the Polygon: 5 Enter the co-ordinates of the Polygon 80 50 200 100 350 350 80 200 40 80 Enter the rectangular co-ordinates 150 150 300 300

[ ] www.Vidyarthiplus.com

www.Vidyarthiplus.com

Aravindan P 30308205012

RESULT: Thus the Sutherland Hodgeman polygon clipping algorithm was successfully executed and the output is drawn and verified.

[ ] www.Vidyarthiplus.com

www.Vidyarthiplus.com

Aravindan P 30308205012

OPEN GL PROGRAMS USING VC++ PROCEDURE:

Before executing the programs you have to place three header files (dll, Header, Lib) in the following locations Header: C:\Program Files\Microsoft Visual Studio\VC98\Include\GL Lib: C:\Program Files\Microsoft Visual Studio\VC98\Lib Dll: C:\WINDOWS\system32

Go to StartPrograms Microsoft Visual Studio 6.0 Microsoft Visual C++ 6.0 FileNew...

New dialog boxes opens in that select the win32console application from the projects tab and give the name for the project and click on the ok and finish button.

[ ] www.Vidyarthiplus.com

www.Vidyarthiplus.com

Aravindan P 30308205012

Now select Tools menu and select options from it.

In that select the directories tab and select the browse button as given in the below screenshot.

Select the path for the three header files that you have pasted in different locations and click on the ok button.

[ ] www.Vidyarthiplus.com

www.Vidyarthiplus.com

Aravindan P 30308205012

FileNew...

Select the C++ source file from the files tab and give the name for the file and click ok button .

Now select project menu and select the settings from the drop down menu.

[ ] www.Vidyarthiplus.com

www.Vidyarthiplus.com

Aravindan P 30308205012

In the project settings dialog box click on the link tab and type the three file names at the end of the object/library modules text box. File names: opengl32.lib glu32.lib glut32.lib

After entering the file names click on the C/C++ tab and select C++ Language from the category drop down list and click on the ok button

[ ] www.Vidyarthiplus.com

www.Vidyarthiplus.com

Aravindan P 30308205012

After performing these steps type the required program code save the program and click on the build button. Then click on the Execute program button

[ ] www.Vidyarthiplus.com

www.Vidyarthiplus.com

Aravindan P 30308205012

IMPLEMENTATION OF 3D OBJECTS MENU USING IN VC++ USING OPENGL

DESCRIPTION:

CODING:

#include<stdio.h> #include<stdlib.h> #include<math.h> #include<GL/glut.h> static GLfloat rot=0,a=1.0,b=1.0,c=1.0,as,tx,tz,sx,sy,sz,rx,ry,rz,an,ang; static GLint op,p,pr,pd,ch,key; void disp() { glClear(GL_COLOR_BUFFER_BIT); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(80.0,(GLdouble)4/(GLdouble)3,0.1,30.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(0.0,0.0,20.0,0.0,0.0,0.0,0.0,1.0,1.0); if(pr==1) { glColor3f(1.0,1.0,0.0); glutWireTeapot(8); } if(pr==2) { glColor3f(0.0,1.0,1.0); glutWireSphere(12,20,40); } if(pr==3) [ ] www.Vidyarthiplus.com

www.Vidyarthiplus.com

Aravindan P 30308205012

{ glColor3f(1.0,0.0,1.0); glutWireTorus(4,8,20,40); } if(pr==4) { glColor3f(1.0,1.0,0.0); glutWireCube(8); } if(pr==5) { glColor3f(1.0,0.0,0.0); glutWireCone(3.0,3.0,10,20); } if(pr==6) { glColor3f(1.0,0.0,1.0); glutWireTetrahedron(); } if(pr==7) { glColor3f(1.0,0.0,1.0); glutWireOctahedron(); } glutSwapBuffers(); } void myidle() { rot=rot+1.0; glutPostRedisplay(); } void mykey(unsigned char pd,int x,int y) [ ] www.Vidyarthiplus.com

www.Vidyarthiplus.com

Aravindan P 30308205012

{ switch(pd) { case 'p': printf("1.Teapot \n2.Sphere \n3.Torus \n4.Cube \n5.Cone \n6.Tetrahedron \n7.Octahedron"); printf("\nEnter the option"); scanf("%d",&p); switch(p) { case 1:pr=1; break; case 2:pr=2; break; case 3:pr=3; break; case 4:pr=4; break; case 5:pr=5; break; case 6:pr=6; break; case 7:pr=7; break; } break; } } void main(int argc,char** argv) { glutInit(&argc,argv); glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB); [ ] www.Vidyarthiplus.com

www.Vidyarthiplus.com

Aravindan P 30308205012

glutInitWindowSize(640,480); glutInitWindowPosition(0,0); glutCreateWindow("3d program"); glClearColor(0,0,0,0); glutDisplayFunc(disp); glutKeyboardFunc(mykey); glutIdleFunc(myidle); glutMainLoop(); }

OUTPUT:

[ ] www.Vidyarthiplus.com

www.Vidyarthiplus.com

Aravindan P 30308205012

[ ] www.Vidyarthiplus.com

www.Vidyarthiplus.com

Aravindan P 30308205012

[ ] www.Vidyarthiplus.com

www.Vidyarthiplus.com

Aravindan P 30308205012

[ ] www.Vidyarthiplus.com

www.Vidyarthiplus.com

Aravindan P 30308205012

IMPLEMENTATION OF 3D COMPOSITE TRANSFORMATION IN VC++ USING OPENGL

DESCRIPTION:

CODING:

#include<stdio.h> #include<stdlib.h> #include<math.h> #include<GL/glut.h> static GLfloat rot=0,a=1.0,b=1.0,c=1.0,as,tx,ty,tz,sx,sy,sz,rx,ry,rz,an,ang; static GLint op,p,pr,pd,ch,key; void mydisp() { glClear(GL_COLOR_BUFFER_BIT); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(80.0,(GLdouble)4/(GLdouble)3,0.1,30.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(0.0,0.0,20.0,0.0,0.0,0.0,0.0,1.0,1.0); if(as==1) glTranslatef(a,b,c); if(as==2) glScalef(a,b,c); if(as==3) glRotatef(an,a,b,c); if(pr==1) { glColor3f(1.0,1.0,0.0); glutWireTeapot(8);

[ ] www.Vidyarthiplus.com

www.Vidyarthiplus.com

Aravindan P 30308205012

} if(pr==2) { glColor3f(0.0,1.0,1.0); glutWireSphere(12,20,40); } if(pr==3) { glColor3f(1.0,0.0,1.0); glutWireTorus(4,8,20,40); } if(pr==4) { glColor3f(1.0,1.0,0.0); glutWireCube(8); } if(pr==5) { glColor3f(1.0,0.0,0.0); glutWireCone(3.0,3.0,10,20); } if(pr==6) { glColor3f(1.0,0.0,1.0); glutWireTetrahedron(); } if(pr==7) { glColor3f(1.0,0.0,1.0); glutWireOctahedron(); } glutSwapBuffers(); [ ] www.Vidyarthiplus.com

www.Vidyarthiplus.com

Aravindan P 30308205012

} void myidle() { rot=rot+1.0; glutPostRedisplay(); } void myKey(unsigned char pd,int x,int y) { switch(pd) { case 'p': case 'P': printf("1.teapot\n2.sphere\n3.torus\n4.cube\n5.cone\n6.tetrahedron\n7.octahedron"); printf("\nEnter the option"); scanf("%d",&p); switch(p) { case 1: pr=1; break; case 2: pr=2; break; case 3: pr=3; break; case 4: pr=4; break; case 5: pr=5; break; [ ] www.Vidyarthiplus.com

www.Vidyarthiplus.com

Aravindan P 30308205012

case 6: pr=6; break; case 7: pr=7; break; } break; case 'm': case'M': glColor3f(1.0,1.0,0.0); glutWireTeapot(8); printf("1.translation \n 2.scaling \n3.rotation"); printf("\n Enter the option"); scanf("%d",&op); switch(op) { case 1:

printf("Enter the tx,ty,tz values"); scanf("%f %f %f",&tx,&ty,&tz); a=(GLfloat)tx; b=(GLfloat)ty; c=(GLfloat)tz; as=1; break; case 2:

printf("Enter the sx,sy,sz values"); scanf("%f %f %f",&sx,&sy,&sz); a=(GLfloat)sx; b=(GLfloat)sy; [ ] www.Vidyarthiplus.com

www.Vidyarthiplus.com

Aravindan P 30308205012

c=(GLfloat)sz; as=2; break; case 3:

printf("Enter the rx,ry,rz values"); scanf("%f %f %f",&rx,&ry,&rz); a=(GLfloat)rx; b=(GLfloat)ry; c=(GLfloat)rz; as=3; break; default: printf("choose the correct option"); break; } } } void main(int argc,char** argv) { glutInit(&argc,argv); glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB); glutInitWindowSize(640,480); glutInitWindowPosition(0,0); glutCreateWindow("3D PROGRAM"); glClearColor(0,0,0,0); glutDisplayFunc(mydisp); glutKeyboardFunc(myKey); glutIdleFunc(myidle); glutMainLoop(); }

[ ] www.Vidyarthiplus.com

www.Vidyarthiplus.com

Aravindan P 30308205012

OUTPUT:

BEFORE TRANSFORMATION:

[ ] www.Vidyarthiplus.com

www.Vidyarthiplus.com

Aravindan P 30308205012

AFTER TRANSLATION:

[ ] www.Vidyarthiplus.com

www.Vidyarthiplus.com

Aravindan P 30308205012

AFTER SCALING:

[ ] www.Vidyarthiplus.com

www.Vidyarthiplus.com

Aravindan P 30308205012

AFTER ROATAION:

[ ] www.Vidyarthiplus.com

www.Vidyarthiplus.com

Aravindan P 30308205012

BEFORE TRANSFORMATION:

AFTER TRANSLATION:

[ ] www.Vidyarthiplus.com

www.Vidyarthiplus.com

Aravindan P 30308205012

[ ] www.Vidyarthiplus.com

www.Vidyarthiplus.com

Aravindan P 30308205012

AFTER SCALING:

[ ] www.Vidyarthiplus.com

www.Vidyarthiplus.com

Aravindan P 30308205012

AFTER ROATAION:

[ ] www.Vidyarthiplus.com

www.Vidyarthiplus.com

Aravindan P 30308205012

[ ] www.Vidyarthiplus.com

You might also like