-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_vector2.cpp
More file actions
115 lines (95 loc) · 3.34 KB
/
Copy pathtest_vector2.cpp
File metadata and controls
115 lines (95 loc) · 3.34 KB
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
#include <iostream>
#include "vector2.hpp"
void test_length()
{
Vector2 test_vec(3.0f, 4.0f);
std::cout << "test length\n"
<< "x: " << test_vec.x << '\n'
<< "y: " << test_vec.y << '\n'
<< "length: " << test_vec.length() << '\n'
<< "length squared: " << test_vec.length_squared() << '\n';
}
void test_normalize()
{
float x = 3.0f, y = 4.0f;
Vector2 my_vec(x, y);
Vector2 my_vec_norm = my_vec.normalize();
std::cout << "normalized\n"
<< "x: " << my_vec_norm.x << ' '
<< "y: " << my_vec_norm.y << '\n';
}
void test_dot_product()
{
Vector2 v1(1.0f, 3.0f);
Vector2 v2(4.0f, -2.0f);
float dot_result = v1.dot(v2);
float dist = v1.distance(v2);
float dist_sqr = v1.distance_squared(v2);
float len = v1.length();
float len_sqr = v1.length_squared();
std::cout << "dot product: " << dot_result << '\n';
std::cout << "distance : " << dist << '\n';
std::cout << "distance squared : " << dist_sqr << '\n';
std::cout << "length : " << len << '\n';
std::cout << "length squared : " << len_sqr << '\n';
}
void test_operator_overloads()
{
Vector2 v3(10.0f, 100.0f);
Vector2 v4(5.0f, 50.0f);
Vector2 div_eql = v4 /= v3;
std::cout << "divide equal /=\n"
<< "x: " << div_eql.x << '\n'
<< "y: " << div_eql.y << '\n';
Vector2 v5(10.0f, 20.0f);
Vector2 v6(10.0f, 20.0f);
float div_by = 2.0f;
Vector2 div_vec = v5/v6;
std::cout << "divide /\n"
<< "x: " << div_vec.x << '\n'
<< "y: " << div_vec.y << '\n';
std::cout << "divide by scalar /\n"
<< "x: " << div_vec.x/div_by << '\n'
<< "y: " << div_vec.y/div_by << '\n';
std::cout << "is v5 != v6\n"
<< (v5 != v6 ? "they are not equal" : "they are equal") << '\n';
std::cout << "is v3 != v4\n"
<< (v3 != v4 ? "they are not equal" : "they are equal") << '\n';
v5 = -v3;
std::cout << "unary minus \n"
<< "v5.x: " << v5.x << '\n'
<< "v5.y: " << v5.y << '\n';
}
void test_rotate()
{
Vector2 v1(10.0f, 20.0f);
Vector2 v2(5.0f, 10.0f);
float angle = 10; // 10 degrees
Vector2 v1_rot = v1.rotate(angle);
Vector2 v2_rot = v2.rotate(angle);
std::cout << "Initial Vector values\n"
<< "v1 x: " << v1.x << '\n'
<< "v1 y: " << v1.y << '\n'
<< "v2 x: " << v2.x << '\n'
<< "v2 y: " << v2.y << '\n';
std::cout << "Rotate by angle: " << angle << '\n';
std::cout << "Rotated vector values\n"
<< "v1_rot x: " << v1_rot.x << '\n'
<< "v1_rot y: " << v1_rot.y << '\n'
<< "v2_rot x: " << v2_rot.x << '\n'
<< "v2_rot y: " << v2_rot.y << '\n';
}
int main()
{
// std::cout << "=================== test_length() ===================\n";
// test_length();
// std::cout << "=================== test_normalize() ===================\n";
// test_normalize();
// std::cout << "=================== test_dot_product() ===================\n";
// test_dot_product();
// std::cout << "=================== test_operator_overloads() ===================\n";
// test_operator_overloads();
// std::cout << "=================== end ===================\n";
test_rotate();
return 0;
}