-
Notifications
You must be signed in to change notification settings - Fork 1
/
MatrixTransform.cpp~
139 lines (137 loc) · 3.44 KB
/
MatrixTransform.cpp~
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#include<iostream>
#include<graphics.h>
#include<math.h>
using namespace std;
void disp(int n, float c[][3])
{
float maxx, maxy;
int i;
maxx= getmaxx();
maxy= getmaxy();
maxx/=2;
maxy/=2;
i=0;
while(i<n-1)
{
line(maxx+c[i][0], maxy-c[i][1], maxx+c[i+1][0], maxy-c[i+1][1]);
i++;
}
i=n-1;
line(maxx+c[i][0], maxy-c[i][1], maxx+c[0][0], maxy-c[0][1]);
setcolor(15);
line(0, maxy, maxx*2,maxy);
line(maxx,0,maxx,maxy*2);
setcolor(WHITE);
}
void mul(int n, float b[][3], float c[][3], float a[][3])
{
int i,j,k;
for(i=0;i<n;i++)
for(j=0;j<3;j++)
{
a[i][j]=0;
}
for(i=0;i<n;i++)
for(j=0;j<3;j++)
for(k=0;k<3;k++)
{
a[i][j]+=c[i][k]*b[k][j];
}
}
void translation(int n, float c[][3], float tx, float ty)
{
int i;
for(i=0;i<n;i++)
{
c[i][0] = c[i][0]+tx;
c[i][1]=c[i][1]+ty;
}
}
void scaling(int n, float c[][3], float sx, float sy)
{
float b[10][3], a[10][3];
int i=0,j;
for(i=0;i<3;i++)
for(j=0;j<3;j++)
b[i][j]=0;
b[0][0]=sx;
b[1][1]=sy;
b[2][2]=1;
mul(n,b,c,a);
setcolor(BLUE);
disp(n,a);
}
void rotation(int n, float c[][3], float ra)
{
int i=0,j;
float b[10][3], xp, yp, a[10][3];
xp = c[0][0];
yp = c[0][1];
for(i=0;i<3;i++)
for(j=0;j<3;j++)
b[i][j]=0;
b[0][0]=cos(ra*3.14/180);
b[1][1]=cos(ra*3.14/180);
b[0][1]=sin(ra*3.14/180);
b[1][0]=-sin(ra*3.14/180);
b[2][0] = (-xp*cos(ra*3.14/180))+(yp*sin(ra*3.14/180))+xp;
b[2][1]= (-xp*sin(ra*3.14/180))-(yp*cos(ra*3.14/180))+yp;
b[2][2]=1;
mul(n,b,c,a);
setcolor(WHITE);
disp(n,a);
}
int main()
{
int i,j,k,cho,n,gd=DETECT,gm;
float c[10][3],tx,ty,sx,sy,ra;
initgraph(&gd,&gm,"");
printf("Enter number of vertices\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter coordinates of %d vertex \n",i+1);
scanf("%f%f",&c[i][0],&c[i][1]);
c[i][2]=1;
}
do
{
printf("\n1.Translation\n2.Scaling\n3.Rotation\n");
scanf("%d",&cho);
switch(cho)
{
case 1: printf("Enter factor\n");
scanf("%f%f",&tx,&ty);
setcolor(BLUE);
disp(n,c);
translation(n,c,tx,ty);
disp(n,c);
getch();
break;
case 2: printf("Enter factor\n");
scanf("%f%f",&sx,&sy);
setcolor(BLUE);
disp(n,c);
scaling(n,c,sx,sy);
disp(n,c);
getch();
break;
case 3: printf("Enter angle\n");
scanf("%f",&ra);
setcolor(BLUE);
disp(n,c);
rotation(n,c,ra);
disp(n,c);
getch();
default : break;
}
}
while(cho!=4);
getch();
int in = 0;
while (in == 0) {
in = getchar();
}
closegraph();
return 0;
}