Graphics programming using c
Home
Question Paperz
Java Projectz
ASP Projectz
8086
Data structures
c++
8085
Monday, September 6, 2010
2D Transformation
Write a program to apply combination of transformation, rotation, reflection and shearing) on the following objects. a)Polygon b)Circle
#include<stdio.h> #include<graphics.h> int midx,midy; int polx[10],poly[10],n,pox,poy,r,rnew,polxnew[10],polynew[10],poxnew,poynew; float trans[3][3],result[3][1],xandy[3][1]; void screendiv() { int winx,winy; cleardevice(); winx=getmaxx(); winy=getmaxy(); line(winx/2,0,winx/2,winy); line(0,winy/2,winx,winy/2); midx=winx/2; midy=winy/2; } void readpoly() { int i; for(i=0;i<n;i++) { printf("\nEnter the %d co=ordinates ",i+1); scanf("%d%d",&polx[i],&poly[i]); } } void readcircle() { printf("Enter the orgin "); scanf("%d%d",&pox,&poy); printf("Enter the radius "); scanf("%d",&r); } void drawcircle(int *x,int *y,int *rad) { circle(midx+*x,midy-*y,*rad); } void drawpolygon(int *x,int *y) { int i; for(i=0;i<n;i++) line(midx+*(x+i),midy-*(y+i),midx+*(x+((i+1)%n)),midy-*(y+((i+1)%n))); } void mulmat() { int i,j,k; for(i=0;i<3;i++) { for(j=0;j<1;j++) { result[i][j]=0.0; for(k=0;k<3;k++) result[i][j]+=trans[i][k]*xandy[k][j]; } } } void transformcirc() { xandy[0][0]=pox; xandy[1][0]=poy; xandy[2][0]=1.0; mulmat(); poxnew=(int)result[0][0]; poynew=(int)result[1][0]; } void transformpoly() { int i; for(i=0;i<n;i++) { xandy[0][0]=polx[i]; xandy[1][0]=poly[i]; xandy[2][0]=1; mulmat(); polxnew[i]=result[0][0]; polynew[i]=result[1][0]; } } void makeIdentity() { int i,j; for(i=0;i<3;i++) { for(j=0;j<3;j++) trans[i][j]=0.0; trans[i][i]=1.0; } } int main() { int gd=DETECT,gm=VGAMAX; int choice,i,j,ch,dx,dy,rx,ry,chr; float sx,sy,ang; while(1) { makeIdentity(); printf("\nMENU\n-------\n1.CIRCLE\n2.POLYGON\n3.EXIT"); printf("\nEnter the choice "); scanf("%d",&choice); switch(choice) { case 1: readcircle(); initgraph(&gd,&gm,NULL); screendiv(); drawcircle(&pox,&poy,&r); break; case 2: printf("\nEnter the no of sides "); scanf("%d",&n); readpoly(); initgraph(&gd,&gm,NULL); screendiv(); drawpolygon(&polx[0],&poly[0]); break; case 3: exit(0); default:printf("\nInvalid choice "); } while (!kbhit()); closegraph(); printf("\nMENU\n-------\n1.TRANSLATE\n2.ROTATION\n3.SCALING\n4.REFLECTION\n5.SHEAR"); printf("\nEnter the choice "); scanf("%d",&ch); switch(ch) { case 1: printf("\ndx and dy"); scanf("%d%d",&dx,&dy); trans[0][2]=dx; trans[1][2]=dy; initgraph(&gd,&gm,NULL); if(choice==1) { transformcirc(); screendiv(); drawcircle(&pox,&poy,&r); drawcircle(&poxnew,&poynew,&r); } else { transformpoly(); screendiv(); drawpolygon(&polx[0],&poly[0]); drawpolygon(&polxnew[0],&polynew[0]); } break; case 2: printf("\nEnter the reference points "); scanf("%d%d",&rx,&ry); printf("\nEnter the angle "); scanf("%f",&ang); ang=(float)(ang*(3.14/180)); trans[0][0]=cos(ang); trans[0][1]=-sin(ang); trans[0][2]=rx*(1-cos(ang))+ry*sin(ang); trans[1][0]=sin(ang); trans[1][1]=cos(ang); trans[1][2]=ry*(1-cos(ang))-rx*sin(ang); initgraph(&gd,&gm,NULL); if(choice==1) { transformcirc(); screendiv(); drawcircle(&pox,&poy,&r); drawcircle(&poxnew,&poynew,&r); } else { transformpoly(); screendiv(); drawpolygon(&polx[0],&poly[0]); drawpolygon(&polxnew[0],&polynew[0]); } break; case 3: printf("\nEnter the reference points "); scanf("%d%d",&rx,&ry); if(choice==1) { printf("\nEnter the scaling factor "); scanf("%f",&sx); rnew=(float)r*sx; pox=rx; poy=ry; initgraph(&gd,&gm,NULL); screendiv(); drawcircle(&pox,&poy,&r); drawcircle(&poxnew,&poynew,&rnew); } else { printf("\nEnter the scaling factor,sx and sy"); scanf("%f%f",&sx,&sy); trans[0][0]=sx; trans[0][2]=(1-sx)*rx; trans[1][1]=sy; trans[1][2]=(1-sy)*ry; transformpoly(); initgraph(&gd,&gm,NULL); screendiv(); drawpolygon(&polx[0],&poly[0]); drawpolygon(&polxnew[0],&polynew[0]); } break; case 4: printf("\nMENU FOR REFLECTION\n-------\n1.X AXIS\n2.Y AXIS\n3.ORGIN\n4.Y=X\n5.Y=-X"); printf("\nEnter the choice "); scanf("%d",&chr); switch(chr) { case 1: trans[1][1]=-1; break; case 2: trans[0][0]=-1; break; case 3: trans[0][0]=-1; trans[1][1]=-1; break; case 4: trans[0][0]=0; trans[0][1]=1; trans[1][0]=1; trans[1][1]=0; break; case 5: trans[0][0]=0; trans[0][1]=-1; trans[1][0]=-1; trans[1][1]=0; break; default:printf("\nInvlalid choice"); break; } initgraph(&gd,&gm,NULL); if(choice==1) { transformcirc(); screendiv(); drawcircle(&pox,&poy,&r); drawcircle(&poxnew,&poynew,&r); } else { transformpoly(); screendiv(); drawpolygon(&polx[0],&poly[0]); drawpolygon(&polxnew[0],&polynew[0]); } break; case 5: if(choice==1) printf("\nShear not possible "); else { printf("\nMENU FOR SHEAR\n-------\n1.X DIRECTION SHEAR RELATIVE TO X AXIS\n2.X DIRECTION SHEAR RELATIVE TO REFERENCE\n3.Y DIRECTION SHEAR RELATIVE TO REFERENCE"); printf("\nEnter the choice "); scanf("%d",&chr); printf("\nEnter the shear factor "); scanf("%f",&sx); switch(chr) { case 1: trans[0][1]=sx; break; case 2: printf("\nEnter y reference point "); scanf("%d",&ry); trans[0][1]=sx; trans[0][2]=-1*sx*ry; break; case 3: printf("\nEnter x reference point "); scanf("%d",&rx); trans[1][0]=sx; trans[1][2]=-1*sx*rx; break; default:printf("\nInvlalid choice"); } initgraph(&gd,&gm,NULL); transformpoly(); screendiv(); drawpolygon(&polx[0],&poly[0]); drawpolygon(&polxnew[0],&polynew[0]); } break; default:printf("\nInvlalid choice"); } while (!kbhit()); closegraph(); } return 0; }
No comments:
Post a Comment
Newer Post
Older Post
Home
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment