Skip to content

Instantly share code, notes, and snippets.

@J-Vernay
Created August 6, 2018 11:08
Show Gist options
  • Save J-Vernay/adfee4bbd7b73cd07a71bd5fa2a3ae12 to your computer and use it in GitHub Desktop.
Save J-Vernay/adfee4bbd7b73cd07a71bd5fa2a3ae12 to your computer and use it in GitHub Desktop.
Vec2f and Pointf in C++
/*
Copyright 2018 Julien Vernay - MIT License
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge,
publish, distribute, sublicense, and/or sell copies of the Software,
and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#pragma once
#include <cmath>
const double PI = 4 * atan(1);
inline float deg2rad(float a_) { return a_ * (PI / 180); }
inline float rad2deg(float a_) { return a_ * (180 / PI); }
//{ Vec2f
struct Vec2f {
float x, y;
constexpr Vec2f() : x(0), y(0) {}
constexpr Vec2f(float x_, float y_) : x(x_), y(y_) {}
constexpr Vec2f& operator+=(Vec2f rhs_) { x += rhs_.x; y += rhs_.y; return *this; }
constexpr Vec2f& operator-=(Vec2f rhs_) { x -= rhs_.x; y -= rhs_.y; return *this; }
constexpr Vec2f& operator*=(float a_) { x *= a_; y *= a_; return *this; }
constexpr Vec2f& operator/=(float a_) { x /= a_; y /= a_; return *this; }
constexpr float sqlen() const { return x*x + y*y; }
float len() const { return sqrt(x*x + y*y); } // sqrt in cmath is not constexpr :<
Vec2f unit() const { return Vec2f(*this) /= len(); }
Vec2f& rotate(float a_) {
float x_ = x, c = cos(a_), s = sin(a_);
x = x * c - y * s;
y = x_ * s + y * c;
return *this;
}
Vec2f& rotateDeg(float a_) { return rotate(deg2rad(a_)); }
constexpr Vec2f& rotate90() { float x_ = x; x = -y; y = x_; return *this; }
constexpr Vec2f& rotate180() { x = -x; y = -y; return *this; }
constexpr Vec2f& rotate270() { float x_ = x; x = y; y = -x_; return *this; }
};
constexpr inline Vec2f operator+(Vec2f lhs_, Vec2f rhs_) { return lhs_ += rhs_; }
constexpr inline Vec2f operator-(Vec2f lhs_, Vec2f rhs_) { return lhs_ -= rhs_; }
constexpr inline Vec2f operator*(Vec2f lhs_, float rhs_) { return lhs_ *= rhs_; }
constexpr inline Vec2f operator*(float lhs_, Vec2f rhs_) { return rhs_ *= lhs_; }
constexpr inline Vec2f operator/(Vec2f lhs_, float rhs_) { return lhs_ /= rhs_; }
constexpr inline Vec2f operator-(Vec2f a_) { return Vec2f{ -a_.x, -a_.y }; }
constexpr inline bool operator==(Vec2f lhs_, Vec2f rhs_) { return lhs_.x == rhs_.x && lhs_.y == rhs_.y; }
constexpr inline bool operator!=(Vec2f lhs_, Vec2f rhs_) { return lhs_.x != rhs_.x || lhs_.y != rhs_.y; }
constexpr inline float dot(Vec2f lhs_, Vec2f rhs_) { return lhs_.x * rhs_.x + lhs_.y * rhs_.y; }
inline Vec2f rotate(Vec2f lhs_, float rhs_) { return lhs_.rotate(rhs_); }
inline Vec2f rotateDeg(Vec2f lhs_, float rhs_) { return lhs_.rotateDeg(rhs_); }
constexpr inline Vec2f rotate90(Vec2f lhs_) { return lhs_.rotate90(); }
constexpr inline Vec2f rotate180(Vec2f lhs_) { return -lhs_; }
constexpr inline Vec2f rotate270(Vec2f lhs_) { return lhs_.rotate270(); }
//}
//{ Pointf
struct Pointf;
constexpr inline Vec2f operator-(Pointf lhs_, Pointf rhs_);
struct Pointf {
float x, y;
constexpr Pointf() : x(0), y(0) {}
constexpr Pointf(float x_, float y_) : x(x_), y(y_) {}
constexpr Pointf& operator+=(Vec2f rhs_) { x += rhs_.x; y += rhs_.y; return *this; }
constexpr Pointf& operator-=(Vec2f rhs_) { x -= rhs_.x; y -= rhs_.y; return *this; }
Pointf& rotate(float a_, Pointf pt_ = Pointf()) { return *this += ::rotate(*this - pt_, a_); }
constexpr Pointf& rotate90(Pointf pt_ = Pointf()) { return *this += ::rotate90(*this - pt_); }
constexpr Pointf& rotate180(Pointf pt_ = Pointf()) { return *this -= *this - pt_; }
constexpr Pointf& rotate270(Pointf pt_ = Pointf()) { return *this += ::rotate270(*this - pt_); }
};
constexpr inline Pointf operator+(Pointf lhs_, Vec2f rhs_) { return lhs_ += rhs_; }
constexpr inline Pointf operator+(Vec2f lhs_, Pointf rhs_) { return rhs_ += lhs_; }
constexpr inline Pointf operator-(Pointf lhs_, Vec2f rhs_) { return lhs_ -= rhs_; }
constexpr inline Vec2f operator-(Pointf lhs_, Pointf rhs_) { return Vec2f{ lhs_.x - rhs_.x, lhs_.y - rhs_.y }; }
inline Pointf rotate(Pointf pt_, float a_, Pointf anch_ = Pointf()) { return pt_.rotate(a_, anch_); }
constexpr inline Pointf rotate90(Pointf pt_, Pointf anch_ = Pointf()) { return pt_.rotate90(anch_); }
constexpr inline Pointf rotate180(Pointf pt_, Pointf anch_ = Pointf()) { return pt_.rotate180(anch_); }
constexpr inline Pointf rotate170(Pointf pt_, Pointf anch_ = Pointf()) { return pt_.rotate270(anch_); }
//}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment