179 lines
3.6 KiB
C++
179 lines
3.6 KiB
C++
#pragma once
|
|
|
|
#include "raylib.h"
|
|
|
|
#include <cmath>
|
|
|
|
#include <iostream>
|
|
|
|
#include "nlohmann/json.hpp"
|
|
|
|
using json = nlohmann::json;
|
|
|
|
namespace VISEQ::VMATH{
|
|
|
|
template<typename T>
|
|
class Vec2D{
|
|
public:
|
|
|
|
Vec2D() {};
|
|
Vec2D(T x, T y) : x(x), y(y) {}
|
|
T x = 0;
|
|
T y = 0;
|
|
T dist(Vec2D* v) { return sqrt(pow((v->x - x), 2) + pow((v->y - y), 2));};
|
|
T dist(Vec2D v) { return sqrt(pow((v.x - x), 2) + pow((v.y - y), 2));};
|
|
|
|
T length() { return (T)sqrt(pow(x, 2) + pow(y, 2));};
|
|
|
|
T angle(Vec2D v){
|
|
T angle = atan2(v.y, v.x) - atan2(y,x);
|
|
if(angle > M_PI) angle -= M_PI_2;
|
|
if(angle < -M_PI) angle += M_PI_2;
|
|
return angle;
|
|
//return acos(dot(v));
|
|
};
|
|
|
|
T angleDeg(Vec2D v){
|
|
return angle(v) * 180 / M_PI;
|
|
};
|
|
|
|
T dot(Vec2D &v){
|
|
return (x*v.x) + (y*v.y);
|
|
};
|
|
|
|
T dot(Vec2D v){
|
|
return (x*v.x) + (y*v.y);
|
|
};
|
|
|
|
operator Vector2() const {
|
|
return (Vector2){x,y};
|
|
}
|
|
|
|
Vec2D orth(Vec2D &v){
|
|
return Vec2D(-v.y, v.x);
|
|
};
|
|
|
|
Vec2D orth(){
|
|
return Vec2D(-y, x);
|
|
};
|
|
|
|
Vec2D rotate(float deg){
|
|
return Vec2D(cos(deg*DEG2RAD) * x, sin(deg*DEG2RAD) * y);
|
|
};
|
|
|
|
Vec2D norm() { return *this / length();};
|
|
|
|
Vec2D center(Vec2D v){
|
|
return Vec2D<T>{x,y} - (Vec2D<T>{x,y} - v)*0.5f;
|
|
}
|
|
|
|
Vec2D orbitAlongVector(Vec2D d, float r){
|
|
return Vec2D<T>{x,y} - ((Vec2D<T>{x,y} - d)).norm() * r;
|
|
}
|
|
|
|
Vec2D orbitAtAngle(float r, float angle){
|
|
return {x + sin(angle) * r, y + cos(angle) * r};
|
|
}
|
|
|
|
Vec2D& operator=(const Vec2D& vec){
|
|
if (this == &vec){
|
|
return *this;
|
|
}
|
|
x = vec.x;
|
|
y = vec.y;
|
|
return *this;
|
|
};
|
|
|
|
Vec2D& operator+=(const Vec2D& vec){
|
|
this->x += vec.x;
|
|
this->y += vec.y;
|
|
return *this;
|
|
};
|
|
|
|
Vec2D& operator-=(const Vec2D& vec){
|
|
this->x -= vec.x;
|
|
this->y -= vec.y;
|
|
return *this;
|
|
};
|
|
|
|
Vec2D& operator*=(const Vec2D& vec){
|
|
this->x *= vec.x;
|
|
this->y *= vec.y;
|
|
return *this;
|
|
};
|
|
|
|
Vec2D& operator/=(const Vec2D& vec){
|
|
this->x /= vec.x;
|
|
this->y /= vec.y;
|
|
return *this;
|
|
};
|
|
|
|
Vec2D& operator*=(T scalar){
|
|
this->x *= scalar;
|
|
this->y *= scalar;
|
|
return *this;
|
|
};
|
|
|
|
Vec2D& operator/=(T scalar){
|
|
this->x /= scalar;
|
|
this->y /= scalar;
|
|
return *this;
|
|
};
|
|
};
|
|
|
|
template<typename T>
|
|
void to_json(json& j, const Vec2D<T>& V){
|
|
j = json{{"x",V.x},{"y",V.y}};
|
|
}
|
|
|
|
template<typename T>
|
|
void from_json(const json& j, Vec2D<T>& V){
|
|
std::cout << "Vec2D" << std::endl;
|
|
j.at("x").get_to(V.x);
|
|
j.at("y").get_to(V.y);
|
|
}
|
|
|
|
template<typename T>
|
|
Vec2D<T> operator+(const Vec2D<T> left, const Vec2D<T> right){
|
|
return Vec2D<T>(left.x + right.x, left.y + right.y);
|
|
};
|
|
|
|
template<typename T>
|
|
Vec2D<T> operator-(const Vec2D<T> left, const Vec2D<T> right){
|
|
return Vec2D<T>(left.x - right.x, left.y - right.y);
|
|
};
|
|
|
|
template<typename T>
|
|
Vec2D<T> operator*(const Vec2D<T> left, const Vec2D<T> right){
|
|
return Vec2D<T>(left.x * right.x, left.y * right.y);
|
|
};
|
|
|
|
template<typename T>
|
|
Vec2D<T> operator/(const Vec2D<T> left, const Vec2D<T> right){
|
|
return Vec2D<T>(left.x / right.x, left.y / right.y);
|
|
};
|
|
|
|
template<typename T>
|
|
Vec2D<T> operator/(const Vec2D<T> left, T scalar){
|
|
return Vec2D<T>(left.x / scalar, left.y / scalar);
|
|
};
|
|
|
|
template<typename T>
|
|
Vec2D<T> operator*(const Vec2D<T> left, T scalar){
|
|
return Vec2D<T>(left.x * scalar, left.y * scalar);
|
|
};
|
|
|
|
template<typename T>
|
|
Vec2D<T> cartesianToPolar(T x, T y) {
|
|
return Vec2D<T>{sqrt(x * x + y * y), atan(y / x)}; // r,theta
|
|
}
|
|
|
|
template<typename T>
|
|
Vec2D<T> polarToCartesian(T r, T q) {
|
|
return Vec2D<T>{r * (T)cos(q), r * (T)sin(q)};
|
|
}
|
|
}
|
|
|
|
using Vec2Df = VISEQ::VMATH::Vec2D<float>;
|
|
|