Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Title: 2D Transformation
- Description: C Program to draw any shape perform 2D Transformations like Translation, Scaling, Rotation Reflection & Shear on that shape.
- Author: Saideep Dicholkar
- */
- #include<stdio.h>
- #include<conio.h>
- #include<graphics.h>
- #include<math.h>
- int x=330,y=250;
- int a[4][2]={{50,50},{100,50},{100,100},{50,100}};
- void draw(int x,int y)
- {
- setcolor(WHITE);
- line(x,y-250,x,y+250);
- line(x-330,y,x+330,y);
- }
- void stat(int x,int y)
- {
- line(x+a[0][0],y-a[0][1],x+a[1][0],y-a[1][1]);
- line(x+a[1][0],y-a[1][1],x+a[2][0],y-a[2][1]);
- line(x+a[2][0],y-a[2][1],x+a[3][0],y-a[3][1]);
- line(x+a[3][0],y-a[3][1],x+a[0][0],y-a[0][1]);
- }
- void translate(int x,int y)
- {
- int tx,ty,xt,yt;
- cleardevice();
- printf("\nEnter translation factor (tx,ty): ");
- scanf("%d%d",&tx,&ty);
- cleardevice();
- draw(x,y);
- setcolor(WHITE);
- stat(x,y);
- xt=x+tx;
- yt=y+ty;
- getch();
- setcolor(GREEN);
- stat(xt,yt);
- }
- void rotate(int x,int y)
- {
- double d,x2r,y2r,x3r,y3r,x4r,y4r;
- cleardevice();
- printf("\nEnter Angle of Rotation: ");
- scanf("%lf",&d);
- cleardevice();
- draw(x,y);
- setcolor(WHITE);
- stat(x,y);
- d=(d*3.14)/180;
- x2r=a[0][0]+(((a[1][0]-a[0][0])*cos(d))-((a[1][1]-a[0][1])*sin(d)));
- y2r=a[0][1]+(((a[1][0]-a[0][0])*sin(d))+((a[1][1]-a[0][1])*cos(d)));
- x3r=a[0][0]+(((a[2][0]-a[0][0])*cos(d))-((a[2][1]-a[0][1])*sin(d)));
- y3r=a[0][1]+(((a[2][0]-a[0][0])*sin(d))+((a[2][1]-a[0][1])*cos(d)));
- x4r=a[0][0]+(((a[3][0]-a[0][0])*cos(d))-((a[3][1]-a[0][1])*sin(d)));
- y4r=a[0][1]+(((a[3][0]-a[0][0])*sin(d))+((a[3][1]-a[0][1])*cos(d)));
- getch();
- setcolor(GREEN);
- line(x+a[0][0],y-a[0][1],x+x2r,y-y2r);
- line(x+x2r,y-y2r,x+x3r,y-y3r);
- line(x+x3r,y-y3r,x+x4r,y-y4r);
- line(x+x4r,y-y4r,x+a[0][0],y-a[0][1]);
- }
- void scale(int x,int y)
- {
- int sx,sy;
- cleardevice();
- printf("\nEnter scaling factor (sx,sy): ");
- scanf("%d%d",&sx,&sy);
- cleardevice();
- draw(x,y);
- setcolor(WHITE);
- stat(x,y);
- getch();
- setcolor(GREEN);
- line(x+a[0][0]*sx,y-a[0][1]*sy,x+a[1][0]*sx,y-a[1][1]*sy);
- line(x+a[1][0]*sx,y-a[1][1]*sy,x+a[2][0]*sx,y-a[2][1]*sy);
- line(x+a[2][0]*sx,y-a[2][1]*sy,x+a[3][0]*sx,y-a[3][1]*sy);
- line(x+a[3][0]*sx,y-a[3][1]*sy,x+a[0][0]*sx,y-a[0][1]*sy);
- }
- void reflect(int x,int y)
- {
- int x1,y1,ch;
- printf("\n1.About X-axis\n2.About Y-axis:\nEnter choice: ");
- scanf("%d",&ch);
- cleardevice();
- draw(x,y);
- setcolor(WHITE);
- stat(x,y);
- getch();
- x1=-1;
- y1=1;
- setcolor(GREEN);
- if(ch==2)
- {
- line(x+a[0][0]*x1,y-a[0][1]*y1,x+a[1][0]*x1,y-a[1][1]*y1);
- line(x+a[1][0]*x1,y-a[1][1]*y1,x+a[2][0]*x1,y-a[2][1]*y1);
- line(x+a[2][0]*x1,y-a[2][1]*y1,x+a[3][0]*x1,y-a[3][1]*y1);
- line(x+a[3][0]*x1,y-a[3][1]*y1,x+a[0][0]*x1,y-a[0][1]*y1);
- }
- else
- {
- line(x-a[0][0]*x1,y+a[0][1]*y1,x-a[1][0]*x1,y+a[1][1]*y1);
- line(x-a[1][0]*x1,y+a[1][1]*y1,x-a[2][0]*x1,y+a[2][1]*y1);
- line(x-a[2][0]*x1,y+a[2][1]*y1,x-a[3][0]*x1,y+a[3][1]*y1);
- line(x-a[3][0]*x1,y+a[3][1]*y1,x-a[0][0]*x1,y+a[0][1]*y1);
- }
- }
- void shear(int x,int y)
- {
- int shr,sh1,sh2,shear;
- cleardevice();
- printf("\n1: Shear w.r.t X-axis\n2: Shear w.r.t Y-axis");
- printf("\nEnter choice: ");
- scanf("%d",&shear);
- printf("\nEnter shear value: ");
- scanf("%d",&shr);
- cleardevice();
- draw(x,y);
- setcolor(WHITE);
- stat(x,y);
- getch();
- setcolor(GREEN);
- switch(shear)
- {
- case 1:
- {
- sh1=shr*a[2][1];
- sh2=shr*a[3][1];
- line(x+a[0][0],y-a[0][1],x+a[1][0],y-a[1][1]);
- line(x+a[1][0],y-a[1][1],x+a[2][0]+sh1,y-a[2][1]);
- line(x+a[2][0]+sh1,y-a[2][1],x+a[3][0]+sh2,y-a[3][1]);
- line(x+a[3][0]+sh2,y-a[3][1],x+a[0][0],y-a[0][1]);
- break;
- }
- case 2:
- {
- sh1=shr*a[1][0];
- sh2=shr*a[2][0];
- line(x+a[0][0],y-a[0][1],x+a[1][0],y-a[1][1]-sh2);
- line(x+a[1][0],y-a[1][1]-sh2,x+a[2][0],y-a[2][1]-sh2);
- line(x+a[2][0],y-a[2][1]-sh2,x+a[3][0],y-a[3][1]);
- line(x+a[3][0],y-a[3][1],x+a[0][0],y-a[0][1]);
- break;
- }
- }
- }
- void main()
- {
- int ch;
- int gd=DETECT,gm;
- initgraph(&gd,&gm,"C:\\tc\\bgi");
- cleardevice();
- printf("\n1: TRANSLATION \n2: ROTATION \n3: SCALING \n4: REFLECTION \n5: SHEAR");
- printf("\nEnter choice: ");
- scanf("%d",&ch);
- switch(ch)
- {
- case 1: translate(x,y);
- break;
- case 2: rotate(x,y);
- break;
- case 3: scale(x,y);
- break;
- case 4: reflect(x,y);
- break;
- case 5: shear(x,y);
- break;
- default:printf("Wrong choice");
- break;
- }
- getch();
- closegraph();
- restorecrtmode();
- }
Add Comment
Please, Sign In to add comment