Skip to content

Instantly share code, notes, and snippets.

@acidleaf
Last active December 18, 2023 19:45
Show Gist options
  • Star 31 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save acidleaf/8957147 to your computer and use it in GitHub Desktop.
Save acidleaf/8957147 to your computer and use it in GitHub Desktop.
C++ 2D Vector
/*
Copyright (c) 2020 Chan Jer Shyan
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.
*/
#ifndef __VEC2_H__
#define __VEC2_H__
#include <cmath>
template <class T>
class vec2 {
public:
T x, y;
vec2() :x(0), y(0) {}
vec2(T x, T y) : x(x), y(y) {}
vec2(const vec2& v) : x(v.x), y(v.y) {}
vec2& operator=(const vec2& v) {
x = v.x;
y = v.y;
return *this;
}
vec2 operator+(vec2& v) {
return vec2(x + v.x, y + v.y);
}
vec2 operator-(vec2& v) {
return vec2(x - v.x, y - v.y);
}
vec2& operator+=(vec2& v) {
x += v.x;
y += v.y;
return *this;
}
vec2& operator-=(vec2& v) {
x -= v.x;
y -= v.y;
return *this;
}
vec2 operator+(double s) {
return vec2(x + s, y + s);
}
vec2 operator-(double s) {
return vec2(x - s, y - s);
}
vec2 operator*(double s) {
return vec2(x * s, y * s);
}
vec2 operator/(double s) {
return vec2(x / s, y / s);
}
vec2& operator+=(double s) {
x += s;
y += s;
return *this;
}
vec2& operator-=(double s) {
x -= s;
y -= s;
return *this;
}
vec2& operator*=(double s) {
x *= s;
y *= s;
return *this;
}
vec2& operator/=(double s) {
x /= s;
y /= s;
return *this;
}
void set(T x, T y) {
this->x = x;
this->y = y;
}
void rotate(double deg) {
double theta = deg / 180.0 * M_PI;
double c = cos(theta);
double s = sin(theta);
double tx = x * c - y * s;
double ty = x * s + y * c;
x = tx;
y = ty;
}
vec2& normalize() {
if (length() == 0) return *this;
*this *= (1.0 / length());
return *this;
}
float dist(vec2 v) const {
vec2 d(v.x - x, v.y - y);
return d.length();
}
float length() const {
return std::sqrt(x * x + y * y);
}
void truncate(double length) {
double angle = atan2f(y, x);
x = length * cos(angle);
y = length * sin(angle);
}
vec2 ortho() const {
return vec2(y, -x);
}
static float dot(vec2 v1, vec2 v2) {
return v1.x * v2.x + v1.y * v2.y;
}
static float cross(vec2 v1, vec2 v2) {
return (v1.x * v2.y) - (v1.y * v2.x);
}
};
typedef vec2<float> vec2f;
typedef vec2<double> vec2d;
#endif
@naortega
Copy link

What license do you have this under? I think I'd like to use it :)

@remar
Copy link

remar commented Jun 25, 2018

I'd also like to use this, in a GPL project. Can you consider applying a license to it?

@daCyuubi
Copy link

I know it's a late reply. But usually if a piece of source code is public domain, your free to reuse it in your licensed projects. AFAIK.

@acidleaf
Copy link
Author

acidleaf commented Jan 1, 2020

Hey guys, sorry for the late reply. Sure, you can use it in your own projects. I've applied the MIT license to it.
Happy coding!

Copy link

ghost commented Oct 22, 2020

@Cyuubi Sharing something online, despite putting the idea into the public domain, does not license something as such. As far as I know, a work's license defaults to "all rights reserved".

@lachsdachs
Copy link

damn, imma use that. Would have written it myself but this much neater, and it has all the features i would have made myself.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment