GAMES101-H0
GAMES101-H0
Basic Example of cpp
解释如下代码:
#include<cmath>
#include<iostream>
int main() {
float a = 1.0, b = 2.0;
std::cout << a << std::endl; //1
std::cout << a / b << std::endl;//2
std::cout << std::sqrt(b) << std::endl;//3
std::cout << std::acos(-1) << std::endl;//4
std::cout << std::sin(30.0 / 180.0 * acos(-1)) << std::endl;//5
return 0;
}
1——输出a的值,即1;
2——输出a/b的值,即0.5;
3——输出b的开方,即1.41421;
4——acos(-1)就是180°,即转化为弧度制就是π,输出3.14159;
5——输出sin(30/180acos(-1))=sin(1/6π)=sin30°=0.5;
Example of vector
解释如下代码:
#include<Eigen/Core>
#include<iostream>
using namespace std;
using namespace Eigen;
int main(void) {
// Example of vector
cout << "Example of vector \n";
// vector definition
Vector3f v(1.0f,2.0f,3.0f);
Vector3f w(1.0f,0.0f,0.0f);
// vector output 1
cout << "Example of output \n";
cout << v << endl;
cout << w << endl;
// vector add 2
cout << "Example of add \n";
cout << v + w << endl;
// vector scalar multiply 3
cout << "Example of scalar multiply \n";
cout << v * 3.0f << endl;
cout << 2.0f * v << endl;
//vector product 4
cout << "Example of product \n"<<endl;
cout << v.dot(w) << endl; //点乘
cout << v.cross(w) << endl; //叉乘
return 0;
}
1——输出\(\vec{v}= \left(\matrix{1\\ 2\\3}\right)\)和$\vec{w}=\left(\matrix {1\\ 0\ 0}\right ) $;
2——输出\(\vec{v}+\vec{w}= \left(\matrix{2\\ 2\\3}\right)\);
3——输出\(\vec{v}*3= \left(\matrix{3\\ 6\\9}\right)\)和\(2*\vec{v}= \left(\matrix{3\\ 6\\9}\right)\);
4——输出\(\vec{v}\cdot\vec{w}= 1\)和\(\vec{v}\times\vec{w}= \left(\matrix{1\\ 2\\3}\right)\times\left(\matrix{1\\ 0\\0}\right)=\left(\matrix{1*0-0*2\\ 3*1-1*0\\1*0-2*1}\right)=\left(\matrix{0\\ 3\\-2}\right)\)


Example of matrix
解释如下代码:
#include<Eigen/Core>
#include<iostream>
using namespace std;
using namespace Eigen;
int main(void) {
// Example of matrix
cout << "Example of matrix \n";
// matrix definition
Matrix3f i, j;
i << 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0;
j << 2.0, 3.0, 1.0, 4.0, 6.0, 5.0, 9.0, 7.0, 8.0;
Vector3f v(1.0f, 2.0f, 3.0f);
Vector3f w(1.0f, 0.0f, 0.0f);
// matrix output 1
cout << "Example of output \n";
cout << i << endl;
cout << j << endl;
// matrix add 2
cout << "Example of add \n";
cout << i + j << endl;
// Example of dimin 3
cout << "Example of dimin\n";
cout << i -j << endl;
// Example of multiply 4
cout << "Example of multiply \n";
cout << i * j << endl;
Example of multiply*vector 5
cout << "Example of multiply*vector \n";
cout << i*v << endl;
return 0;
}
1——输出\(i=\left[ \matrix{1&2&3\\4&5&6\\ 7&8&9}\right]\)和\(j=\left[ \matrix{2&3&1\\4&6&5\\ 9&7&8}\right]\);
2——输出\(i+j=\left[ \matrix{3&5&4\\8&11&11\\ 16&15&17}\right]\);
3——输出\(i-j=\left[ \matrix{-1&-1&-2\\0&-1&1\\ -2&1&1}\right]\);
4——输出\(i*j=\left[ \matrix{37&36&35\\82&84&77\\ 127&132&119}\right]\);
5——输出\(i*v=\left[ \matrix{14\\32\\50}\right]\);
作业
给定一个点P=(2,1), 将该点绕原点先逆时针旋转45◦,再平移(1,2), 计算出变换后点的坐标(要求用齐次坐标进行计算)。
#include<cmath>
#include<eigen3/Eigen/Core>
#include<eigen3/Eigen/Dense>
#include<iostream>
using namespace std;
usnig namespace Eigen;
int main(){
Eigen::Vector3f p(2,1,1);//用齐次坐标表示点为(x,y,1)T
Eigen::Matrix3f Rotation_p,Translation_p;
Rotation_p<<cos(45.0/180.0*acos(-1)),-sin(45.0/180.0*acos(-1)),0,sin(45.0/180.0*acos(-1)),cos(45.0/180.0*acos(-1)),0,0,0,1;
Translation_p<<1,0,1,0,1,2,0,0,1;
cout<<Translation_p*Rotation_p*p;
return 0;
}

则点P=(2,1), 绕原点先逆时针旋转45◦,再平移(1,2)后,点的坐标为\(\left( \matrix{1.70711 \\4.12132}\right)\)(要求用齐次坐标进行计算)。

浙公网安备 33010602011771号