CS2405 - COMPUTER GRAPHICS LAB - - COMPUTER GRAPHICS LAB 1. ... Line, Circle, Ellipse. 3. Implementation of Line, Circle and ellipse Attributes 4.

  • Published on
    16-Mar-2018

  • View
    215

  • Download
    2

Transcript

CS2405 - COMPUTER GRAPHICS LAB 1. Output primitives. 2. Implementation of Bresenhams Algorithm Line, Circle, Ellipse. 3. Implementation of Line, Circle and ellipse Attributes 4. Two Dimensional transformations - Translation, Rotation, Scaling, Reflection, Shear. 5. Composite 2D Transformations 6. Cohen Sutherland 2D line clipping and Windowing 7. Sutherland Hodgeman Polygon clipping Algorithm 8. Three dimensional transformations - Translation, Rotation, Scaling 9. Composite 3D transformations 10. Drawing three dimensional objects and Scenes 11. Generating Fractal images BASIC GRAPHICS FUNCTION 1) Initgraph () initgraph() function initializes the graphics mode and clears the screen. Declaration: void far initgraph(int far *driver, int far *mode, char far *path) 2) Detectgraph () Detectgraph function determines the graphics hardware in the system, if the function finds a graphics adapter then it returns the highest graphics mode that the adapter supports. Declaration: void far detectgraph(int far *driver, int far *mode) Integer that specifies the graphics driver to be used. You can give graphdriver a value using a constant of the graphics_drivers enumeration type. 3) Closegraph () closegraph() function switches back the screen from graphcs mode to text mode. It clears the screen also. A graphics program should have a closegraph function at the end of graphics. Otherwise DOS screen will not go to text mode after running the program. 4) Getpixel () getpixel function returns the color of pixel present at location(x, y). Declaration:- int getpixel(int x, int y); 5) Putpixel () putpixel function plots a pixel at location (x, y) of specified color. Declaration:- void putpixel(int x, int y, int color); For example if we want to draw a GREEN color pixel at (35, 45) then we will write putpixel(35, 35, GREEN); in our c program, putpixel function can be used to draw circles, lines and ellipses using various algorithms. 6) line() line function is used to draw a line from a point(x1,y1) to point(x2,y2) i.e. (x1,y1) and (x2,y2) are end points of the line. Declaration :- void line(int x1, int y1, int x2, int y2); 7) lineto() lineto function draws a line from current position(CP) to the point(x,y), you can get current position using getx and gety function. 8) circle() circle function is used to draw a circle with center (x,y) and third parameter specifies the radius of the circle. Declaration :- void circle(int x, int y, int radius); 9)ellipse() Ellipse is used to draw an ellipse (x,y) are coordinates of center of the ellipse, stangle is the starting angle, end angle is the ending angle, and fifth and sixth parameters specifies the X and Y radius of the ellipse. To draw a complete ellipse strangles and end angle should be 0 and 360 respectively. Declaration :- void ellipse(int x, int y, int stangle, int endangle, int xradius, int yradius); 10) drawpoly() drawpoly function is used to draw polygons i.e. triangle, rectangle, pentagon, hexagon etc. Declaration :- void drawpoly( int num, int *polypoints ); num indicates (n+1) number of points where n is the number of vertices in a polygon, polypoints points to a sequence of (n*2) integers . Each pair of integers gives x and y coordinates of a point on the polygon. We specify (n+1) points as first point coordinates should be equal to (n+1)th to draw a complete figure. To understand more clearly we will draw a triangle using drawpoly, consider for example the array :- int points[] = { 320, 150, 420, 300, 250, 300, 320, 150}; points array contains coordinates of triangle which are (320, 150), (420, 300) and (250, 300). Note that last point(320, 150) in array is same as first. 11) outtext () outtext function displays text at current position. Declaration :- void outtext(char *string); 12) outtextxy () outtextxy function display text or string at a specified point(x,y) on the screen. Declaration :- void outtextxy(int x, int y, char *string); x, y are coordinates of the point and third argument contains the address of string to be displayed. 13)rectangle() Rectangle function is used to draw a rectangle. Coordinates of left top and right bottom corner are required to draw the rectangle. left specifies the X-coordinate of top left corner, top specifies the Y-coordinate of top left corner, right specifies the X-coordinate of right bottom corner, bottom specifies the Y-coordinate of right bottom corner. Declaration :- void rectangle(int left, int top, int right, int bottom); 14) floodfill() floodfill function is used to fill an enclosed area. Current fill pattern and fill color is used to fill the area.(x, y) is any point on the screen if (x,y) lies inside the area then inside will be filled otherwise outside will be filled,border specifies the color of boundary of area. Declaration :- void floodfill(int x, int y, int border); 15)fillpoly() f illpoly function draws and fills a polygon. It require same arguments as drawpoly. Declaration :- void drawpoly( int num, int *polypoints ); 16)fillellipse() f illellipse function draws and fills a polygon. Declaration:- void fillellipse(int x, int y, int xradius, int yradius); x and y are coordinates of center of the ellipse, xradius and yradius are x and y radius of ellipse respectively. An Example program using the basic graphic functions #include #include #include #include void main() { int gd=DETECT, gm; initgraph(&gd, &gm, " c:\\turboc\\bgi"); circle(100,100,50); outtextxy(75,170, "Circle"); rectangle(200,50,350,150); outtextxy(240, 170, "Rectangle"); ellipse(500, 100,0,360, 100,50); outtextxy(480, 170, "Ellipse"); line(100,250,540,250); outtextxy(300,260,"Line"); getch(); closegraph(); } Ex.No:1 Program: #include #include #include #include void main() { char ch='y'; int gd=DETECT,gm,x1,y1,x2,y2,rad,sa,ea,xrad,yrad,i; initgraph(&gd,&gm,""); while(ch=='y') { cleardevice(); setbkcolor(9); outtextxy(100,150,"Enter 1 to get line"); outtextxy(100,170,"2.Circle"); outtextxy(100,190,"3.Box"); outtextxy(100,210,"4.Arc"); outtextxy(100,230,"5.Ellipse"); outtextxy(100,250,"6.Rectangle"); outtextxy(100,270,"7.Exit"); ch=getch(); cleardevice(); switch(ch) { case '1': line(100,200,300,400); break; case '2': circle(200,200,100); break; case '3': setfillstyle(5,4); bar(100,300,200,100); break; case '4': setfillstyle(5,4); arc(200,200,100,300,100); break; case '5': setfillstyle(5,4); fillellipse(100,100,50,100); break; case '6': settextstyle(DEFAULT_FONT,0,2); outtextxy(120,140,"VEL TECH"); line(100,100,100,300); line(300,300,100,300); line(100,100,300,100); line(300,100,300,300); break; case '7': closegraph(); return; } ch='y'; getch(); } } Output: Ex.No:2a Program: #include #include #include #include void main() { int gd = DETECT, gm; int x1, y1, x2, y2,dx,dy,steps,k; float xincrement,yincrement,x,y; initgraph(&gd, &gm, "..\\bgi"); printf("Enter the Starting Point of x axis : "); scanf("%d", &x1); printf("Enter the Starting of y axis : "); scanf("%d", &y1); printf("Enter the End Point of x axis : "); scanf("%d", &x2); printf("Enter the End Point of y axis : "); scanf("%d", &y2); clrscr(); dx = x2 x1; dy = y2 y1; x=x1; y=y1; if(abs(dx) > abs(dy)) steps=abs(dx); else steps=abs(dy); xincrement=dx/(float)steps; yincrement=dy/(float)steps; putpixel(ceil(x), ceil(y), RED); for(k=1;kOutput Enter the Starting Point of x axis : 100 Enter the Starting of y axis : 100 Enter the End Point of x axis : 200 Enter the End Point of y axis : 200 Ex.No:2b Program #include #include #include #include void main() { int gd = DETECT, gm; int x,y,x1,y1,x2,y2,p,dx,dy,twody,twodydx,xend; initgraph(&gd,&gm,"..\\BGI:"); printf("\nEnter the x-coordinate of the starting point :"); scanf("%d",&x1); printf("\nEnter the y-coordinate of the starting point :"); scanf("%d",&y1); printf("\nEnter the x-coordinate of the Ending point :"); scanf("%d",&x2); printf("\nEnter the y-coordinate of the ending point :"); scanf("%d",&y2); clrscr(); dx=x2-x1; dy=y2-y1; p=2*dy-dx; twody=2*dy; twodydx=2*(dy-dx); if (x1>x2) { x=x2; y=y2; xend=x1; } else { x=x1; y=y1; xend=x2; } putpixel(x,y,RED); while(xx++; if (p Output Enter the x-coordinate of the starting point : 100 Enter the y-coordinate of the starting point : 100 Enter the x-coordinate of the Ending point : 200 Enter the y-coordinate of the ending point : 200 Ex.No:2c Program #include #include #include #include int x,y,r,p,xcenter, ycenter; void circleplot(int,int,int,int); void main() { int gd = DETECT, gm; initgraph(&gd,&gm,"..\\BGI:"); printf("\nEnter the x-coordinate of the centre point :"); scanf("%d",&xcenter); printf("\nEnter the y-coordinate of the centre point :"); scanf("%d",&ycenter); printf("\nEnter the radius :"); scanf("%d",&r); x=0; y=r; p=1-r; while (xputpixel(xcenter+x,ycenter-y,10); putpixel(xcenter-x,ycenter-y,10); putpixel(xcenter+y,ycenter+x,10); putpixel(xcenter-y,ycenter+x,10); putpixel(xcenter+y,ycenter-x,10); putpixel(xcenter-y,ycenter-x,10); } Output Enter the x-coordinate of the centre point : 100 Enter the y-coordinate of the centre point : 100 Enter the radius : 50 Ex.No:2d Program #include #include #include #include int x,y,r,p,xcenter, ycenter; void circleplot(int,int,int,int); void main() { int gd = DETECT, gm; initgraph(&gd,&gm,"..\\BGI:"); printf("\nEnter the x-coordinate of the centre point :"); scanf("%d",&xcenter); printf("\nEnter the y-coordinate of the centre point :"); scanf("%d",&ycenter); printf("\nEnter the radius :"); scanf("%d",&r); x=0; y=r; p=3-(2*r); while (xputpixel(xcenter+x,ycenter-y,10); putpixel(xcenter-x,ycenter-y,10); putpixel(xcenter+y,ycenter+x,10); putpixel(xcenter-y,ycenter+x,10); putpixel(xcenter+y,ycenter-x,10); putpixel(xcenter-y,ycenter-x,10); } Output Enter the x-coordinate of the centre point : 100 Enter the y-coordinate of the centre point : 100 Enter the radius : 50 Ex.No:2e Program #include #include #include #include #define round(a) ((int)(a+0.5)) void ellipseplot(int,int,int,int); int xcenter,yc,rx,ry; void main() { int gd=DETECT,gm; int xcenter,ycenter,rx,ry; long rx2,ry2,tworx2,twory2,p,x,y,px,py; initgraph(&gd,&gm,"..\\BGI:"); printf("\nEnter the x-coordinate of the centre point :"); scanf("%d",&xcenter); printf("\nEnter the y-coordinate of the centre point :"); scanf("%d",&ycenter); printf("\nEnter the x-coordinate of the radius :"); scanf("%d",&rx); printf("\nEnter the y-coordinate of the radius :"); scanf("%d",&ry); rx2=rx*rx; ry2=ry*ry; twory2=2*ry2; tworx2=2*rx2; x=0; y=ry; py=tworx2*y; px=0; ellipseplot(xcenter,ycenter,x,y); p=round(ry2-(rx2*ry)+(0.25*rx2)); while(pxy--; py=py-tworx2; p=p+ry2+px-py; } ellipseplot(xcenter,ycenter,x,y); } p=round(ry2*(x+0.5)*(x+0.5)+rx2*(y-1)*(y-1)-rx2*ry2); while(y>0) { y--; py=py-tworx2; if(p>0) p=p+rx2-py; else { x++; px=px+twory2; p=p+rx2-py+px; } ellipseplot(xcenter,ycenter,x,y); } getch(); closegraph(); } void ellipseplot(int xcenter,int ycenter,int x,int y) { putpixel(xcenter +x,ycenter +y,20); putpixel(xcenter -x,ycenter +y,20); putpixel(xcenter +x,ycenter -y,20); putpixel(xcenter -x,ycenter -y,20); } Output Enter the x-coordinate of the centre point : 100 Enter the y-coordinate of the centre point : 200 Enter the x-coordinate of the radius : 50 Enter the y-coordinate of the radius : 60 Ex.No:2f Program #include #include #include #include void ellipseplot(int,int,int,int); int xcenter,yc,rx,ry; void main() { int gd=DETECT,gm; long xcenter,ycenter,rx,ry; long rx2,ry2,tworx2,twory2,d1,d2,x,y,dx,dy; initgraph(&gd,&gm,"..\\BGI:"); printf("\nEnter the x-coordinate of the centre point :"); scanf("%ld",&xcenter); printf("\nEnter the y-coordinate of the centre point :"); scanf("%ld",&ycenter); printf("\nEnter the x-coordinate of the radius :"); scanf("%ld",&rx); printf("\nEnter the y-coordinate of the radius :"); scanf("%ld",&ry); clrscr(); rx2=rx*rx; ry2=ry*ry; twory2=2*ry2; tworx2=2*rx2; x=0; y=ry; d1=ry2-rx2*ry+(0.25*rx2); dx=twory2*x; dy=tworx2*y; do { ellipseplot(xcenter,ycenter,x,y); if(d1d1=d1+dx+ry2; } else { x=x+1; y=y-1; dx=dx+twory2; dy=dy-tworx2; d1=d1+dx-dy+ry2; } } while(dx0) { x=x; y=y-1; dy=dy-tworx2; d2=d2-dy+rx2; } else { x=x+1; y=y-1; dy=dy-tworx2; dx=dx+twory2; d2=d2+dx-dy+rx2; } } while(y>0); getch(); closegraph(); } void ellipseplot(int xcenter,int ycenter,int x,int y) { putpixel(xcenter+x,ycenter+y,15); putpixel(xcenter-x,ycenter+y,15); putpixel(xcenter+x,ycenter-y,15); putpixel(xcenter-x,ycenter-y,15); } Output Enter the x-coordinate of the centre point : 100 Enter the y-coordinate of the centre point : 200 Enter the x-coordinate of the radius : 50 Enter the y-coordinate of the radius : 60 Ex. No. 3a : Program #include #include #include #include #include void main() { int gd=DETECT,gm; int ch; clrscr(); while(1) { printf("******Line Styles*******\n"); printf("\n1.Solid Line"); printf("\n2.Dotted Line"); printf("\n3.Center Line"); printf("\n4.Dashed Line"); printf("\n5.Userbit Line"); printf("\n6.Exit"); printf("\n\nEnter your choice:\n"); scanf("%d",&ch); switch(ch) { case 1: clrscr(); initgraph(&gd,&gm," "); setlinestyle(0,1,3); line(100,30,250,250); getch(); cleardevice();closegraph(); break; case 2: clrscr(); initgraph(&gd,&gm," "); clrscr(); setlinestyle(1,1,3); line(100,30,250,250); getch(); cleardevice();closegraph(); break; case 3: clrscr(); initgraph(&gd,&gm," "); setlinestyle(2,1,3); line(100,30,250,250); getch(); cleardevice();closegraph(); break; case 4: clrscr(); initgraph(&gd,&gm," "); setlinestyle(3,1,3); line(100,30,250,250); getch(); cleardevice(); closegraph(); break; case 5: clrscr(); initgraph(&gd,&gm," "); setlinestyle(4,1,3); line(100,30,250,250); getch(); cleardevice();closegraph(); break; case 6: exit(0); } } } OUTPUT ******Line Styles******* 1.Solid Line 2.Dotted Line 3.Center Line 4.Dashed Line 5.Userbit Line 6.Exit Enter Ur Choice: 1 Solid Line Entrer ur Choice: 2 Dotted Line .. Enter ur choice : 3 Enter ur Choicre: 6 Ex.No:3b Program #include #include #include #include #include void main() { int gd=DETECT,gm; int ch; clrscr(); while(1) { printf("******Circle Attributes*******\n"); printf("\n1.Empty Fill"); printf("\n2.Soild Fill"); printf("\n3.Line Fill"); printf("\n4.Wide dot Fill"); printf("\n5.close dot Fill"); printf("\n6.User Fill"); printf("\n7.Exit"); printf("\n\nEnter your choice:\n"); scanf("%d",&ch); switch(ch) { case 1: clrscr(); initgraph(&gd,&gm," "); setfillstyle(EMPTY_FILL, RED); circle(100, 100, 50); floodfill(100, 100, WHITE); getch(); cleardevice(); closegraph(); break; case 2: clrscr(); initgraph(&gd,&gm," "); setfillstyle(SOLID_FILL, RED); circle(100, 100, 50); floodfill(100, 100, WHITE); getch(); cleardevice(); closegraph(); break; case 3: clrscr(); initgraph(&gd,&gm," "); setfillstyle(LINE_FILL, RED); circle(100, 100, 50); floodfill(100, 100, WHITE); getch(); cleardevice(); closegraph(); break; case 4: clrscr(); initgraph(&gd,&gm," "); setfillstyle(WIDE_DOT_FILL, RED); circle(100, 100, 50); floodfill(100, 100, WHITE); getch(); cleardevice();closegraph(); break; case 5: clrscr(); initgraph(&gd,&gm," "); setfillstyle(CLOSE_DOT_FILL, RED); circle(100, 100, 50); floodfill(100, 100, WHITE); getch(); cleardevice();closegraph(); break; case 6: clrscr(); initgraph(&gd,&gm," "); setfillstyle(USER_FILL, RED); circle(100, 100, 50); floodfill(100, 100, WHITE); getch(); cleardevice();closegraph(); break; case 7: exit(0); } } } OUTPUT "******Circle Attributes******* 1.Empty Fill 2.Soild Fill 3.Line Fill 4.Wide dot Fill 5.close dot Fill 6.User Fill 7.Exit Enter your choice: 1 Enter Ur Choice:2 Enter Ur Choice:3 Entrer ur Choice:6 Ex.No:3c Program: #include #include #include #include #include void main() { int gd=DETECT,gm; int ch; clrscr(); while(1) { printf("******Ellipse Attributes*******\n"); printf("\n1.Empty Fill"); printf("\n2.Soild Fill"); printf("\n3.Line Fill"); printf("\n4.Wide dot Fill"); printf("\n5.close dot Fill"); printf("\n6.User Fill"); printf("\n7.Exit"); printf("\n\nEnter your choice:\n"); scanf("%d",&ch); switch(ch) { case 1: clrscr(); initgraph(&gd,&gm," "); setfillstyle(EMPTY_FILL, RED); ellipse(100, 100,0,360,50,25); floodfill(100, 100, WHITE); getch(); cleardevice(); closegraph(); break; case 2: clrscr(); initgraph(&gd,&gm," "); setfillstyle(SOLID_FILL, RED); ellipse(100, 100,0,360,50,25); floodfill(100, 100, WHITE); getch(); cleardevice(); closegraph(); break; case 3: clrscr(); initgraph(&gd,&gm," "); setfillstyle(LINE_FILL, RED); ellipse(100, 100,0,360,50,25); floodfill(100, 100, WHITE); getch(); cleardevice(); closegraph(); break; case 4: clrscr(); initgraph(&gd,&gm," "); setfillstyle(WIDE_DOT_FILL, RED); ellipse(100, 100,0,360,50,25); floodfill(100, 100, WHITE); getch(); cleardevice(); closegraph(); break; case 5:clrscr(); initgraph(&gd,&gm," "); setfillstyle(CLOSE_DOT_FILL, RED); ellipse(100, 100,0,360,50,25); floodfill(100, 100, WHITE); getch(); cleardevice(); closegraph(); break; case 6: clrscr(); initgraph(&gd,&gm," "); setfillstyle(USER_FILL, RED); ellipse(100, 100,0,360,50,25); floodfill(100, 100, WHITE); getch(); cleardevice();closegraph(); break; case 7: exit(0); } } } OUTPUT: "******Ellipse Attributes******* 1.Empty Fill 2.Soild Fill 3.Line Fill 4.Wide dot Fill 5.close dot Fill 6.User Fill 7.Exit Entrer ur choice: 1 Entrer ur choice: 2 Enter ur choice : 6 Ex.No.4 Program #include #include #include #include #include #include void menu(); void input(); void output(); void translation(); void rotation(); void scaling(); void shearing(); void reflection(); int a[10][2],i,x,option,temp,angle,tx,ty,fx,fy,sh,k,n,axis,y; float sx,sy; void menu() { printf("menu\n"); printf("1.Translation\n"); printf("2.rotation\n"); printf("3.scaling\n"); printf("4.shearing\n"); printf("5.reflection\n"); printf("6.exit\n"); printf("enter the choice:"); scanf("%d",&option); switch(option) { case 1: input(); translation(); break; case 2: input(); rotation(); break; case 3: input(); scaling(); break; case 4 : input(); shearing(); break; case 5: input(); reflection(); break; case 6: exit(0); break; } } void input() { printf("enter the number of vertices:" ); scanf("%d",&n); for(i=0;ifor(i=0;iscanf("%d",&y); printf("enter the pivot point:"); scanf("%d%d",&fx,&fy); k=(y*3.14)/180; for(i=0;ia[i][1]=a[i][1]*sy+fy*(1-sy); } output(); delay(10); menu(); } void shearing() { output(); printf("enter the shear value:"); scanf("%d",&sh); printf("enter the fixed point:"); scanf("%d%d",&fx,&fy); printf("enter the axis for shearing if x-axis then 1 if y-axis the 0:"); scanf("%d",&axis); for(i=0;ia[i][1]=a[i][1]+sh*(a[i][0]-fx); } } output(); delay(10); menu(); } void reflection() { output(); for(i=0;iinitgraph(&gd,&gm,"c:\\tcplus\\bgi"); menu(); getch(); } OUTPUT Menu 1. Translation 2. Rotation 3. Scaling 4. Shearing 5. Reflection 6. Exit TRANSLATION Enter the choice : 1 Enter the number of Vertices: 3 Enter the coordinates : 30 150 10 200 Enter the coordinates : 10 200 60 200 Enter the coordinates : 60 200 30 150 Enter the translation vector Tx, Ty : 90 60 ROTATION Enter the choice : 2 Enter the number of Vertices: 3 Enter the coordinates : 30 150 10 200 Enter the coordinates : 10 200 60 200 Enter the coordinates : 60 200 30 150 Enter the Rotating Angle : 90 Enter the Pivot Point : 100 200 ` SCALING Enter the choice : 3 Enter the number of Vertices: 3 Enter the coordinates : 30 150 10 200 Enter the coordinates : 10 200 60 200 Enter the coordinates : 60 200 30 150 Enter the scaling Factor : 0.3 0.4 Enter the Fixed Point : 100 200 SHEARING Enter the choice : 4 Enter the number of Vertices: 3 Enter the coordinates : 30 150 10 200 Enter the coordinates : 10 200 60 200 Enter the coordinates : 60 200 30 150 Enter the shear Value : 5 Enter the fixed point : 50 100 Enter the Axis for shearing if x-axis then 1 if y-axis then 0 REFLECTION Enter the choice : 5 Enter the number of Vertices: 3 Enter the coordinates : 30 150 10 200 Enter the coordinates : 10 200 60 200 Enter the coordinates : 60 200 30 150 Ex.No:5 Program #include "Stdio.h" #include "conio.h" #include "math.h" #include "graphics.h" struct point { int x; int y; }; typedef float matrix[3][3]; matrix tm; void Identity( matrix m) { int i,j; for(i=0;ivoid scale(float sx,float sy, struct point refpt) { matrix m; Identity(m); m[0][0]=sx; m[0][2]=(1-sx)*refpt.x; m[1][1]=sy; m[1][2]=(1-sy)*refpt.y; multiply(m,tm); } void rotate(float a , struct point refpt) { matrix m; Identity(m); a=(a*3.14)/180.0; m[0][0]=cos(a); m[0][1]=-sin(a); m[0][2]=refpt.x*(1-cos(a))+refpt.y*sin(a); m[1][0]=sin(a); m[1][1]=-cos(a); m[1][2]=refpt.y*(1-cos(a))+refpt.x*sin(a); multiply(m,tm); } void transform(int npts, struct point *pts) { int k; float tmp; for(k=0;kdrawpoly(4,pts); getch(); Identity(tm); scale(0.5,0.5,refpt); rotate(90.0,refpt); translate(100,100); transform(4,pts); setcolor(WHITE); drawpoly(4,pts); getch(); closegraph(); } Output Ex.No:6 Program #include #include #include #define LEFT_EDGE 0x1 #define RIGHT_EDGE 0x2 #define BOTTOM_EDGE 0x4 #define TOP_EDGE 0x8 #define INSIDE(a) (!a) #define REJECT(a,b) (a&b) #define ACCEPT(a,b) (!(a|b)) #define FALSE 0 #define TRUE 1 struct point { int x; int y; } p1,p2,tmp; struct win { int x; int y; } winmin,winmax; unsigned char encode(struct point pt,struct win winmin,struct win winmax) { unsigned char code=0x00; if(pt.xwinmax.x) code=code|RIGHT_EDGE; if(pt.ywinmax.y) code=code|TOP_EDGE; return(code); } void swappts(struct point *p1,struct point *p2) { tmp=*p1; *p1=*p2; *p2=tmp; } void swapcodes(unsigned char *c1,unsigned char *c2) { unsigned char tmp; tmp=*c1; *c1=*c2; *c2=tmp; } void clipline(struct win winmin, struct win winmax,struct point p1,struct point p2) { unsigned char code1,code2; int done=FALSE,draw=FALSE; float m; while(!done) { code1=encode(p1,winmin,winmax); code2=encode(p2,winmin,winmax); if(ACCEPT(code1,code2)) { done=TRUE; draw=TRUE; } else if(REJECT(code1,code2)) done=TRUE; else { if(INSIDE(code1)) { swappts(&p1,&p2); swapcodes(&code1,&code2); } if(p2.x!=p1.x) m=(p2.y-p1.y)/(p2.x-p1.x); if(code1 &LEFT_EDGE) { p1.y+=(winmin.x-p1.x)*m; p1.x=winmin.x; } else if(code1 &RIGHT_EDGE) { p1.y+=(winmax.x-p1.x)*m; p1.x=winmax.x; } else if(code1 &BOTTOM_EDGE) { if(p2.x!=p1.x) p1.x+=(winmin.y-p1.y)/m; p1.y=winmin.y; } else if(code1 &TOP_EDGE) { if(p2.x!=p1.x) p1.x+=(winmax.y-p1.y)/m; p1.y=winmax.y; } } } if(draw) line(p1.x,p1.y,p2.x,p2.y); } void main() { int c,gm,gr; clrscr(); printf("\nEnter the window minimum coordinates"); scanf("%d%d",&winmin.x,&winmin.y); printf("\nEnter the window max coordinates"); scanf("%d%d",&winmax.x,&winmax.y); printf("\nEnter the starting point"); scanf("%d%d",&p1.x,&p1.y); printf("\nenter the end point"); scanf("%d%d",&p2.x,&p2.y); detectgraph(&gm,&gr); initgraph(&gm,&gr,"d:\\tc\\BGI"); printf("Before Clipping"); line(p1.x,p1.y,p2.x,p2.y); rectangle(winmin.x,winmax.y,winmax.x,winmin.y); getch(); clrscr(); cleardevice(); printf("After Clipping"); rectangle(winmin.x,winmax.y,winmax.x,winmin.y); clipline(winmin,winmax,p1,p2); getch(); } OUTPUT: Enter the clip window coordinates : 100 100 200 200 Enter the line coordinates : 0 0 500 500 Before clipping After clipping Ex.No:7 Program #include #include #include typedef enum { left,right,bottom,top } edge; #define N_EDGE 4 #define TRUE 1 #define FALSE 0 struct point { int x; int y; }p,wmin,wmax,p1,p2,ipt,i,pin[50],pout[50],first[50],s[50],i; int inside(struct point p,int b,struct point wmin,struct point wmax) { switch(b) { case left: if(p.xwmax.x) return (FALSE); break; case bottom: if(p.ywmax.y) return (FALSE); break; } return (TRUE); } int cross(struct point p1,struct point p2,int b,struct point wmin,struct point wmax) { if(inside(p1,b,wmin,wmax)==inside(p2,b,wmin,wmax)) return (FALSE); else return (TRUE); } struct point intersect(struct point p1,struct point p2,int b,struct point wmin,struct point wmax) { float m; if(p1.x!=p2.x) m=(p1.y-p2.y)/(p1.x-p2.x); switch(b) { case left: ipt.x=wmin.x; ipt.y=p2.y+(wmin.x-p2.x)*m; break; case right: ipt.x=wmax.x; ipt.y=p2.y+(wmax.x-p2.x)*m; break; case bottom: ipt.y=wmin.y; if(p1.x!=p2.x) ipt.x=p2.x+(wmin.y-p2.y)/m; else ipt.x=p2.x; break; case top: ipt.y=wmax.y; if(p1.x!=p2.x) ipt.x=p2.x+(wmax.y-p2.y)/m; else ipt.x=p2.x; break; } return(ipt); } void clippoint(struct point p,int b,struct point wmin,struct point wmax,struct point *pout,int *cnt,struct point *first[],struct point *s) { if(!first[b]) first[b]=&p; else if(cross(p,s[b],b,wmin,wmax)) { ipt=intersect(p,s[b],b,wmin,wmax); if(b{ struct point *first[N_EDGE]={0,0,0,0},s[N_EDGE]; int i,cnt=0; for(i=0;iOutput Enter the window minimum coordinates 200 200 Enter the window max coordinates 400 400 Enter the no of sides in polygon 3 Enter the coordinates(x,y)for pin ,pout 150 300 250 175 250 175 350 410 350 410 150 300 Ex.No:8 PROGRAM: Translation: #include #include #include #include void main(void) { int gd=DETECT,gm,errorcode; int l,t,r,b,tf,x,y,z,dp; initgraph(&gd,&gm,"c:\\tc\\bgi"); clrscr(); setcolor(9); setfillstyle(CLOSE_DOT_FILL,3); settextstyle(GOTHIC_FONT,HORIZ_DIR,3); outtextxy(200,10,"3D TRANSFORMATION"); bar3d(l=10,t=50,r=100,b=150,dp=15,tf=1); getch(); cleardevice(); printf("\nEnter the x and y values : "); scanf("%d%d",&x,&y); setcolor(14); outtextxy(150,150,"After Transformation"); setcolor(9); bar3d(l=x+10,t=y+50,r+x+100,b+y+150,15,1); getch(); setcolor(9); setfillstyle(CLOSE_DOT_FILL,3); bar3d(l=10,t=50,r=100,b=150,dp=15,tf=1); getch(); } OUTPUT: 3D TRANSFOTMATIONS Enter x ,y values:350,250 Rotation: #include #include #include #include int mx,my,mix,miy; void axis() { getch(); cleardevice(); line(mix,0,mix,my); line(0,miy,mx,miy); } void main() { int gd=DETECT,gm,x,y,z,ang,x1,x2,y1,y2,o; initgraph(&gd,&gm,"c:\\tc\\bgi"); setfillstyle(0,getmaxcolor()); mx=getmaxx(); my=getmaxy(); mix=mx/2; miy=my/2; axis(); bar3d(mix+50,miy-100,mix+60,miy-90,5,1); printf("\nEnter the rotation angle : "); scanf("%d",&o); x1=50*cos(o*3.14/180)-100*sin(o*3.14/180); y1=50*cos(o*3.14/180)-100*sin(o*3.14/180); x2=60*sin(o*3.14/180)-90*cos(o*3.14/180); y2=60*sin(o*3.14/180)+90*cos(o*3.14/180); axis(); printf("\nAfter rotation about z-axis"); bar3d(mix+x1,miy-y1,mix+x2,miy-y2,5,1); axis(); printf("\nAfter rotation about y-axis"); bar3d(mix+50,miy-x1,mix+60,miy-x2,5,1); axis(); printf("\nAftet rotation about x-axis"); bar3d(mix+x1,miy-100,mix+x2,miy-90,5,1); getch(); closegraph(); } OUTPUT: Enter the rotation angle: 60 After rotation about z-axis After rotation about y-axis After rotation about x-axis Scaling #include #include #include void main(void) { int gd=DETECT,gm,errorcode, l,t,r,b,tf,x,y,z,dp; initgraph(&gd,&gm,"c:\\tc\\bgi"); clrscr(); setcolor(9); setfillstyle(CLOSE_DOT_FILL,3); settextstyle(GOTHIC_FONT,HORIZ_DIR,3); outtextxy(200,10,"3D TRANSFORMATION"); bar3d(l=10,t=50,r=100,b=150,dp=15,tf=1); getch(); cleardevice(); printf("\n Enter the x ,y and z values : "); scanf("%d%d%d",&x,&y,&z); cleardevice(); setcolor(14); outtextxy(200,10,"AFTER SCALING"); setcolor(9); setfillstyle(CLOSE_DOT_FILL,3); bar3d(l=10,t=50,r=x+100,b=y+150,dp=z+15,tf=1); getch(); } OUTPUT: Original image: Enter x, y, and z: 20, 30, 40 Ex.No.9 PROGRAM: #include #include #include #include #include class cube { public: void drawcube(int x1[],int y1[]) { int i; for(i=0;iline(x1[4],y1[4],x1[7],y1[7]); } for(i=0;i>y>>z; coutsize; x1[0]=x1[3]=x; x1[1]=x1[2]=x+size; x1[4]=x1[7]=x; x1[5]=x1[6]=x+size; y1[0]=y1[1]=y; y1[2]=y1[3]=y+size; y1[4]=y1[5]=y; y1[6]=y1[7]=y+size; z1[1]=z1[2]=z1[3]=z1[0]=z ; z1[4]=z1[5]=z1[6]=z1[7]=z-size; for(i=0;icouttx>>ty>>tz; for(i=0;icase 2: couttheta; theta=(theta*3.14)/180; coutx3[i]=z1[i]*sin(theta)+x1[i]*cos(theta); } } else if(op==3) { for(i=0;igetch(); cleardevice(); coutsy>>sz; coutxf>>yf>>zf; for(i=0;icleardevice(); coutOUTPUT Enter the point in the cube: 100 100 100 Enter the size of the edge: 50 Menu 1. translation 2. rotation 3. scaling 4. exit Enter the choice:1 Enter the translation vector: 5,10,15 OpenGL to Work with Visual C++ Installation 1. Install Visual C++ 2008 Express Edition (To support OpenGL). 2. Copy all the .h files into the C:\Program Files\Microsoft SDKs\Windows\v6.1\Include\GL folder. Header Files (.h files) : Gl.h, glu.h, glut.h, freeglut.h, freeglut_ext.h , freeglut_std.h 3. Copy all the .lib files into the C:\Program Files\Microsoft SDKs\Windows\v6.1\Lib folder. Library files (.lib files) : opengl32.lib, glut32.lib, glu32.lib 4. Copy all the .dll files into the C:\Windows\system32 folder. Dynamic Link Library Files (.dll) : freeglut.dll , glut32.dll Working with Console Application Program in OpenGL 1. Creating a Project 1. Start Visual C++ and Under the File menu select New Project (Ctrl+Shift+N). 2. Select project types as Win32 and Win32 Console Application. 3. Give a User name for the Project. 4. Add a GLUT program to the project in the window. 2. Linking OpenGL Libraries 1. Under the Project menu select Project Properties (Alt+F7) at the bottom. 2. Select Configuration Properties Select C/C++ Preprocessor In preprocessor definitions additionally include the path where gl/glut.h is present. Example : C:\Program Files\Microsoft \ SDKs \Windows \v6.0A \Include 3. Select "Linker" folder and click "Input" . 4. Select All Configurations from the Configuration drop-down box at the top of the dialog. This ensures you are changing the settings for both the Debug and Release configurations. 5. Select "Additional Dependencies" and type the following contents: opengl32.lib glut32.lib glu32.lib 3. Compiling the Application Choose "Build" from the menu options. Select "Build filename". 4. Run the Application Choose "Debug" from the menu options. Select "Start without Debugging". 6. Save the Project Select File Menu select Save all (Ctrl+Shift+S). Save all the documents before quitting. Working with Windows Application Program in OpenGL 1. Creating a Project 1. Start Visual C++ and Under the File menu select New Project (Ctrl+Shift+N). 2. Select Win32 Project, enter a Username, and click OK. 3. In the Wizard click Next, then in Additional options check the box of Empty Project, and click Finish. 4. Under the Project menu select Add New Item (Ctrl+Shift+A). 5. Select C++ File (.cpp), enter a Name, and click OK. 6. Add a GLUT program to the project in the window. 2. Link to the OpenGL libraries: 1. Under the Project menu select Project Properties (Alt+F7) at the bottom. 2. Select Configuration Properties Linker Input from the navigation panel on the left. 3. Select All Configurations from the Configuration drop-down box at the top of the dialog. 4. Type opengl32.lib glut32.lib glu32.lib in Additional Dependencies and click OK. 1. Compiling the Application Choose "Build" from the menu options. Select "Build filename". 2. Run the Application Choose "Debug" from the menu options. Select "Start Without Debugging". 5. Save the Project Select File Menu select Save all (Ctrl+Shift+S). Save all the documents before quitting. Ex.No:10 Program #include "stdafx.h" #include #include void init (void) { GLfloat light_ambient[] = { 0.0, 0.0, 0.0, 1.0 }; GLfloat light_diffuse[] = { 1.0, 1.0, 1.0, 1.0 }; GLfloat light_specular[] = { 1.0, 1.0, 1.0, 1.0 }; GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 }; glLightfv (GL_LIGHT0, GL_AMBIENT, light_ambient); glLightfv (GL_LIGHT0, GL_DIFFUSE, light_diffuse); glLightfv (GL_LIGHT0, GL_SPECULAR, light_specular); glLightfv (GL_LIGHT0, GL_POSITION, light_position); glEnable (GL_LIGHTING); glEnable (GL_LIGHT0); glEnable(GL_DEPTH_TEST); } void display (void) { glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glPushMatrix (); glRotatef (20.0, 1.0, 0.0, 0.0); glPushMatrix (); glTranslatef (-0.75, 0.5, 0.0); glRotatef (90.0, 1.0, 0.0, 0.0); glutSolidTorus (0.275, 0.85, 15, 15); glPopMatrix (); glPushMatrix (); glTranslatef (-0.75, -0.5, 0.0); glRotatef (270.0, 1.0, 0.0, 0.0); glutSolidCone (1.0, 2.0, 15, 15); glPopMatrix (); glPushMatrix (); glTranslatef (0.75, 0.0, -1.0); glutSolidSphere (1.0, 15, 15); glPopMatrix (); glPopMatrix (); glFlush (); } void reshape(int w, int h) { glViewport (0, 0, (GLsizei) w, (GLsizei) h); glMatrixMode (GL_PROJECTION); glLoadIdentity (); if (w Output Ex.No:11a Program using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Data; using System.Text; using System.Windows.Forms; public class Form1 : Form { PictureBox picFractal=new PictureBox(); public Form1() { picFractal.BackColor = Color.White; picFractal.Dock = DockStyle.Fill; picFractal.Location = new Point(0, 0); picFractal.Name = "picFractal"; picFractal.Size = new Size(241, 223); picFractal.Click += new EventHandler(picFractal_Click); Controls.Add(picFractal); Name = "tsquare"; Text="Fractal T-Square"; Size = new Size(241, 223); WindowState = FormWindowState.Maximized; } public void GenerateTSquare(Graphics g, int iIterations, double iLeft, double iTop, double iWidth, double iHeight, Color oColor) { g.FillRectangle(new SolidBrush(oColor), (float)iLeft, (float)iTop, (float)iWidth, (float)iHeight); if (iIterations > 1) { double dNewWidth = iWidth / 2.0; double dNewHeight = iHeight / 2.0; GenerateTSquare(g, iIterations - 1, iLeft - (dNewWidth / 2.0), iTop - (dNewHeight / 2.0), dNewWidth, dNewHeight, oColor); GenerateTSquare(g, iIterations - 1, iLeft + iWidth - (dNewWidth / 2.0), iTop - (dNewHeight / 2.0), dNewWidth, dNewHeight, oColor); GenerateTSquare(g, iIterations - 1, iLeft - (dNewWidth / 2.0), iTop + iHeight - (dNewHeight / 2.0), dNewWidth, dNewHeight, oColor); GenerateTSquare(g, iIterations - 1, iLeft + iWidth - (dNewWidth / 2.0), iTop + iHeight - (dNewHeight / 2.0), dNewWidth, dNewHeight, oColor); } } public void DrawTSquare(int iIterations, Color oColor) { picFractal.Image = DrawTSquare(iIterations, oColor, picFractal.Width, picFractal.Height); } public Bitmap DrawTSquare(int iIterations, Color oColor, int iWidth, int iHeight) { Bitmap oImage = new Bitmap(iWidth, iHeight); Graphics g = Graphics.FromImage(oImage); GenerateTSquare(g, iIterations, (double)((iWidth - 2.0) / 4.0) + 1, ((iHeight - 2.0) / 4.0) + 1, (double)(iWidth - 2.0) / 2.0, (double)(iHeight - 2.0) / 2.0, oColor); return oImage; } public Image GetImage() { return picFractal.Image; } private void picFractal_Click(object sender, EventArgs e) { DrawTSquare(5, Color.Black); } public static void Main() { Application.Run(new Form1()); } } Output Ex.No:11b Program using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Data; using System.Text; using System.Windows.Forms; public class Form1 : Form { PictureBox picFractal=new PictureBox(); public Form1() { picFractal.BackColor = Color.Black; picFractal.Dock = DockStyle.Fill; picFractal.Location = new Point(0, 0); picFractal.Name = "picFractal"; picFractal.Size = new Size(241, 223); picFractal.Click += new EventHandler(picFractal_Click); Controls.Add(picFractal); Name = "Fern"; Text="Fractal Fern"; Size = new Size(241, 223); WindowState = FormWindowState.Maximized; } public void GenerateFern(Graphics g, int iIterations, double dStartX, double dStartY, double dScale, double[] dA, double[] dB, double[] dC, double[] dD, int[] iRand, Color oColor) { Random rnd = new Random(); int iRandNum; double dX = 0; double dY = 0; double dNewX = 0; double dNewY = 0; for (int i = 0; i < iIterations; i++) { iRandNum = rnd.Next(0, 100); if (iRandNum < iRand[0]) { dNewX = dA[0]; dNewY = dA[1] * dY; } else if (iRandNum < iRand[1]) { dNewX = (dB[0] * dX) + (dB[1] * dY) + dB[2]; dNewY = (dB[3] * dX) + (dB[4] * dY) + dB[5]; } else if (iRandNum < iRand[2]) { dNewX = (dC[0] * dX) + (dC[1] * dY) + dC[2]; dNewY = (dC[3] * dX) + (dC[4] * dY) + dC[5]; } else { dNewX = (dD[0] * dX) + (dD[1] * dY) + dD[2]; dNewY = (dD[3] * dX) + (dD[4] * dY) + dD[5]; } dX = dNewX; dY = dNewY; g.FillRectangle(new SolidBrush(oColor), (float)(dStartX + (dNewX * dScale)), (float)(dStartY - (dNewY * dScale)), 1, 1); } } public void DrawFern(int iIterations, double fScale, Color oColor) { double[] dA = new double[2]; double[] dB = new double[6]; double[] dC = new double[6]; double[] dD = new double[6]; int[] iRand = new int[4]; dA[0] = 0; dA[1] = 0.16; dB[0] = 0.2; dB[1] = -0.26; dB[2] = 0; dB[3] = 0.23; dB[4] = 0.22; dB[5] = 1.6; dC[0] = -0.15; dC[1] = 0.28; dC[2] = 0; dC[3] = 0.26; dC[4] = 0.25; dC[5] = 0.44; dD[0] = 0.85; dD[1] = 0.04; dD[2] = 0; dD[3] = -0.04; dD[4] = 0.85; dD[5] = 1.6; iRand[0] = 1; iRand[1] = 8; iRand[2] = 15; iRand[3] = 85; picFractal.Image = DrawFernImage(iIterations, fScale, oColor, dA, dB, dC, dD, iRand, picFractal.Width, picFractal.Height); } public void DrawFern(int iIterations, double fScale, Color oColor, double[] dA, double[] dB, double[] dC, double[] dD, int[] iRand) { picFractal.Image = DrawFernImage(iIterations, fScale, oColor, dA, dB, dC, dD, iRand, picFractal.Width, picFractal.Height); } public Bitmap DrawFernImage(int iIterations, double fScale, Color oColor, double[] dA, double[] dB, double[] dC, double[] dD, int[] iRand, int iWidth, int iHeight) { Bitmap oImage = new Bitmap(iWidth,iHeight); Graphics g = Graphics.FromImage(oImage); GenerateFern(g, iIterations, iWidth / 2.0, (double)iHeight, fScale, dA, dB, dC, dD, iRand, oColor); return oImage; } public Image GetImage() { return picFractal.Image; } private void picFractal_Click(object sender, EventArgs e) { DrawFern(40000, 60.0, Color.Red); } public static void Main() { Application.Run(new Form1()); } } Output :

Recommended

View more >