2D Transformation - Reflection about X and Y axis and Arbitrary Axis
#include<stdio.h> #include<conio.h> #include<graphics.h> void main() { int a,a1,b,b1,c,c1,xt,ch; int gd=DETECT,gm; initgraph(&gd,&gm,"c:\\tc\\bgi"); a=getmaxx(); a1=getmaxy(); b=a/2; b1=a1/2; line(b,0,b,a1); line(0,b1,a,b1); line(400,200,600,200); line(400,200,400,100); line(400,100,600,200); printf("1.origin\n"); printf("2.x-axis\n"); printf("3.y-axis\n"); printf("4.exit\n"); do { printf("Enter your choice\n"); scanf("%d",&ch); switch(ch) { case 1: c=400-b;c1=200-b1; line(b-c,b1-c1,b-c-200,b1-c1); line(b-c,b1-c1,b-c,b1-c1+100); line(b-c,b1-c1+100,b-c-200,b1-c1); break; case 2: c=400-b;c1=200-b1; line(b+c,b1-c1,b+c+200,b1-c1); line(b+c,b1-c1,b+c,b1-c1+100); line(b+c,b1-c1+100,b+c+200,b1-c1); break; case 3: c=400-b;c1=200-b1; line(b-c,b1+c1,b-c-200,b1+c1); line(b-c,b1+c1,b-c,b1+c1-100); line(b-c,b1+c1-100,b-c-200,b1+c1); break; } }while(ch<4); getch(); closegraph(); }Another Program
#include<stdio.h> #include<conio.h> #include<graphics.h> void accept(int a[30][3],int n) { int i; printf("\nEnter the coordinates of vertices:\n"); for(i=0;i<n;i++) { scanf("%d%d",&a[i][0],&a[i][1]); } } void mul(int a[30][3],int s[3][3],int m[30][3],int n) { int i,j,k; for(i=0;i<n;i++) { for(j=0;j<3;j++) { m[i][j]=0; for(k=0;k<3;k++) m[i][j]+=(a[i][k]*s[k][j]); } } } void display(int a[30][3],int m[30][3],int n) { int i,mx,my; cleardevice(); mx=getmaxx()/2; my=getmaxy()/2; setcolor(1); line(0,my,getmaxx(),my); line(mx,0,mx,getmaxy()); for(i=10;i<=mx;i=i+10) { putpixel(mx+i,my+1,4); putpixel(mx+i,my-1,4); putpixel(mx-i,my+1,4); putpixel(mx-i,my-1,4); putpixel(mx+1,my+i,4); putpixel(mx+1,my-i,4); putpixel(mx-1,my+i,4); putpixel(mx-1,my-i,4); } for(i=0;i<n-1;i++) { setcolor(2); line(mx+a[i][0],my-a[i][1],mx+a[i+1][0],my-a[i+1][1]); setcolor(5); line(mx+m[i][0],my-m[i][1],mx+m[i+1][0],my-m[i+1][1]); } setcolor(2); line(mx+a[i][0],my-a[i][1],mx+a[0][0],my-a[0][1]); setcolor(5); line(mx+m[i][0],my-m[i][1],mx+m[0][0],my-m[0][1]); } void main() { int gd=DETECT,gm,n,a[30][3],ch,s[3][3],m[30][3],i,j; initgraph(&gd,&gm,"c:\\tc\\bgi"); printf("\nEnter the edges of polygon\n"); scanf("%d",&n); accept(a,n); do { printf("\nEnter your choice: \n1.Reflection about x-axis \n2.Reflection about y-axis \n3.Reflection about line y=x \n4.Reflection about y=-x \n5.Reflection about origin\n6.Exit\n"); scanf("%d",&ch); for(i=0;i<3;i++) { for(j=0;j<3;j++) s[i][j]=0; } s[2][2]=1; switch(ch) { case 1: cleardevice(); s[0][0]=1; s[1][1]=-1; mul(a,s,m,n); display(a,m,n); break; case 2: cleardevice(); s[0][0]=-1; s[1][1]=1; mul(a,s,m,n); display(a,m,n); break; case 3: cleardevice(); s[0][1]=1; s[1][0]=1; mul(a,s,m,n); display(a,m,n); break; case 4: cleardevice(); s[0][1]=-1; s[1][0]=-1; mul(a,s,m,n); display(a,m,n); break; case 5: cleardevice(); s[0][0]=-1; s[1][1]=-1; mul(a,s,m,n); display(a,m,n); break; } }while(ch!=6); getch(); closegraph(); }
Comments
Post a Comment