-
Notifications
You must be signed in to change notification settings - Fork 0
/
Matrix2D.c
119 lines (101 loc) · 2.61 KB
/
Matrix2D.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
#include "Matrix2D.h"
/*
将pResult设置为单位矩阵
*/
void Matrix2DIdentity(Matrix2D *pResult)
{
int i, j;
for (i = 0; i < 3; i++)
for (j = 0; j < 3; j++)
pResult->m[i][j] = (i==j)?1.f:0.f;
}
// ---------------------------------------------------------------------------
/*
计算pMtx的转置,并将结果放到pResult
*/
void Matrix2DTranspose(Matrix2D *pResult, Matrix2D *pMtx)
{
int i, j;
for (i = 0; i < 3; i++)
for (j = 0; j < 3; j++)
pResult->m[j][i] = pMtx->m[i][j];
}
// ---------------------------------------------------------------------------
/*
Result = Mtx0*Mtx1
*/
void Matrix2DConcat(Matrix2D *pResult, Matrix2D *pMtx0, Matrix2D *pMtx1)
{
int i, j, p;
float m[3][3] = { 0 };
for (i = 0; i < 3; i++)
for (j = 0; j < 3; j++)
for (p = 0; p < 3; p++)
m[i][j] = m[i][j] + pMtx0->m[i][p] * pMtx1->m[p][j];
for (i = 0; i < 3; i++)
for (j = 0; j < 3; j++)
pResult->m[i][j]=m[i][j];
}
// ---------------------------------------------------------------------------
/*
将pResult设置为平移矩阵,平移位移为x和y
*/
void Matrix2DTranslate(Matrix2D *pResult, float x, float y)
{
Matrix2DIdentity(pResult);
pResult->m[0][2] = x;
pResult->m[1][2] = y;
}
// ---------------------------------------------------------------------------
/*
将pResult设置为缩放矩阵,x和y分别为水平和垂直方向的缩放比例
*/
void Matrix2DScale(Matrix2D *pResult, float x, float y)
{
Matrix2DIdentity(pResult);
pResult->m[0][0] = x;
pResult->m[1][1] = y;
}
// ---------------------------------------------------------------------------
/*
将pResult设置为旋转矩阵,旋转量为Angle,为度数
*/
void Matrix2DRotDeg(Matrix2D *pResult, float Angle)
{
Matrix2DIdentity(pResult);
float a;
a = Angle * PI / 180;
pResult->m[0][0] = cos(a);
pResult->m[1][1] = cos(a);
pResult->m[0][1] = -sin(a);
pResult->m[1][0] = sin(a);
}
// ---------------------------------------------------------------------------
/*
将pResult设置为旋转矩阵,旋转量为Angle,为弧度
*/
void Matrix2DRotRad(Matrix2D *pResult, float Angle)
{
Matrix2DIdentity(pResult);
pResult->m[0][0] = cos(Angle);
pResult->m[1][1] = cos(Angle);
pResult->m[0][1] = -sin(Angle);
pResult->m[1][0] = sin(Angle);
}
// ---------------------------------------------------------------------------
/*
Result = Mtx * Vec
*/
void Matrix2DMultVec(Vector2D *pResult, Matrix2D *pMtx, Vector2D *pVec)
{
float x, y;
int i, j;
float m[3][1] = { 0 };
float v[3][1] = { pVec->x,pVec->y,1 };
for (i = 0; i < 3; i++)
for (j = 0; j < 3; j++)
m[i][0] = m[i][0] + pMtx->m[i][j] * v[j][0];
pResult->x = m[0][0];
pResult->y = m[1][0];
}
// ---------------------------------------------------------------------------