-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-3.cpp
More file actions
61 lines (51 loc) · 1.11 KB
/
test-3.cpp
File metadata and controls
61 lines (51 loc) · 1.11 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
#include "Vector2.h"
#include "Tuple2.h"
#include "Point2.h"
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
#ifdef VM_INCLUDE_NAMESPACE
using namespace kh_vecmath;
#endif
template<class T, class S>
bool equals(T a, S b) {
return fabs(a - b) < 1.0e-8;
}
/**
* test-3 for Vector2
*/
#ifdef TESTALL
int test_3() {
#else
int main(int, char**) {
#endif
Vector2d p1(1,2);
Vector2d p2(4,6);
// dot product
Vector2d::value_type d = p1.dot(p2);
assert(equals(d, 16));
// length
d = p1.length();
assert(equals(d, sqrt(p1.dot(p1))));
// normalize
p1.normalize();
assert(equals(p1.x,1/d));
assert(equals(p1.y,2/d));
p1.set(-1,3);
d = p1.length();
p2.normalize(p1);
assert(equals(p2.x,-1/d));
assert(equals(p2.y, 3/d));
// angle
p1.set(3, 4);
p2.set(-4, 3);
d = p1.angle(p2);
assert(equals(d, M_PI/2));
d = p2.angle(p1);
assert(equals(d, M_PI/2));
d = p1.angle(p1);
assert(equals(d, 0));
d = p2.angle(p2);
assert(equals(d, 0));
return 0;
}