| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183 |
- #pragma once
- #include <cmath>
- #include "Helpers.h"
- struct Vector {
- float x;
- float y;
- Vector &operator=(const Vector &other) {
- if (this != &other) {
- x = other.x;
- y = other.y;
- }
- return *this;
- }
- Vector(const Vector &other) : x(other.x), y(other.y) {}
- Vector(float _x, float _y) : x(_x), y(_y) {}
- Vector() : x(0), y(0) {}
- Vector operator+(Vector const &other) {
- return Vector({x + other.x, y + other.y});
- }
- Vector operator+(float const &other) {
- return Vector({x + other, y + other});
- }
- Vector &operator+=(Vector const &other) {
- x += other.x;
- y += other.y;
- return *this;
- }
- Vector operator-(Vector const &other) {
- return Vector{x - other.x, y - other.y};
- }
- Vector &operator-=(Vector const &other) {
- x -= other.x;
- y -= other.y;
- return *this;
- }
- Vector operator-(Vector const &other) const {
- return Vector{x - other.x, y - other.y};
- }
- Vector operator*(Vector const &other) {
- return Vector{x * other.x, y * other.y};
- }
- Vector operator*(float other) {
- return Vector{x * other, y * other};
- }
- Vector &operator*=(Vector const &other) {
- x *= other.x;
- y *= other.y;
- return *this;
- }
- Vector &operator*=(float other) {
- x *= other;
- y *= other;
- return *this;
- }
- Vector operator/(Vector const &other) {
- return Vector{x / other.x, y / other.y};
- }
- Vector &operator/=(Vector const &other) {
- x /= other.x;
- y /= other.y;
- return *this;
- }
- Vector operator/(float other) {
- return Vector{x / other, y / other};
- }
- Vector &operator/=(float other) {
- x /= other;
- y /= other;
- return *this;
- }
- float magnitude() {
- return sqrtf(x * x + y * y);
- }
- float distance(Vector const &other) {
- Vector v = *this - other;
- return v.magnitude();
- }
- void normalize() {
- float m = magnitude();
- if (m == 0) {
- x = 0;
- y = 0;
- }else{
- x = x / m;
- y = y / m;
- }
- }
- Vector normalized() {
- float m = magnitude();
- if (m == 0) return {0, 0};
- return {x / m, y / m};
- }
- Vector inverse() {
- return {-x, -y};
- }
- float dot(Vector const &b) {
- return x * b.x + y * b.y;
- }
- void rotate(float deg) {
- float tx = x;
- float ty = y;
- x = (float) (cos(deg) * (double) tx - sin(deg) * (double) ty);
- y = (float) (sin(deg) * (double) tx + cos(deg) * (double) ty);
- }
- void rounded() {
- x = (float) round(x);
- y = (float) round(y);
- }
- Vector rotated(float deg) {
- return {
- (float) (cos(deg) * (double) x - sin(deg) * (double) y),
- (float) (sin(deg) * (double) x + cos(deg) * (double) y)
- };
- }
- float cross(Vector const &other) {
- return x * other.x - y * other.y;
- }
- Vector perpendicular() {
- return {-y, x};
- }
- Vector project(Vector const &lineA, Vector const &lineB, bool *success) {
- Vector AB = lineB - lineA;
- Vector AC = *this - lineA;
- float k = AC.dot(AB) / AB.dot(AB);
- if (k < 0 || k > 1) {
- *success = false;
- return {};
- }
- *success = true;
- return {
- k * AB.x + lineA.x,
- k * AB.y + lineA.y
- };
- }
- static Vector Lerp(Vector const &start, Vector const &end, float time) {
- return {
- lerp(start.x, end.x, time),
- lerp(start.y, end.y, time)
- };
- }
- static Vector Quadratic(Vector const &start, Vector const &control, Vector const &end, float time) {
- Vector a = Vector::Lerp(start, control, time);
- Vector b = Vector::Lerp(control, end, time);
- return Vector::Lerp(a, b, time);
- }
- };
|