-
Notifications
You must be signed in to change notification settings - Fork 0
/
geometry.h
69 lines (53 loc) · 2.19 KB
/
geometry.h
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
#ifndef __GEOMETRY_H__
#define __GEOMETRY_H__
#include <iostream>
#include <cmath>
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
template <class t> struct Vec2 {
t x, y;
Vec2<t>() {}
Vec2<t>(const t& _x, const t& _y) : x(_x), y(_y) {}
inline Vec2<t> operator +(const Vec2<t>& v) const { return Vec2<t>(x + v.x, y + v.y); }
inline Vec2<t> operator -(const Vec2<t>& v) const { return Vec2<t>(x - v.x, y - v.y); }
inline Vec2<t> operator *(float f) const { return Vec2<t>(x * f, y * f); }
template <class > friend std::ostream& operator<<(std::ostream& s, const Vec2<t>& v);
};
template <class t> struct Vec3 {
t x, y, z;
Vec3<t>() {}
Vec3<t>(const t& _x, const t& _y, const t& _z) : x(_x), y(_y), z(_z) {}
template<class t1>
Vec3<t>(const Vec3<t1>& v) : x(v.x), y(v.y), z(v.z) {}
inline Vec3<t> operator +(const Vec3<t>& v) const { return Vec3<t>(x + v.x, y + v.y, z + v.z); }
inline Vec3<t> operator -(const Vec3<t>& v) const { return Vec3<t>(x - v.x, y - v.y, z - v.z); }
inline Vec3<t> operator *(float f) const { return Vec3<t>(x * f, y * f, z * f); }
inline Vec3<t> operator ^(const Vec3<t>& v) const {
return Vec3<t>(
y * v.z - z * v.y,
z * v.x - x * v.z,
x * v.y - y * v.x
);
}
inline t operator* (const Vec3<t>& v) const { return x * v.x + y * v.y + z * v.z; }
float norm() const {
return std::sqrt((*this) * (*this));
}
Vec3<t>& normalize(t l = 1) {
*this = (*this) * (l / norm());
return *this;
}
template <class > friend std::ostream& operator<<(std::ostream& s, const Vec3<t>& v);
};
typedef Vec2<float> Vec2f;
typedef Vec2<int> Vec2i;
typedef Vec3<float> Vec3f;
typedef Vec3<int> Vec3i;
template <class t> std::ostream& operator<<(std::ostream& s, const Vec2<t>& v) {
s << "(" << v.x << ", " << v.y << ")\n";
return s;
}
template <class t> std::ostream& operator<<(std::ostream& s, const Vec3<t>& v) {
s << "(" << v.x << ", " << v.y << ", " << v.z << ")\n";
return s;
}
#endif //__GEOMETRY_H__