Path: blob/master/thirdparty/msdfgen/core/Vector2.hpp
9903 views
1#pragma once23#include <cmath>4#include "base.h"56namespace msdfgen {78/**9* A 2-dimensional euclidean floating-point vector.10* @author Viktor Chlumsky11*/12struct Vector2 {1314double x, y;1516inline Vector2(double val = 0) : x(val), y(val) { }1718inline Vector2(double x, double y) : x(x), y(y) { }1920/// Sets the vector to zero.21inline void reset() {22x = 0, y = 0;23}2425/// Sets individual elements of the vector.26inline void set(double newX, double newY) {27x = newX, y = newY;28}2930/// Returns the vector's squared length.31inline double squaredLength() const {32return x*x+y*y;33}3435/// Returns the vector's length.36inline double length() const {37return sqrt(x*x+y*y);38}3940/// Returns the normalized vector - one that has the same direction but unit length.41inline Vector2 normalize(bool allowZero = false) const {42if (double len = length())43return Vector2(x/len, y/len);44return Vector2(0, !allowZero);45}4647/// Returns a vector with the same length that is orthogonal to this one.48inline Vector2 getOrthogonal(bool polarity = true) const {49return polarity ? Vector2(-y, x) : Vector2(y, -x);50}5152/// Returns a vector with unit length that is orthogonal to this one.53inline Vector2 getOrthonormal(bool polarity = true, bool allowZero = false) const {54if (double len = length())55return polarity ? Vector2(-y/len, x/len) : Vector2(y/len, -x/len);56return polarity ? Vector2(0, !allowZero) : Vector2(0, -!allowZero);57}5859#ifdef MSDFGEN_USE_CPP1160inline explicit operator bool() const {61return x || y;62}63#else64inline operator const void *() const {65return x || y ? this : NULL;66}67#endif6869inline Vector2 &operator+=(const Vector2 other) {70x += other.x, y += other.y;71return *this;72}7374inline Vector2 &operator-=(const Vector2 other) {75x -= other.x, y -= other.y;76return *this;77}7879inline Vector2 &operator*=(const Vector2 other) {80x *= other.x, y *= other.y;81return *this;82}8384inline Vector2 &operator/=(const Vector2 other) {85x /= other.x, y /= other.y;86return *this;87}8889inline Vector2 &operator*=(double value) {90x *= value, y *= value;91return *this;92}9394inline Vector2 &operator/=(double value) {95x /= value, y /= value;96return *this;97}9899};100101/// A vector may also represent a point, which shall be differentiated semantically using the alias Point2.102typedef Vector2 Point2;103104/// Dot product of two vectors.105inline double dotProduct(const Vector2 a, const Vector2 b) {106return a.x*b.x+a.y*b.y;107}108109/// A special version of the cross product for 2D vectors (returns scalar value).110inline double crossProduct(const Vector2 a, const Vector2 b) {111return a.x*b.y-a.y*b.x;112}113114inline bool operator==(const Vector2 a, const Vector2 b) {115return a.x == b.x && a.y == b.y;116}117118inline bool operator!=(const Vector2 a, const Vector2 b) {119return a.x != b.x || a.y != b.y;120}121122inline Vector2 operator+(const Vector2 v) {123return v;124}125126inline Vector2 operator-(const Vector2 v) {127return Vector2(-v.x, -v.y);128}129130inline bool operator!(const Vector2 v) {131return !v.x && !v.y;132}133134inline Vector2 operator+(const Vector2 a, const Vector2 b) {135return Vector2(a.x+b.x, a.y+b.y);136}137138inline Vector2 operator-(const Vector2 a, const Vector2 b) {139return Vector2(a.x-b.x, a.y-b.y);140}141142inline Vector2 operator*(const Vector2 a, const Vector2 b) {143return Vector2(a.x*b.x, a.y*b.y);144}145146inline Vector2 operator/(const Vector2 a, const Vector2 b) {147return Vector2(a.x/b.x, a.y/b.y);148}149150inline Vector2 operator*(double a, const Vector2 b) {151return Vector2(a*b.x, a*b.y);152}153154inline Vector2 operator/(double a, const Vector2 b) {155return Vector2(a/b.x, a/b.y);156}157158inline Vector2 operator*(const Vector2 a, double b) {159return Vector2(a.x*b, a.y*b);160}161162inline Vector2 operator/(const Vector2 a, double b) {163return Vector2(a.x/b, a.y/b);164}165166}167168169