准备环境
- 官方发布的作业0链接
- 百度盘链接,密码:p085
- 下载完的压缩包,在windows下最好使用7Zip进行解压
- Virtual Box官网
- 如何使用vdi文件
- 在创建虚拟机时建议将cpu核数调到4,防止后面运行程序时卡顿
使用C++
基本使用
// main.cpp
#include<cmath>
#include<iostream>
int main(){
// Basic Example of cpp
std::cout << "Example of cpp \n";
float a = 1.0, b = 2.0;
std::cout << a << std::endl;
std::cout << a/b << std::endl;
std::cout << std::sqrt(b) << std::endl;
std::cout << std::acos(-1) << std::endl;
std::cout << std::sin(30.0/180.0*acos(-1)) << std::endl;
return 0;
}
使用CMake编译
配置文件
// CMakeLists.txt
cmake_minimum_required (VERSION 2.8.11)
project (Transformation)
find_package (Eigen3 REQUIRED)
include_directories (EIGEN3_INCLUDE_DIR)
add_executable (Transformation main.cpp)
编译步骤
$ mkdir build && cd build # 在main.cpp所在目录创建build目录
$ cmake ..
$ make # 编译程序
$ ./Transformation # 运行程序
使用Eigen
- Eigen:一个线性代数运算库
基本使用
#include<cmath>
#include<iostream>
// 引入 eigen
#include<eigen3/Eigen/Core>
#include<eigen3/Eigen/Dense>
int main(){
// Example of vector
std::cout << "Example of vector \n";
// vector definition
Eigen::Vector3f v(1.0f,2.0f,3.0f);
Eigen::Vector3f w(1.0f,0.0f,0.0f);
// vector output
std::cout << "Example of output \n";
std::cout << v << std::endl;
// vector add
std::cout << "Example of add \n";
std::cout << v + w << std::endl;
// vector scalar multiply
std::cout << "Example of scalar multiply \n";
std::cout << v * 3.0f << std::endl;
std::cout << 2.0f * v << std::endl;
// Example of matrix
std::cout << "Example of matrix \n";
// matrix definition
Eigen::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;
// matrix output
std::cout << "Example of output \n";
std::cout << i << std::endl;
// matrix add i + j
std::cout << i + j << std::endl;
// matrix scalar multiply i * 2.0
std::cout << i * 2.0 << std::endl;
// matrix multiply i * j
std::cout << i * j << std::endl;
// matrix multiply vector i * v
std::cout << i * v << std::endl;
return 0;
}
完成作业
描述
给定一个点 P = (2,1)
,将该点绕原点先逆时针旋转45°,再平移(1,2)
,计算出变换后的坐标(要求使用齐次坐标进行运算)。
实现
#include<cmath>
#include<eigen3/Eigen/Core>
#include<eigen3/Eigen/Dense>
#include<iostream>
int main(){
// Point definition
Eigen::Vector3f p(2.0,1.0,1.0);
// Matrix definition
Eigen::Matrix3f RotateMatrix,TranslateMatrix;
TranslateMatrix << 1.0, 0.0, 1.0,
0.0, 1.0, 2.0,
0.0, 0.0, 1.0;
float cos45 = std::cos(45),
sin45 = std::sin(45);
RotateMatrix << cos45, -1.0*sin45, 0.0,
sin45, cos45, 0.0,
0.0, 0.0, 1.0;
std::cout << (TranslateMatrix * RotateMatrix * p) / 1.0 << std::endl;
return 0;
}