Rotation And Scalling
This code demonstrates polygon scaling and rotation using C++ graphics libraries. First, it prompts the user to input the number of sides and coordinates of a polygon. It then applies scaling based on user-defined factors, displaying the scaled polygon. Next, it allows the user to enter an angle for rotation and performs the rotation on the polygon around the first vertex. The code utilizes graphics.h, which requires a Turbo C++ environment to run, providing a visual representation of geometric transformations in graphics programming.
Rotation And Scalling
E N D
Presentation Transcript
Rotation And Scalling By: Asima Latif
SCALING #include<iostream.h> #include<conio.h> #include<graphics.h> #include<dos.h> void main() { int gd=DETECT,gm,x[10],y[10],a,b,i,j,dx,dy; clrscr();
SCALING… initgraph(&gd,&gm,"c:\\tc\\bgi"); cout<<" To draw polygon enter the number of sides for polygon\t"; cin>>j; cout<<"\n Now enter co-ordinates for each point\n\t"; for(i=0;i<j;i++) cin>>x[i]>>y[i]; a=x[0]; b=y[0]; setcolor(BLUE); for(i=1;i<=j-1;i++) { line(x[i],y[i],x[i+1],y[i+1]); }
SCALING… line(x[i],y[i],a,b); cout<<"\n Enter the co-ordinates for scaling \t"; cin>>dx>>dy; cout<<"\n After scaling"; for(i=1;i<=j-1;i++) { line(x[i]*dx, y[i]*dy, x[i+1]*dx, y[i+1]*dy); } line(x[i]*dx, y[i]*dy, a*dx, b*dy); getch(); closegraph(); }
ROTATION • #include<iostream.h> • #include<conio.h> • #include<graphics.h> • #include<dos.h> • #include<math.h> • void main() • { • int gd=DETECT,gm,x[10],y[10],a,b,i,j,dx,dy,var1,var2,ang; • float d; • clrscr();
ROTATION… • initgraph(&gd,&gm,"c:\\tc\\bgi"); • cout<<" To draw polygon enter the number of sides for polygon\t"; • cin>>j; • cout<<"\n Now enter co-ordinates for each point\n\t"; • for(i=0;i<j;i++) • cin>>x[i]>>y[i]; • a=x[0]; b=y[0]; • setcolor(BLUE); • for(i=0;i<j-1;i++) • { • line(x[i],y[i],x[i+1],y[i+1]); • delay(500); • }
ROTATION… • line(x[i],y[i],a,b); • setcolor(RED); • cout<<"\n Enter the angle for rotation \t"; • cin>>ang; • cout<<"\n After rotation"; • d=(3.14*ang)/180 • var1=x[0]*cos(d)-y[0]*sin(d)-x[0]; • var2=x[0]*sin(d)+y[0]*cos(d)-y[0];
ROTATION… for(i=0;i<j-1;i++) { line( x[i]*cos(d)-y[i]*sin(d)-var1, x[i]*sin(d)+y[i]*cos(d)-var2, x[i+1]*cos(d)-y[i+1]*sin(d)-var1, x[i+1]*sin(d)+y[i+1]*cos(d)-var2 ); delay(500); } line( x[i]*cos(d)-y[i]*sin(d)-var1, x[i]*sin(d)+y[i]*cos(d)-var2, x[0]*cos(d)-y[0]*sin(d)-var1, x[0]*sin(d)+y[0]*cos(d)-var2 ); getch(); closegraph(); }