mirror of
https://github.com/ProgramSnail/snake_2024.git
synced 2025-12-31 18:48:14 +00:00
moving circle prototype, circle and square drawing, part of food map implementation, utils
This commit is contained in:
parent
b4a4ffc08a
commit
4570d6e593
9 changed files with 378 additions and 18 deletions
99
include/Vec.hpp
Normal file
99
include/Vec.hpp
Normal file
|
|
@ -0,0 +1,99 @@
|
|||
#pragma once
|
||||
|
||||
#include <cmath>
|
||||
|
||||
template <typename T> struct Vec {
|
||||
T x;
|
||||
T y;
|
||||
|
||||
//
|
||||
|
||||
template <typename U> explicit operator Vec<U>() {
|
||||
return Vec<U>{.x = x, .y = y};
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
Vec &operator-() {
|
||||
x = -x;
|
||||
y = -y;
|
||||
return *this;
|
||||
}
|
||||
|
||||
Vec &operator*=(const T &value) {
|
||||
x *= value;
|
||||
y *= value;
|
||||
return *this;
|
||||
}
|
||||
|
||||
Vec &operator/=(const T &value) {
|
||||
x /= value;
|
||||
y /= value;
|
||||
return *this;
|
||||
}
|
||||
|
||||
Vec operator*(const T &value) const {
|
||||
auto copy = Vec{*this};
|
||||
copy *= value;
|
||||
return copy;
|
||||
}
|
||||
|
||||
Vec operator/(const T &value) const {
|
||||
auto copy = Vec{*this};
|
||||
copy /= value;
|
||||
return copy;
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
Vec &operator+=(const Vec &other) {
|
||||
x += other.x;
|
||||
y += other.y;
|
||||
return *this;
|
||||
}
|
||||
|
||||
Vec &operator-=(const Vec &other) {
|
||||
x -= other.x;
|
||||
y -= other.y;
|
||||
return *this;
|
||||
}
|
||||
|
||||
Vec operator+(const Vec &other) const {
|
||||
auto copy = Vec{*this};
|
||||
copy += other;
|
||||
return copy;
|
||||
}
|
||||
|
||||
Vec operator-(const Vec &other) const {
|
||||
auto copy = Vec{*this};
|
||||
copy -= other;
|
||||
return copy;
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
bool operator==(const Vec &other) const = default;
|
||||
|
||||
bool operator!=(const Vec &other) const = default;
|
||||
|
||||
//
|
||||
|
||||
int len_sq() const { return dot(*this, *this); }
|
||||
|
||||
double len() const { return std::sqrt(len_sq()); }
|
||||
|
||||
Vec<double> norm() { return Vec<double>(*this) / len(); }
|
||||
|
||||
//
|
||||
|
||||
int static dot(Vec left, Vec right) {
|
||||
return left.x * right.x + left.y * right.y;
|
||||
}
|
||||
|
||||
int static cross(Vec left, Vec right) {
|
||||
return left.x * right.y - left.y * right.x;
|
||||
}
|
||||
};
|
||||
|
||||
using Veci = Vec<int>;
|
||||
using Vecf = Vec<double>;
|
||||
Loading…
Add table
Add a link
Reference in a new issue