Update rive-cpp to 2.0 version
[platform/core/uifw/rive-tizen.git] / submodule / rive-cpp / include / rive / math / vec2d.hpp
1 #ifndef _RIVE_VEC2D_HPP_
2 #define _RIVE_VEC2D_HPP_
3
4 #include "rive/rive_types.hpp"
5
6 namespace rive {
7     class Mat2D;
8     class Vec2D {
9     public:
10         float x, y;
11
12         constexpr Vec2D() : x(0), y(0) {}
13         constexpr Vec2D(float x, float y) : x(x), y(y) {}
14         constexpr Vec2D(const Vec2D&) = default;
15
16         float lengthSquared() const { return x * x + y * y; }
17         float length() const;
18         Vec2D normalized() const;
19
20         // Normalize this Vec, and return its previous length
21         float normalizeLength() {
22             const float len = this->length();
23             x /= len;
24             y /= len;
25             return len;
26         }
27
28         Vec2D operator-() const { return {-x, -y}; }
29
30         void operator*=(float s) {
31             x *= s;
32             y *= s;
33         }
34
35         void operator/=(float s) {
36             x /= s;
37             y /= s;
38         }
39
40         friend inline Vec2D operator-(const Vec2D& a, const Vec2D& b) {
41             return {a.x - b.x, a.y - b.y};
42         }
43
44         static inline Vec2D lerp(Vec2D a, Vec2D b, float f);
45
46         static Vec2D transformDir(const Vec2D& a, const Mat2D& m);
47
48         static float dot(Vec2D a, Vec2D b) { return a.x * b.x + a.y * b.y; }
49         static Vec2D scaleAndAdd(Vec2D a, Vec2D b, float scale) {
50             return {
51                 a.x + b.x * scale,
52                 a.y + b.y * scale,
53             };
54         }
55         static float distance(const Vec2D& a, const Vec2D& b) { return (a - b).length(); }
56         static float distanceSquared(const Vec2D& a, const Vec2D& b) {
57             return (a - b).lengthSquared();
58         }
59
60         Vec2D& operator+=(Vec2D v) {
61             x += v.x;
62             y += v.y;
63             return *this;
64         }
65         Vec2D& operator-=(Vec2D v) {
66             x -= v.x;
67             y -= v.y;
68             return *this;
69         }
70     };
71
72     inline Vec2D operator*(const Vec2D& v, float s) { return {v.x * s, v.y * s}; }
73     inline Vec2D operator*(float s, const Vec2D& v) { return {v.x * s, v.y * s}; }
74     inline Vec2D operator/(const Vec2D& v, float s) { return {v.x / s, v.y / s}; }
75
76     inline Vec2D operator+(const Vec2D& a, const Vec2D& b) { return {a.x + b.x, a.y + b.y}; }
77
78     inline bool operator==(const Vec2D& a, const Vec2D& b) { return a.x == b.x && a.y == b.y; }
79     inline bool operator!=(const Vec2D& a, const Vec2D& b) { return a.x != b.x || a.y != b.y; }
80
81     Vec2D Vec2D::lerp(Vec2D a, Vec2D b, float t) {
82         return a + (b - a) * t;
83     }
84
85 } // namespace rive
86 #endif