载入中...
搜索中...
未找到
Vec2.cpp
浏览该文件的文档.
1#include "math/Vec2.h"
2
3#include "common/Exception.h"
4
5#include <cmath>
6
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
int v
uint32_t s
Definition Weather.cpp:28
2D float vector (script-facing math module value).
Definition Vec2.h:8
Vec2 * sub(const Vec2 *other) const
Definition Vec2.cpp:47
Vec2 * normalized() const
Definition Vec2.cpp:17
float cross(const Vec2 *other) const
Definition Vec2.cpp:28
float dot(const Vec2 *other) const
Dot/cross product, distance, angle (radians).
Definition Vec2.cpp:23
Vec2 * scale(float s) const
Definition Vec2.cpp:52
float angle() const
Definition Vec2.cpp:40
float distanceTo(const Vec2 *other) const
Definition Vec2.cpp:33
float length() const
Magnitude (and squared magnitude).
Definition Vec2.h:26
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