-
Notifications
You must be signed in to change notification settings - Fork 0
/
triangle_transformation_tc.c
162 lines (116 loc) · 3.26 KB
/
triangle_transformation_tc.c
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void matrix_multi(int matA[3][3], int matB[3][3], int resultant_mat[3][3]){
int i,j, k, sum;
for(i=0; i<3; i++){
for(j=0; j<3; j++){
sum = 0;
for(k=0; k<3; k++){
sum += matA[i][k] * matB[k][j];
}
resultant_mat[i][j] = sum;
}
}
}
void set_shearing_factor(int mat[3][3], int shx, int shy){
mat[0][0] = 1;
mat[0][1] = shy;
mat[0][2] = 0;
mat[1][0] = shx;
mat[1][1] = 1;
mat[1][2] = 0;
mat[2][0] = 0;
mat[2][1] = 0;
mat[2][2] = 1;
}
void set_translation(int mat[3][3], int tx, int ty){
mat[0][0] = 1;
mat[0][1] = 0;
mat[0][2] = 0;
mat[1][0] = 0;
mat[1][1] = 1;
mat[1][2] = 0;
mat[2][0] = tx;
mat[2][1] = ty;
mat[2][2] = 1;
}
void set_rotation(int mat[3][3], int q){
mat[0][0] = cos(q);
mat[0][1] = sin(q);
mat[0][2] = 0;
mat[1][0] = -sin(q);
mat[1][1] = cos(q);
mat[1][2] = 0;
mat[2][0] = 0;
mat[2][1] = 0;
mat[2][2] = 1;
}
void draw_triangle(int mat[3][3]){
line(mat[0][0], mat[0][1], mat[1][0], mat[1][1]);
line(mat[1][0], mat[1][1], mat[2][0], mat[2][1]);
line(mat[2][0], mat[2][1], mat[0][0], mat[0][1]);
}
void set_reflection(int mat[3][3]){
mat[0][0] = 1;
mat[0][1] = 0;
mat[0][2] = 0;
mat[1][0] = 0;
mat[1][1] = -1;
mat[1][2] = 0;
mat[2][0] = 0;
mat[2][1] = 0;
mat[2][2] = 1;
}
int main(){
int gd=DETECT, gm;
float angle;
int orig_tri[3][3], shearing_mat[3][3], sheared_tri[3][3];
int translation_mat_1[3][3], translation_mat_2[3][3], rotation_mat[3][3];
int rotated_tri[3][3], reflected_tri[3][3], reflection_mat[3][3];
int xmax, ymax;
int i,j;
int shx, shy;
initgraph(&gd, &gm, " ");
printf("\n Enter triangle matrix : ");
for(i=0; i<3; i++) for(j=0; j<3; j++) scanf("%d", &orig_tri[i][j]);
/* for reflection :
xmax = getmaxx();
ymax = getmaxy();
line(xmax/2, 0, xmax/2, ymax);
line(0, ymax/2, xmax, ymax/2);
set_translation(translation_mat_1, -xmax/2, -ymax/2);
set_reflection(reflection_mat);
set_translation(translation_mat_2, xmax/2, ymax/2 );
matrix_multi(orig_tri, translation_mat_1, reflected_tri);
matrix_multi(reflected_tri, reflection_mat, reflected_tri);
matrix_multi(reflected_tri, translation_mat_2, reflected_tri);
draw_triangle(orig_tri);
draw_triangle(reflected_tri);
*/
/* for rotation :
printf("\n Enter the Rotation angle : ");
scanf("%d", &angle);
angle = (angle * 3.14) / 10;
set_translation(translation_mat_1, -orig_tri[0][0], -orig_tri[0][1]);
set_rotation(rotation_mat, angle);
set_translation(translation_mat_2, orig_tri[0][0], orig_tri[0][1]);
matrix_multi(orig_tri, translation_mat_1, rotated_tri);
matrix_multi(rotated_tri, rotation_mat, rotated_tri);
matrix_multi(rotated_tri, translation_mat_2, rotated_tri);
draw_triangle(orig_tri);
draw_triangle(rotated_tri);
*/
/*
for shearing :
printf("\n Enter the shearing factor : ");
scanf("%d%d", &shx, ­);
set_shearing_factor(shearing_mat, shx, shy);
matrix_multi(orig_tri, shearing_mat, sheared_tri);
draw_triangle(orig_tri);
draw_triangle(sheared_tri);
*/
getch();
closegraph();
return 0;
}