Homework

Games 101 Homework

Homework 0

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
#include<cmath>
#include<eigen3/Eigen/Core>
#include<eigen3/Eigen/Dense>
#include<iostream>

#define M_PI 3.14159265358979323846

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;

// 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
Eigen::Matrix3f sum1 = i + j;
std::cout << "Matrix sum" << std::endl;
std::cout << sum1 << std::endl;
std::cout << "Std sum" << std::endl;
std::cout << i + j << std::endl;
// matrix scalar multiply i * 2.0
Eigen::Matrix3f scalar_mult = i * 2.0;
std::cout << "Matrix i * 2.0:\n" << scalar_mult << std::endl;
// matrix multiply i * j
Eigen::Matrix3f matrix_mult = i * j;
std::cout << "Matrix i * j:\n" << matrix_mult << std::endl;
// matrix multiply vector i * v
Eigen::Vector3f matrix_vector_mult = i * v;
std::cout << "Matrix i * Vector v:\n" << matrix_vector_mult << std::endl;

// 旋转
Eigen::Vector3f P(2.0, 1.0, 1.0);
double theta = M_PI / 4.0;
Eigen::Matrix3f rotation;
rotation << cos(theta), -sin(theta), 0,
sin(theta), cos(theta), 0,
0, 0, 1;
Eigen::Matrix3f translation;
translation << 1, 0, 1,
0, 1, 2,
0, 0, 1;

// 先旋转再平移
Eigen::Vector3f P_transformed = translation * rotation * P;
std::cout << P_transformed << std::endl;

return 0;
}

std::acos(-1)

在 C++ 标准库中,std::acos 函数用于计算反余弦(arccosine)。反余弦函数的定义是:对于一个给定的值 y(-1 ≤ y ≤ 1),找到一个角度 θ,使得 cos(θ) = y。函数的输出范围是 [0, π]。

https://github.com/EdmendZ/games101-Assignment0

1,2,3

4,5,6

7,8,9

v

1

2

3

2,3,1

4,6,5

9,7,8

2+8+27

Homework 2