载入中...
搜索中...
未找到
Vec2.cpp
浏览该文件的文档.
1#include "math/Vec2.h"
2#include <cmath>
3#include "common/Exception.h"
4#include "math/Vec3.h"
5
6// Vec2.cpp
7namespace eve::math {
8
10 float len = length();
11 if (len > 0.f) {
12 x_ /= len;
13 y_ /= len;
14 }
15}
16
18 auto *v = new Vec2(x_, y_);
19 v->normalize();
20 return v;
21}
22
23float Vec2::dot(const Vec2 *other) const {
24 if (!other) throw eve::Exception("Vec2.dot: other is null");
25 return x_ * other->x_ + y_ * other->y_;
26}
27
28float Vec2::cross(const Vec2 *other) const {
29 if (!other) throw eve::Exception("Vec2.cross: other is null");
30 return x_ * other->y_ - y_ * other->x_;
31}
32
33float Vec2::distanceTo(const Vec2 *other) const {
34 if (!other) throw eve::Exception("Vec2.distanceTo: other is null");
35 float dx = x_ - other->x_;
36 float dy = y_ - other->y_;
37 return std::sqrt(dx * dx + dy * dy);
38}
39
40float Vec2::angle() const { return std::atan2(y_, x_); }
41
42Vec2 *Vec2::add(const Vec2 *other) const {
43 if (!other) throw eve::Exception("Vec2.add: other is null");
44 return new Vec2(x_ + other->x_, y_ + other->y_);
45}
46
47Vec2 *Vec2::sub(const Vec2 *other) const {
48 if (!other) throw eve::Exception("Vec2.sub: other is null");
49 return new Vec2(x_ - other->x_, y_ - other->y_);
50}
51
52Vec2 *Vec2::scale(float s) const { return new Vec2(x_ * s, y_ * s); }
53
54Vec2 *Vec2::lerpTo(const Vec2 *other, float t) const {
55 if (!other) throw eve::Exception("Vec2.lerpTo: other is null");
56 return new Vec2(x_ + (other->x_ - x_) * t, y_ + (other->y_ - y_) * t);
57}
58
59Vec2 *Vec2::clone() const { return new Vec2(x_, y_); }
60
61} // namespace eve::math
62
63// Vec3.cpp
64namespace eve::math {
65
67 float len = length();
68 if (len > 0.f) {
69 x_ /= len;
70 y_ /= len;
71 z_ /= len;
72 }
73}
74
76 auto *v = new Vec3(x_, y_, z_);
77 v->normalize();
78 return v;
79}
80
81float Vec3::dot(const Vec3 *other) const {
82 if (!other) throw eve::Exception("Vec3.dot: other is null");
83 return x_ * other->x_ + y_ * other->y_ + z_ * other->z_;
84}
85
86Vec3 *Vec3::cross(const Vec3 *other) const {
87 if (!other) throw eve::Exception("Vec3.cross: other is null");
88 return new Vec3(y_ * other->z_ - z_ * other->y_, z_ * other->x_ - x_ * other->z_, x_ * other->y_ - y_ * other->x_);
89}
90
91float Vec3::distanceTo(const Vec3 *other) const {
92 if (!other) throw eve::Exception("Vec3.distanceTo: other is null");
93 float dx = x_ - other->x_;
94 float dy = y_ - other->y_;
95 float dz = z_ - other->z_;
96 return std::sqrt(dx * dx + dy * dy + dz * dz);
97}
98
99Vec3 *Vec3::add(const Vec3 *other) const {
100 if (!other) throw eve::Exception("Vec3.add: other is null");
101 return new Vec3(x_ + other->x_, y_ + other->y_, z_ + other->z_);
102}
103
104Vec3 *Vec3::sub(const Vec3 *other) const {
105 if (!other) throw eve::Exception("Vec3.sub: other is null");
106 return new Vec3(x_ - other->x_, y_ - other->y_, z_ - other->z_);
107}
108
109Vec3 *Vec3::scale(float s) const { return new Vec3(x_ * s, y_ * s, z_ * s); }
110
111Vec3 *Vec3::lerpTo(const Vec3 *other, float t) const {
112 if (!other) throw eve::Exception("Vec3.lerpTo: other is null");
113 return new Vec3(x_ + (other->x_ - x_) * t, y_ + (other->y_ - y_) * t, z_ + (other->z_ - z_) * t);
114}
115
116Vec3 *Vec3::clone() const { return new Vec3(x_, y_, z_); }
117
118} // namespace eve::math
const std::string & s
float v
glm::dvec3 dx
glm::dvec3 dz
float t
float dy
Definition Shadow.cpp:151
EVENGINE_API_FOUNDATION public API.
Definition Exception.h:13
2D float vector (script-facing math module value).
Definition Vec2.h:10
Vec2 * sub(const Vec2 *other) const
Definition Vec2.cpp:47
Vec2 * normalized() const
Definition Vec2.cpp:17
float cross(const Vec2 *other) const
Cross product with other.
Definition Vec2.cpp:28
float dot(const Vec2 *other) const
Atan2 angle in radians.
Definition Vec2.cpp:23
Vec2 * scale(float s) const
Definition Vec2.cpp:52
float angle() const
Atan2 angle in radians.
Definition Vec2.cpp:40
float distanceTo(const Vec2 *other) const
Euclidean distance to other.
Definition Vec2.cpp:33
float length() const
Magnitude (and squared magnitude).
Definition Vec2.h:31
Vec2()=default
Vec2 * clone() const
Copies this vector.
Definition Vec2.cpp:59
Vec2 * add(const Vec2 *other) const
Arithmetic helpers returning new (caller-owned) vectors.
Definition Vec2.cpp:42
void normalize()
Normalizes in place / returns a normalized copy.
Definition Vec2.cpp:9
Vec2 * lerpTo(const Vec2 *other, float t) const
Linear interpolation to other at t in [0,1].
Definition Vec2.cpp:54
3D float vector (script-facing math module value).
Definition Vec3.h:10
Vec3()=default
Vec3 * normalized() const
Definition Vec2.cpp:75
Vec3 * cross(const Vec3 *other) const
Definition Vec2.cpp:86
Vec3 * clone() const
Copies this vector.
Definition Vec2.cpp:116
Vec3 * sub(const Vec3 *other) const
Definition Vec2.cpp:104
Vec3 * lerpTo(const Vec3 *other, float t) const
Linear interpolation to other at t in [0,1].
Definition Vec2.cpp:111
float length() const
Magnitude (and squared magnitude).
Definition Vec3.h:36
Vec3 * scale(float s) const
Definition Vec2.cpp:109
Vec3 * add(const Vec3 *other) const
Arithmetic helpers returning new (caller-owned) vectors.
Definition Vec2.cpp:99
float dot(const Vec3 *other) const
Dot/cross product and distance.
Definition Vec2.cpp:81
float distanceTo(const Vec3 *other) const
Euclidean distance to other.
Definition Vec2.cpp:91
void normalize()
Normalizes in place / returns a normalized copy.
Definition Vec2.cpp:66