2013-05-29 20:51:51 -03:00
|
|
|
|
// -*- tab-width: 4; Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*-
|
2013-08-29 02:34:34 -03:00
|
|
|
|
/*
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
(at your option) any later version.
|
2010-09-08 05:21:46 -03:00
|
|
|
|
|
2013-08-29 02:34:34 -03:00
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
2010-09-08 05:21:46 -03:00
|
|
|
|
|
2013-08-29 02:34:34 -03:00
|
|
|
|
// Copyright 2010 Michael Smith, all rights reserved.
|
2010-09-08 05:21:46 -03:00
|
|
|
|
|
|
|
|
|
// Derived closely from:
|
|
|
|
|
/****************************************
|
2012-08-17 03:20:14 -03:00
|
|
|
|
* 2D Vector Classes
|
|
|
|
|
* By Bill Perone (billperone@yahoo.com)
|
|
|
|
|
* Original: 9-16-2002
|
|
|
|
|
* Revised: 19-11-2003
|
|
|
|
|
* 18-12-2003
|
|
|
|
|
* 06-06-2004
|
|
|
|
|
*
|
|
|
|
|
* <EFBFBD> 2003, This code is provided "as is" and you can use it freely as long as
|
|
|
|
|
* credit is given to Bill Perone in the application it is used in
|
|
|
|
|
****************************************/
|
2016-02-17 21:25:33 -04:00
|
|
|
|
#pragma once
|
2010-09-08 05:21:46 -03:00
|
|
|
|
|
2016-03-31 18:43:36 -03:00
|
|
|
|
#include <cmath>
|
2010-09-08 05:21:46 -03:00
|
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
|
struct Vector2
|
|
|
|
|
{
|
2012-08-17 03:20:14 -03:00
|
|
|
|
T x, y;
|
|
|
|
|
|
|
|
|
|
// trivial ctor
|
2016-04-22 09:59:24 -03:00
|
|
|
|
constexpr Vector2<T>()
|
|
|
|
|
: x(0)
|
|
|
|
|
, y(0) {}
|
2012-08-17 03:20:14 -03:00
|
|
|
|
|
|
|
|
|
// setting ctor
|
2016-04-22 09:59:24 -03:00
|
|
|
|
constexpr Vector2<T>(const T x0, const T y0)
|
|
|
|
|
: x(x0)
|
|
|
|
|
, y(y0) {}
|
2012-08-17 03:20:14 -03:00
|
|
|
|
|
|
|
|
|
// function call operator
|
|
|
|
|
void operator ()(const T x0, const T y0)
|
|
|
|
|
{
|
|
|
|
|
x= x0; y= y0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// test for equality
|
2013-04-11 21:04:18 -03:00
|
|
|
|
bool operator ==(const Vector2<T> &v) const;
|
2012-08-17 03:20:14 -03:00
|
|
|
|
|
|
|
|
|
// test for inequality
|
2013-04-11 21:04:18 -03:00
|
|
|
|
bool operator !=(const Vector2<T> &v) const;
|
2012-08-17 03:20:14 -03:00
|
|
|
|
|
|
|
|
|
// negation
|
2013-04-11 21:04:18 -03:00
|
|
|
|
Vector2<T> operator -(void) const;
|
2012-08-17 03:20:14 -03:00
|
|
|
|
|
|
|
|
|
// addition
|
2013-04-11 21:04:18 -03:00
|
|
|
|
Vector2<T> operator +(const Vector2<T> &v) const;
|
2012-08-17 03:20:14 -03:00
|
|
|
|
|
|
|
|
|
// subtraction
|
2013-04-11 21:04:18 -03:00
|
|
|
|
Vector2<T> operator -(const Vector2<T> &v) const;
|
2012-08-17 03:20:14 -03:00
|
|
|
|
|
|
|
|
|
// uniform scaling
|
2013-04-11 21:04:18 -03:00
|
|
|
|
Vector2<T> operator *(const T num) const;
|
2012-08-17 03:20:14 -03:00
|
|
|
|
|
|
|
|
|
// uniform scaling
|
2013-04-11 21:04:18 -03:00
|
|
|
|
Vector2<T> operator /(const T num) const;
|
2012-08-17 03:20:14 -03:00
|
|
|
|
|
|
|
|
|
// addition
|
2013-04-11 21:04:18 -03:00
|
|
|
|
Vector2<T> &operator +=(const Vector2<T> &v);
|
2012-08-17 03:20:14 -03:00
|
|
|
|
|
|
|
|
|
// subtraction
|
2013-04-11 21:04:18 -03:00
|
|
|
|
Vector2<T> &operator -=(const Vector2<T> &v);
|
2012-08-17 03:20:14 -03:00
|
|
|
|
|
|
|
|
|
// uniform scaling
|
2013-04-11 21:04:18 -03:00
|
|
|
|
Vector2<T> &operator *=(const T num);
|
2012-08-17 03:20:14 -03:00
|
|
|
|
|
|
|
|
|
// uniform scaling
|
2013-04-11 21:04:18 -03:00
|
|
|
|
Vector2<T> &operator /=(const T num);
|
2012-08-17 03:20:14 -03:00
|
|
|
|
|
|
|
|
|
// dot product
|
2013-04-11 21:04:18 -03:00
|
|
|
|
T operator *(const Vector2<T> &v) const;
|
|
|
|
|
|
|
|
|
|
// cross product
|
2013-05-05 00:47:51 -03:00
|
|
|
|
T operator %(const Vector2<T> &v) const;
|
2013-04-11 21:04:18 -03:00
|
|
|
|
|
|
|
|
|
// computes the angle between this vector and another vector
|
|
|
|
|
float angle(const Vector2<T> &v2) const;
|
|
|
|
|
|
|
|
|
|
// computes the angle in radians between the origin and this vector
|
|
|
|
|
T angle(void) const;
|
|
|
|
|
|
|
|
|
|
// check if any elements are NAN
|
|
|
|
|
bool is_nan(void) const;
|
|
|
|
|
|
|
|
|
|
// check if any elements are infinity
|
|
|
|
|
bool is_inf(void) const;
|
2012-08-17 03:20:14 -03:00
|
|
|
|
|
2015-09-15 03:53:44 -03:00
|
|
|
|
// check if all elements are zero
|
|
|
|
|
bool is_zero(void) const { return (fabsf(x) < FLT_EPSILON) && (fabsf(y) < FLT_EPSILON); }
|
|
|
|
|
|
2016-05-18 07:23:39 -03:00
|
|
|
|
const T & operator[](uint8_t i) const {
|
|
|
|
|
const T *_v = &x;
|
|
|
|
|
#if MATH_CHECK_INDEXES
|
|
|
|
|
assert(i >= 0 && i < 2);
|
|
|
|
|
#endif
|
|
|
|
|
return _v[i];
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-19 22:20:36 -04:00
|
|
|
|
// zero the vector
|
|
|
|
|
void zero()
|
|
|
|
|
{
|
|
|
|
|
x = y = 0;
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-17 03:20:14 -03:00
|
|
|
|
// gets the length of this vector squared
|
|
|
|
|
T length_squared() const
|
|
|
|
|
{
|
|
|
|
|
return (T)(*this * *this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// gets the length of this vector
|
2012-12-18 22:33:52 -04:00
|
|
|
|
float length(void) const;
|
2012-08-17 03:20:14 -03:00
|
|
|
|
|
|
|
|
|
// normalizes this vector
|
|
|
|
|
void normalize()
|
|
|
|
|
{
|
|
|
|
|
*this/=length();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// returns the normalized vector
|
|
|
|
|
Vector2<T> normalized() const
|
|
|
|
|
{
|
|
|
|
|
return *this/length();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// reflects this vector about n
|
|
|
|
|
void reflect(const Vector2<T> &n)
|
|
|
|
|
{
|
|
|
|
|
Vector2<T> orig(*this);
|
|
|
|
|
project(n);
|
|
|
|
|
*this= *this*2 - orig;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// projects this vector onto v
|
|
|
|
|
void project(const Vector2<T> &v)
|
|
|
|
|
{
|
|
|
|
|
*this= v * (*this * v)/(v*v);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// returns this vector projected onto v
|
|
|
|
|
Vector2<T> projected(const Vector2<T> &v)
|
|
|
|
|
{
|
|
|
|
|
return v * (*this * v)/(v*v);
|
|
|
|
|
}
|
2016-06-05 23:19:36 -03:00
|
|
|
|
|
|
|
|
|
// given a position p1 and a velocity v1 produce a vector
|
|
|
|
|
// perpendicular to v1 maximising distance from p1
|
|
|
|
|
static Vector2<T> perpendicular(const Vector2<T> &pos_delta, const Vector2<T> &v1)
|
|
|
|
|
{
|
|
|
|
|
Vector2<T> perpendicular1 = Vector2<T>(-v1[1], v1[0]);
|
|
|
|
|
Vector2<T> perpendicular2 = Vector2<T>(v1[1], -v1[0]);
|
|
|
|
|
T d1 = perpendicular1 * pos_delta;
|
|
|
|
|
T d2 = perpendicular2 * pos_delta;
|
|
|
|
|
if (d1 > d2) {
|
|
|
|
|
return perpendicular1;
|
|
|
|
|
}
|
|
|
|
|
return perpendicular2;
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-24 22:56:10 -03:00
|
|
|
|
/*
|
|
|
|
|
* Returns the point closest to p on the line segment (v,w).
|
|
|
|
|
*
|
|
|
|
|
* Comments and implementation taken from
|
|
|
|
|
* http://stackoverflow.com/questions/849211/shortest-distance-between-a-point-and-a-line-segment
|
|
|
|
|
*/
|
|
|
|
|
static Vector2<T> closest_point(const Vector2<T> &p, const Vector2<T> &v, const Vector2<T> &w)
|
|
|
|
|
{
|
|
|
|
|
// length squared of line segment
|
|
|
|
|
const float l2 = (v - w).length_squared();
|
|
|
|
|
if (l2 < FLT_EPSILON) {
|
|
|
|
|
// v == w case
|
|
|
|
|
return v;
|
|
|
|
|
}
|
|
|
|
|
// Consider the line extending the segment, parameterized as v + t (w - v).
|
|
|
|
|
// We find projection of point p onto the line.
|
|
|
|
|
// It falls where t = [(p-v) . (w-v)] / |w-v|^2
|
|
|
|
|
// We clamp t from [0,1] to handle points outside the segment vw.
|
|
|
|
|
const float t = ((p - v) * (w - v)) / l2;
|
|
|
|
|
if (t <= 0) {
|
|
|
|
|
return v;
|
|
|
|
|
} else if (t >= 1) {
|
|
|
|
|
return w;
|
|
|
|
|
} else {
|
|
|
|
|
return v + (w - v)*t;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-05 23:19:36 -03:00
|
|
|
|
// w defines a line segment from the origin
|
|
|
|
|
// p is a point
|
|
|
|
|
// returns the closest distance between the radial and the point
|
|
|
|
|
static float closest_distance_between_radial_and_point(const Vector2<T> &w,
|
|
|
|
|
const Vector2<T> &p)
|
|
|
|
|
{
|
2016-07-24 22:56:10 -03:00
|
|
|
|
const Vector2<T> closest = closest_point(p, Vector2<T>(0,0), w);
|
|
|
|
|
const Vector2<T> delta = closest - p;
|
|
|
|
|
return delta.length();
|
2016-06-05 23:19:36 -03:00
|
|
|
|
}
|
|
|
|
|
|
2010-09-08 05:21:46 -03:00
|
|
|
|
};
|
|
|
|
|
|
2012-08-17 03:20:14 -03:00
|
|
|
|
typedef Vector2<int16_t> Vector2i;
|
|
|
|
|
typedef Vector2<uint16_t> Vector2ui;
|
|
|
|
|
typedef Vector2<int32_t> Vector2l;
|
|
|
|
|
typedef Vector2<uint32_t> Vector2ul;
|
|
|
|
|
typedef Vector2<float> Vector2f;
|