/*Program no. 1 Program to implement the following using built-in functions available in the library 1.Kite flying 2.Bar Graph 3.Spiral Model 4.Fish Movement 5.Rotation of wheel 6.Moving car*/ #include #include #include #include #include #include #include #define RAD 3.141592/180 class fig { private: int x1,x2,y1,y2,xinc,yinc; public: void kite() { x1=y1=10;x2=x1+40;y2=y1+40; int poly[]={30,10,50,30,30,50,10,30}; int sang=240; xinc=yinc=10; setcolor(4); setfillstyle(1,5); fillpoly(4,poly); setcolor(3); arc(x1+20,y2,45,135,28); line(x1+20,y1,x1+20,y2); int s=imagesize(x1,y1,x2,y2); void *buf=malloc(s); getimage(x1,y1,x2,y2,buf); setlinestyle(SOLID_LINE,1,1); line(x1+20,y1+10,0,478); setlinestyle(SOLID_LINE,1,3); arc(x1-10,y2,sang,360,30); while(!kbhit()) { if(getpixel(x1+20,y1)!=0) putimage(x1,y1,buf,XOR_PUT); setcolor(0); setlinestyle(SOLID_LINE,1,1); line(x1+20,y1+10,0,478); setlinestyle(SOLID_LINE,1,3); arc(x1-10,y2,sang,360,30); setcolor(3); if(sang>=240 && sang<300) sang+=10; else if(sang>240 && sang>=300) sang-=10; x1+=xinc;y1+=yinc; x2+=xinc;y2+=yinc; if(x2>=50 && x2<(getmaxx()-20) && y2>=50 && y2<(getmaxy()-20)) { putimage(x1,y1,buf,COPY_PUT); arc(x1-10,y2,sang,360,30); setlinestyle(SOLID_LINE,1,1); line(x1+20,y1+10,0,478); delay(200); continue; } if(!(x2>=50 && x2<(getmaxx()-20))) { xinc*=(-1); x1+=xinc;x2=x1+40;} if(!(y2>=50 && y2<(getmaxy()-20))) { yinc*=(-1); y1+=yinc;y2=y1+40;} } free(buf); getch(); } void fish() { x1=y1=10; x2=x1+70;y2=y1+40; int flag=1; xinc=10;yinc=20; int poly[]={x1,y1+10,x1+12,y1+16,x1+12,y1+22,x1,y1+26,x1+5,y1+19}; int poly1[]={x1+45,y1+7,x1+25,y1+9,x1+28,y1}; int poly2[]={x1+45,y2-7,x1+25,y2-9,x1+28,y2}; int poly3[]={x2,y1+20,x2,y1+24,x2-3,y1+22}; setcolor(6); setfillstyle(LTBKSLASH_FILL,4); fillellipse(x1+40,y1+20,30,12); //eyes setcolor(9); circle(x2-10,y2-22,3); circle(x2-10,y2-22,2); floodfill(x2-10,y2-22,9); arc(x2-10,y2-23,120,240,10); setfillstyle(SLASH_FILL,MAGENTA); pieslice(x2-20,y2-20,135,210,14); fillpoly(5,poly); fillpoly(3,poly1); fillpoly(3,poly2); fillpoly(3,poly3); int size=imagesize(x1,y1,x2,y2); void *buf=malloc(size); getimage(x1,y1,x2,y2,buf); //changed view poly[0]=x1+5; poly[6]=x1+5; poly[8]=x1+8; fillpoly(5,poly); poly1[5]=y1+3; poly2[5]=y2-3; fillpoly(3,poly1); fillpoly(3,poly2); size=imagesize(x1,y1,x2,y2); void *buf1=malloc(size); getimage(x1,y1,x2,y2,buf1); while(!kbhit()) { if(flag) putimage(x1,y1,buf1,XOR_PUT); else putimage(x1,y1,buf,XOR_PUT); x1+=xinc;x2+=xinc; if(x2<(getmaxx()-10)) { if(flag) putimage(x1,y1,buf,COPY_PUT); else putimage(x1,y1,buf1,COPY_PUT); } else { cleardevice(); x1=10;x2=x1+70; y1+=yinc;y2+=yinc; if(y2<(getmaxy()-10)) { if(flag) putimage(x1,y1,buf,COPY_PUT); else putimage(x1,y1,buf1,COPY_PUT); } else {y1=10;y2=y1+40;} } flag=(flag+1)%2; delay(250); } free(buf); free(buf1); getch(); } void wheel() { int mx,my; mx=getmaxx()/2; my=getmaxy()/2; double x1=mx, y1=my-50.0; double x2=mx+43.30127, y2=my+25.0; double x3=mx-43.30127, y3=my+25.0; while(!kbhit()) { // cleardevice(); x1-=mx, x2-=mx, x3-=mx; y1-=my, y2-=my, y3-=my; x1=x1*cos(1*RAD)-y1*sin(1*RAD); x2=x2*cos(1*RAD)-y2*sin(1*RAD); x3=x3*cos(1*RAD)-y3*sin(1*RAD); y1=x1*sin(1*RAD)+y1*cos(1*RAD); y2=x2*sin(1*RAD)+y2*cos(1*RAD); y3=x3*sin(1*RAD)+y3*cos(1*RAD); x1+=mx, x2+=mx, x3+=mx; y1+=my, y2+=my, y3+=my; setcolor(WHITE); circle(mx,my,55); circle(mx,my,50); circle(mx,my,5); setfillstyle(SOLID_FILL,WHITE); floodfill(mx, my, WHITE); line(mx,my,x1,y1); line(mx,my,x2,y2); line(mx,my,x3,y3); delay(10); setcolor(BLACK); line(mx,my,x1,y1); line(mx,my,x2,y2); line(mx,my,x3,y3); } // closegraph(); // restorecrtmode(); getch(); } void spiral() { float a=1,theta=0,alpha=0.1; int r,x,y,i=1; int sx=getmaxx()/2; int sy=getmaxy()/2; while(!kbhit()) { r=a*exp(theta*alpha); if(r<10) theta+=0.05; else if(r<100) theta+=0.001; else theta+=0.003; x=sx+r*cos(2*theta); y=sy+r*sin(2*theta); putpixel(x,y,i); if(r>=350) {theta=0; i=(i+1)%14 +1; delay(200); } } getch(); } void car() { xinc=10;yinc=10; x1=y1=10; x2=x1+90;y2=y1+35; int poly[]={x1+5,y1+10,x1+15,y1+10,x1+20,y1,x1+50,y1,x1+60,y1+10,x1+90,y1+17,x1+90,y1+20,x1+5,y1+20,x1+5,y1+10}; setfillstyle(SOLID_FILL,LIGHTGRAY); setlinestyle(SOLID_LINE,1,2); setcolor(4); drawpoly(9,poly); line(x1+15,y1+10,x1+60,y1+10); line(x1+20,y1+10,x1+20,y1); line(x1+35,y1+10,x1+35,y1); line(x1+50,y1+10,x1+50,y1); floodfill(x1+18,y1+8,4); floodfill(x1+28,y1+8,4); floodfill(x1+36,y1+8,4); floodfill(x1+52,y1+8,4); setfillstyle(SOLID_FILL,4); floodfill(x1+18,y1+12,4); setfillstyle(SOLID_FILL,BLUE); bar(x1+5,y1+20,x1+90,y1+25); setcolor(DARKGRAY); circle(x1+20,y1+25,8); circle(x1+20,y1+25,6); setfillstyle(1,8); floodfill(x1+21,y1+25,8); circle(x1+70,y1+25,8); circle(x1+70,y1+25,6); floodfill(x1+71,y1+25,8); int size=imagesize(x1,y1,x2,y2); void far *buf=farmalloc(size); getimage(x1,y1,x2,y2,buf); while(!kbhit()) { putimage(x1,y1,buf,XOR_PUT); x1+=xinc;x2+=xinc; if(x2<(getmaxx()-10)) putimage(x1,y1,buf,COPY_PUT); else { cleardevice(); x1=10;x2=x1+90; y1+=yinc;y2+=yinc; if(y2<(getmaxy()-10)) { putimage(x1,y1,buf,COPY_PUT); } else {y1=10;y2=y1+35;} } delay(200); } farfree(buf); getch(); } void barg() { int n,i,j,k; int x=40,max=0,htfactor=1; char txt[10],t[10]; printf("\nEnter the no. of values:"); scanf("%d",&n); int *a=new int[n+1]; for(i=1;i<=n;i++) { printf("\nEnter value for item %d:",i); scanf("%d",&a[i-1]); if(max460) htfactor++; setcolor(4); setlinestyle(SOLID_LINE,1,3); line(30,10,30,460); line(30,460,getmaxx(),460); for(i=1;i<=n;i++) { setfillstyle(1,i); j=460-a[i-1]/htfactor; k=x+wid; bar(x,j,k,460); sprintf(txt,"%d",a[i-1]); sprintf(t,"%d",i); outtextxy(5,460-a[i-1]/htfactor,txt); outtextxy(x+wid/2,470,t); x=x+wid+10; } getch(); } }; void main() { int gd=DETECT,gm; initgraph(&gd,&gm,"d:\\cplus"); fig f; f.kite(); cleardevice(); f.fish(); cleardevice(); f.car(); cleardevice(); f.wheel(); cleardevice(); f.spiral(); cleardevice(); f.barg(); //restorecrtmode(); closegraph(); }