forked from alifahrri/biped-walking-sim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
matrix2.cpp
116 lines (99 loc) · 2.39 KB
/
matrix2.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
#include "matrix2.h"
#include <iostream>
#include <cmath>
Matrix2::Matrix2()
{
m[0]=1; m[1]=0;
m[2]=0; m[3]=1;
}
Matrix2::Matrix2(double m11, double m12, double m21, double m22){
m[0]=m11; m[1]=m12; m[2]=m21; m[3]=m22;
}
void Matrix2::identity(){
m[0]=1; m[1]=0;
m[2]=0; m[3]=1;
}
void Matrix2::transpose(){
double temp=m[1];
m[1]=m[2];
m[2]=temp;
}
double Matrix2::determinant(){
double ret;
ret = m[0]*m[3]-m[1]*m[2];
return ret;
}
Matrix2 Matrix2::inverse(){
double det = determinant();
Matrix2 temp(m[3]/det,-m[1]/det,-m[2]/det,m[0]/det);
return temp;
}
double Matrix2::at(int i, int j){
return m[2*i+j];
}
void Matrix2::set(int i, int j, double val){
m[2*i+j] = val;
}
void Matrix2::set(double m11, double m12, double m21, double m22){
m[0]=m11; m[1]=m12; m[2]=m21; m[3]=m22;
}
void Matrix2::print(){
std::cout<<m[0]<<"\t"<<m[1]<<std::endl;
std::cout<<m[2]<<"\t"<<m[3]<<std::endl;
}
Matrix2 Matrix2::operator +(Matrix2 mat){
double a1 = m[0]+mat.m[0];
double a2 = m[1]+mat.m[1];
double a3 = m[2]+mat.m[2];
double a4 = m[3]+mat.m[3];
Matrix2 temp(a1,a2,a3,a4);
return temp;
}
Matrix2 Matrix2::operator -(Matrix2 mat){
double a1 = m[0]-mat.m[0];
double a2 = m[1]-mat.m[1];
double a3 = m[2]-mat.m[2];
double a4 = m[3]-mat.m[3];
Matrix2 temp(a1,a2,a3,a4);
return temp;
}
Matrix2 Matrix2::operator *(Matrix2 mat){
double a1 = m[0]*mat.m[0]+m[1]*mat.m[2];
double a2 = m[0]*mat.m[1]+m[1]*mat.m[3];
double a3 = m[2]*mat.m[0]+m[3]*mat.m[2];
double a4 = m[2]*mat.m[1]+m[3]*mat.m[3];
Matrix2 temp(a1,a2,a3,a4);
return temp;
}
Matrix2 Matrix2::operator *(double s){
double a1 = m[0]*s;
double a2 = m[1]*s;
double a3 = m[2]*s;
double a4 = m[3]*s;
Matrix2 temp(a1,a2,a3,a4);
return temp;
}
RotMat2::RotMat2(){
angle=0;
double a1 = cos(angle);
double a2 = -sin(angle);
double a3 = sin(angle);
double a4 = cos(angle);
Matrix2::set(a1,a2,a3,a4);
}
RotMat2::RotMat2(double an){
angle=an;
double a1 = cos(angle);
double a2 = -sin(angle);
double a3 = sin(angle);
double a4 = cos(angle);
Matrix2::set(a1,a2,a3,a4);
}
void RotMat2::setAngle(double heading){
angle=heading;
double a1 = cos(angle);
double a2 = -sin(angle);
double a3 = sin(angle);
double a4 = cos(angle);
Matrix2::set(a1,a2,a3,a4);
}