2012-12-18 22:33:52 -04:00
|
|
|
/*
|
|
|
|
* vector3.cpp
|
|
|
|
* Copyright (C) Andrew Tridgell 2012
|
|
|
|
*
|
|
|
|
* This file 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.
|
|
|
|
*
|
|
|
|
* This file 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/>.
|
|
|
|
*/
|
|
|
|
|
2019-09-27 17:45:12 -03:00
|
|
|
#pragma GCC optimize("O2")
|
2015-10-19 18:27:22 -03:00
|
|
|
|
2012-12-18 22:33:52 -04:00
|
|
|
#include "AP_Math.h"
|
|
|
|
|
2019-05-29 08:16:12 -03:00
|
|
|
template <typename T>
|
2021-05-04 08:12:23 -03:00
|
|
|
T Vector2<T>::length_squared() const
|
2019-05-29 08:16:12 -03:00
|
|
|
{
|
2021-05-04 08:12:23 -03:00
|
|
|
return (T)(x*x + y*y);
|
2019-05-29 08:16:12 -03:00
|
|
|
}
|
|
|
|
|
2012-12-18 22:33:52 -04:00
|
|
|
template <typename T>
|
2021-05-04 08:12:23 -03:00
|
|
|
T Vector2<T>::length(void) const
|
2012-12-18 22:33:52 -04:00
|
|
|
{
|
2019-05-29 08:31:50 -03:00
|
|
|
return norm(x, y);
|
2012-12-18 22:33:52 -04:00
|
|
|
}
|
|
|
|
|
2021-03-11 08:04:12 -04:00
|
|
|
// limit vector to a given length. returns true if vector was limited
|
|
|
|
template <typename T>
|
2021-05-04 08:12:23 -03:00
|
|
|
bool Vector2<T>::limit_length(T max_length)
|
2021-03-11 08:04:12 -04:00
|
|
|
{
|
2021-05-04 08:12:23 -03:00
|
|
|
const T len = length();
|
2021-03-11 08:04:12 -04:00
|
|
|
if ((len > max_length) && is_positive(len)) {
|
|
|
|
x *= (max_length / len);
|
|
|
|
y *= (max_length / len);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-04-11 21:04:18 -03:00
|
|
|
// dot product
|
|
|
|
template <typename T>
|
|
|
|
T Vector2<T>::operator *(const Vector2<T> &v) const
|
|
|
|
{
|
|
|
|
return x*v.x + y*v.y;
|
|
|
|
}
|
|
|
|
|
2013-05-05 00:47:51 -03:00
|
|
|
// cross product
|
|
|
|
template <typename T>
|
|
|
|
T Vector2<T>::operator %(const Vector2<T> &v) const
|
|
|
|
{
|
|
|
|
return x*v.y - y*v.x;
|
|
|
|
}
|
|
|
|
|
2013-04-11 21:04:18 -03:00
|
|
|
template <typename T>
|
|
|
|
Vector2<T> &Vector2<T>::operator *=(const T num)
|
|
|
|
{
|
|
|
|
x*=num; y*=num;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
Vector2<T> &Vector2<T>::operator /=(const T num)
|
|
|
|
{
|
|
|
|
x /= num; y /= num;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
Vector2<T> &Vector2<T>::operator -=(const Vector2<T> &v)
|
|
|
|
{
|
|
|
|
x -= v.x; y -= v.y;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
bool Vector2<T>::is_nan(void) const
|
|
|
|
{
|
|
|
|
return isnan(x) || isnan(y);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
bool Vector2<T>::is_inf(void) const
|
|
|
|
{
|
|
|
|
return isinf(x) || isinf(y);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
Vector2<T> &Vector2<T>::operator +=(const Vector2<T> &v)
|
|
|
|
{
|
|
|
|
x+=v.x; y+=v.y;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
Vector2<T> Vector2<T>::operator /(const T num) const
|
|
|
|
{
|
|
|
|
return Vector2<T>(x/num, y/num);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
Vector2<T> Vector2<T>::operator *(const T num) const
|
|
|
|
{
|
|
|
|
return Vector2<T>(x*num, y*num);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
Vector2<T> Vector2<T>::operator -(const Vector2<T> &v) const
|
|
|
|
{
|
|
|
|
return Vector2<T>(x-v.x, y-v.y);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
Vector2<T> Vector2<T>::operator +(const Vector2<T> &v) const
|
|
|
|
{
|
|
|
|
return Vector2<T>(x+v.x, y+v.y);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
Vector2<T> Vector2<T>::operator -(void) const
|
|
|
|
{
|
|
|
|
return Vector2<T>(-x,-y);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
bool Vector2<T>::operator ==(const Vector2<T> &v) const
|
|
|
|
{
|
2015-05-04 23:35:31 -03:00
|
|
|
return (is_equal(x,v.x) && is_equal(y,v.y));
|
2013-04-11 21:04:18 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
bool Vector2<T>::operator !=(const Vector2<T> &v) const
|
|
|
|
{
|
2015-05-04 23:35:31 -03:00
|
|
|
return (!is_equal(x,v.x) || !is_equal(y,v.y));
|
2013-04-11 21:04:18 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
2021-05-04 08:12:23 -03:00
|
|
|
T Vector2<T>::angle(const Vector2<T> &v2) const
|
2013-04-11 21:04:18 -03:00
|
|
|
{
|
2021-05-04 08:12:23 -03:00
|
|
|
const T len = this->length() * v2.length();
|
2014-04-21 02:23:25 -03:00
|
|
|
if (len <= 0) {
|
|
|
|
return 0.0f;
|
|
|
|
}
|
2021-05-04 08:12:23 -03:00
|
|
|
const T cosv = ((*this)*v2) / len;
|
2017-03-01 19:22:01 -04:00
|
|
|
if (cosv >= 1) {
|
2014-04-21 02:23:25 -03:00
|
|
|
return 0.0f;
|
|
|
|
}
|
2017-03-01 19:22:01 -04:00
|
|
|
if (cosv <= -1) {
|
|
|
|
return M_PI;
|
|
|
|
}
|
2021-05-04 08:12:23 -03:00
|
|
|
return acosF(cosv);
|
2013-04-11 21:04:18 -03:00
|
|
|
}
|
|
|
|
|
2019-05-11 01:58:28 -03:00
|
|
|
template <typename T>
|
2021-05-04 08:12:23 -03:00
|
|
|
T Vector2<T>::angle(void) const
|
2019-05-11 01:58:28 -03:00
|
|
|
{
|
2021-07-09 01:03:04 -03:00
|
|
|
return M_PI_2 + atan2F(-x, y);
|
2019-05-11 01:58:28 -03:00
|
|
|
}
|
|
|
|
|
2017-12-13 00:40:57 -04:00
|
|
|
// find the intersection between two line segments
|
|
|
|
// returns true if they intersect, false if they do not
|
|
|
|
// the point of intersection is returned in the intersection argument
|
|
|
|
template <typename T>
|
|
|
|
bool Vector2<T>::segment_intersection(const Vector2<T>& seg1_start, const Vector2<T>& seg1_end, const Vector2<T>& seg2_start, const Vector2<T>& seg2_end, Vector2<T>& intersection)
|
|
|
|
{
|
|
|
|
// implementation borrowed from http://stackoverflow.com/questions/563198/how-do-you-detect-where-two-line-segments-intersect
|
|
|
|
const Vector2<T> r1 = seg1_end - seg1_start;
|
|
|
|
const Vector2<T> r2 = seg2_end - seg2_start;
|
|
|
|
const Vector2<T> ss2_ss1 = seg2_start - seg1_start;
|
2021-05-04 08:12:23 -03:00
|
|
|
const T r1xr2 = r1 % r2;
|
|
|
|
const T q_pxr = ss2_ss1 % r1;
|
2022-01-25 16:15:41 -04:00
|
|
|
if (::is_zero(r1xr2)) {
|
2017-12-13 00:40:57 -04:00
|
|
|
// either collinear or parallel and non-intersecting
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
// t = (q - p) * s / (r * s)
|
|
|
|
// u = (q - p) * r / (r * s)
|
2021-05-04 08:12:23 -03:00
|
|
|
const T t = (ss2_ss1 % r2) / r1xr2;
|
|
|
|
const T u = q_pxr / r1xr2;
|
2018-08-12 23:34:23 -03:00
|
|
|
if ((u >= 0) && (u <= 1) && (t >= 0) && (t <= 1)) {
|
2017-12-13 00:40:57 -04:00
|
|
|
// lines intersect
|
|
|
|
// t can be any non-negative value because (p, p + r) is a ray
|
|
|
|
// u must be between 0 and 1 because (q, q + s) is a line segment
|
|
|
|
intersection = seg1_start + (r1*t);
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
// non-parallel and non-intersecting
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-17 01:21:40 -04:00
|
|
|
// find the intersection between a line segment and a circle
|
|
|
|
// returns true if they intersect and intersection argument is updated with intersection closest to seg_start
|
|
|
|
// solution adapted from http://stackoverflow.com/questions/1073336/circle-line-segment-collision-detection-algorithm
|
|
|
|
template <typename T>
|
2021-05-04 08:12:23 -03:00
|
|
|
bool Vector2<T>::circle_segment_intersection(const Vector2<T>& seg_start, const Vector2<T>& seg_end, const Vector2<T>& circle_center, T radius, Vector2<T>& intersection)
|
2018-01-17 01:21:40 -04:00
|
|
|
{
|
|
|
|
// calculate segment start and end as offsets from circle's center
|
2021-06-17 22:17:50 -03:00
|
|
|
const Vector2<T> seg_start_local = seg_start - circle_center;
|
2018-01-17 01:21:40 -04:00
|
|
|
|
|
|
|
// calculate vector from start to end
|
2021-06-17 22:17:50 -03:00
|
|
|
const Vector2<T> seg_end_minus_start = seg_end - seg_start;
|
2018-01-17 01:21:40 -04:00
|
|
|
|
2021-05-04 08:12:23 -03:00
|
|
|
const T a = sq(seg_end_minus_start.x) + sq(seg_end_minus_start.y);
|
|
|
|
const T b = 2 * ((seg_end_minus_start.x * seg_start_local.x) + (seg_end_minus_start.y * seg_start_local.y));
|
|
|
|
const T c = sq(seg_start_local.x) + sq(seg_start_local.y) - sq(radius);
|
2020-03-14 04:55:42 -03:00
|
|
|
|
2021-01-03 21:24:53 -04:00
|
|
|
// check for invalid data
|
|
|
|
if (::is_zero(a) || isnan(a) || isnan(b) || isnan(c)) {
|
2020-03-14 04:55:42 -03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-05-04 08:12:23 -03:00
|
|
|
const T delta = sq(b) - (4.0f * a * c);
|
2018-01-17 01:21:40 -04:00
|
|
|
|
2020-03-14 04:55:42 -03:00
|
|
|
if (isnan(delta)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-01-17 01:21:40 -04:00
|
|
|
// check for invalid delta (i.e. discriminant)
|
|
|
|
if (delta < 0.0f) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-05-04 08:12:23 -03:00
|
|
|
const T delta_sqrt = sqrtF(delta);
|
|
|
|
const T t1 = (-b + delta_sqrt) / (2.0f * a);
|
|
|
|
const T t2 = (-b - delta_sqrt) / (2.0f * a);
|
2018-01-17 01:21:40 -04:00
|
|
|
|
|
|
|
// Three hit cases:
|
|
|
|
// -o-> --|--> | | --|->
|
|
|
|
// Impale(t1 hit,t2 hit), Poke(t1 hit,t2>1), ExitWound(t1<0, t2 hit),
|
|
|
|
|
|
|
|
// Three miss cases:
|
|
|
|
// -> o o -> | -> |
|
|
|
|
// FallShort (t1>1,t2>1), Past (t1<0,t2<0), CompletelyInside(t1<0, t2>1)
|
|
|
|
|
|
|
|
// intersection = new Vector3(E.x + t1 * d.x, secondPoint.y, E.y + t1 * d.y);
|
|
|
|
// intersection.x = seg_start.x + t1 * seg_end_minus_start.x;
|
|
|
|
// intersection.y = seg_start.y + t1 * seg_end_minus_start.y;
|
|
|
|
|
|
|
|
if ((t1 >= 0.0f) && (t1 <= 1.0f)) {
|
|
|
|
// t1 is the intersection, and it is closer than t2 (since t1 uses -b - discriminant)
|
|
|
|
// Impale, Poke
|
|
|
|
intersection = seg_start + (seg_end_minus_start * t1);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// here t1 did not intersect so we are either started inside the sphere or completely past it
|
|
|
|
if ((t2 >= 0.0f) && (t2 <= 1.0f)) {
|
|
|
|
// ExitWound
|
|
|
|
intersection = seg_start + (seg_end_minus_start * t2);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// no intersection: FallShort, Past or CompletelyInside
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-05-29 08:39:02 -03:00
|
|
|
// normalizes this vector
|
|
|
|
template <typename T>
|
|
|
|
void Vector2<T>::normalize()
|
|
|
|
{
|
|
|
|
*this /= length();
|
|
|
|
}
|
|
|
|
|
|
|
|
// returns the normalized vector
|
|
|
|
template <typename T>
|
|
|
|
Vector2<T> Vector2<T>::normalized() const
|
|
|
|
{
|
|
|
|
return *this/length();
|
|
|
|
}
|
|
|
|
|
|
|
|
// reflects this vector about n
|
|
|
|
template <typename T>
|
|
|
|
void Vector2<T>::reflect(const Vector2<T> &n)
|
|
|
|
{
|
|
|
|
const Vector2<T> orig(*this);
|
|
|
|
project(n);
|
|
|
|
*this = *this*2 - orig;
|
|
|
|
}
|
|
|
|
|
|
|
|
// projects this vector onto v
|
|
|
|
template <typename T>
|
|
|
|
void Vector2<T>::project(const Vector2<T> &v)
|
|
|
|
{
|
|
|
|
*this= v * (*this * v)/(v*v);
|
|
|
|
}
|
|
|
|
|
|
|
|
// returns this vector projected onto v
|
|
|
|
template <typename T>
|
2023-06-27 01:09:13 -03:00
|
|
|
Vector2<T> Vector2<T>::projected(const Vector2<T> &v) const
|
2019-05-29 08:39:02 -03:00
|
|
|
{
|
|
|
|
return v * (*this * v)/(v*v);
|
|
|
|
}
|
|
|
|
|
2019-12-04 03:09:48 -04:00
|
|
|
// extrapolate position given bearing (in degrees) and distance
|
|
|
|
template <typename T>
|
2021-05-04 08:12:23 -03:00
|
|
|
void Vector2<T>::offset_bearing(T bearing, T distance)
|
2019-12-04 03:09:48 -04:00
|
|
|
{
|
2021-05-04 08:12:23 -03:00
|
|
|
x += cosF(radians(bearing)) * distance;
|
|
|
|
y += sinF(radians(bearing)) * distance;
|
2019-12-04 03:09:48 -04:00
|
|
|
}
|
|
|
|
|
2019-05-29 08:39:02 -03:00
|
|
|
// given a position pos_delta and a velocity v1 produce a vector
|
|
|
|
// perpendicular to v1 maximising distance from p1
|
|
|
|
template <typename T>
|
|
|
|
Vector2<T> Vector2<T>::perpendicular(const Vector2<T> &pos_delta, const Vector2<T> &v1)
|
|
|
|
{
|
|
|
|
const Vector2<T> perpendicular1 = Vector2<T>(-v1[1], v1[0]);
|
|
|
|
const Vector2<T> perpendicular2 = Vector2<T>(v1[1], -v1[0]);
|
|
|
|
const T d1 = perpendicular1 * pos_delta;
|
|
|
|
const T d2 = perpendicular2 * pos_delta;
|
|
|
|
if (d1 > d2) {
|
|
|
|
return perpendicular1;
|
|
|
|
}
|
|
|
|
return perpendicular2;
|
|
|
|
}
|
|
|
|
|
2019-05-29 08:46:09 -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
|
|
|
|
*/
|
|
|
|
template <typename T>
|
|
|
|
Vector2<T> Vector2<T>::closest_point(const Vector2<T> &p, const Vector2<T> &v, const Vector2<T> &w)
|
|
|
|
{
|
|
|
|
// length squared of line segment
|
2021-05-04 08:12:23 -03:00
|
|
|
const T l2 = (v - w).length_squared();
|
2019-05-29 08:46:09 -03:00
|
|
|
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.
|
2021-05-04 08:12:23 -03:00
|
|
|
const T t = ((p - v) * (w - v)) / l2;
|
2019-05-29 08:46:09 -03:00
|
|
|
if (t <= 0) {
|
|
|
|
return v;
|
|
|
|
} else if (t >= 1) {
|
|
|
|
return w;
|
|
|
|
} else {
|
|
|
|
return v + (w - v)*t;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-29 09:02:32 -03:00
|
|
|
/*
|
|
|
|
* Returns the point closest to p on the line segment (0,w).
|
|
|
|
*
|
|
|
|
* this is a simplification of closest point with a general segment, with v=(0,0)
|
|
|
|
*/
|
|
|
|
template <typename T>
|
|
|
|
Vector2<T> Vector2<T>::closest_point(const Vector2<T> &p, const Vector2<T> &w)
|
|
|
|
{
|
|
|
|
// length squared of line segment
|
2021-05-04 08:12:23 -03:00
|
|
|
const T l2 = w.length_squared();
|
2019-05-29 09:02:32 -03:00
|
|
|
if (l2 < FLT_EPSILON) {
|
|
|
|
// v == w case
|
|
|
|
return w;
|
|
|
|
}
|
2021-05-04 08:12:23 -03:00
|
|
|
const T t = (p * w) / l2;
|
2019-05-29 09:02:32 -03:00
|
|
|
if (t <= 0) {
|
|
|
|
return Vector2<T>(0,0);
|
|
|
|
} else if (t >= 1) {
|
|
|
|
return w;
|
|
|
|
} else {
|
|
|
|
return w*t;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-29 09:05:24 -03:00
|
|
|
// closest distance between a line segment and a point
|
|
|
|
// https://stackoverflow.com/questions/2824478/shortest-distance-between-two-line-segments
|
|
|
|
template <typename T>
|
2021-05-04 08:12:23 -03:00
|
|
|
T Vector2<T>::closest_distance_between_line_and_point_squared(const Vector2<T> &w1,
|
|
|
|
const Vector2<T> &w2,
|
|
|
|
const Vector2<T> &p)
|
2019-05-29 09:05:24 -03:00
|
|
|
{
|
|
|
|
return closest_distance_between_radial_and_point_squared(w2-w1, p-w1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// w1 and w2 define a line segment
|
|
|
|
// p is a point
|
|
|
|
// returns the closest distance between the line segment and the point
|
|
|
|
template <typename T>
|
2021-05-04 08:12:23 -03:00
|
|
|
T Vector2<T>::closest_distance_between_line_and_point(const Vector2<T> &w1,
|
|
|
|
const Vector2<T> &w2,
|
|
|
|
const Vector2<T> &p)
|
2019-05-29 09:05:24 -03:00
|
|
|
{
|
2021-05-04 08:12:23 -03:00
|
|
|
return sqrtF(closest_distance_between_line_and_point_squared(w1, w2, p));
|
2019-05-29 09:05:24 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
// a1->a2 and b2->v2 define two line segments
|
|
|
|
// returns the square of the closest distance between the two line segments
|
|
|
|
// see https://stackoverflow.com/questions/2824478/shortest-distance-between-two-line-segments
|
|
|
|
template <typename T>
|
2021-05-04 08:12:23 -03:00
|
|
|
T Vector2<T>::closest_distance_between_lines_squared(const Vector2<T> &a1,
|
|
|
|
const Vector2<T> &a2,
|
|
|
|
const Vector2<T> &b1,
|
|
|
|
const Vector2<T> &b2)
|
|
|
|
{
|
|
|
|
const T dist1 = Vector2<T>::closest_distance_between_line_and_point_squared(b1,b2,a1);
|
|
|
|
const T dist2 = Vector2<T>::closest_distance_between_line_and_point_squared(b1,b2,a2);
|
|
|
|
const T dist3 = Vector2<T>::closest_distance_between_line_and_point_squared(a1,a2,b1);
|
|
|
|
const T dist4 = Vector2<T>::closest_distance_between_line_and_point_squared(a1,a2,b2);
|
|
|
|
const T m1 = MIN(dist1,dist2);
|
|
|
|
const T m2 = MIN(dist3,dist4);
|
2019-05-29 09:05:24 -03:00
|
|
|
return MIN(m1,m2);
|
|
|
|
}
|
|
|
|
|
2019-05-29 09:00:13 -03:00
|
|
|
// w defines a line segment from the origin
|
|
|
|
// p is a point
|
|
|
|
// returns the square of the closest distance between the radial and the point
|
|
|
|
template <typename T>
|
2021-05-04 08:12:23 -03:00
|
|
|
T Vector2<T>::closest_distance_between_radial_and_point_squared(const Vector2<T> &w,
|
|
|
|
const Vector2<T> &p)
|
2019-05-29 09:00:13 -03:00
|
|
|
{
|
|
|
|
const Vector2<T> closest = closest_point(p, w);
|
|
|
|
return (closest - p).length_squared();
|
|
|
|
}
|
|
|
|
|
|
|
|
// w defines a line segment from the origin
|
|
|
|
// p is a point
|
|
|
|
// returns the closest distance between the radial and the point
|
|
|
|
template <typename T>
|
2021-05-04 08:12:23 -03:00
|
|
|
T Vector2<T>::closest_distance_between_radial_and_point(const Vector2<T> &w,
|
2019-05-29 09:00:13 -03:00
|
|
|
const Vector2<T> &p)
|
|
|
|
{
|
2021-05-04 08:12:23 -03:00
|
|
|
return sqrtF(closest_distance_between_radial_and_point_squared(w,p));
|
2019-05-29 09:00:13 -03:00
|
|
|
}
|
|
|
|
|
2020-07-03 01:05:34 -03:00
|
|
|
// rotate vector by angle in radians
|
|
|
|
template <typename T>
|
2021-05-04 08:12:23 -03:00
|
|
|
void Vector2<T>::rotate(T angle_rad)
|
2020-07-03 01:05:34 -03:00
|
|
|
{
|
2021-05-04 08:12:23 -03:00
|
|
|
const T cs = cosF(angle_rad);
|
|
|
|
const T sn = sinF(angle_rad);
|
|
|
|
T rx = x * cs - y * sn;
|
|
|
|
T ry = x * sn + y * cs;
|
2020-07-03 01:05:34 -03:00
|
|
|
x = rx;
|
|
|
|
y = ry;
|
|
|
|
}
|
|
|
|
|
2021-06-17 22:17:50 -03:00
|
|
|
// define for float and double
|
2021-01-09 01:22:17 -04:00
|
|
|
template class Vector2<float>;
|
2021-06-17 22:17:50 -03:00
|
|
|
template class Vector2<double>;
|
2018-08-28 09:42:40 -03:00
|
|
|
|
2021-01-09 01:22:17 -04:00
|
|
|
// define some ops for int and long
|
2018-01-05 19:37:30 -04:00
|
|
|
template bool Vector2<long>::operator ==(const Vector2<long> &v) const;
|
2021-01-09 01:22:17 -04:00
|
|
|
template bool Vector2<long>::operator !=(const Vector2<long> &v) const;
|
2016-12-06 11:45:11 -04:00
|
|
|
template bool Vector2<int>::operator ==(const Vector2<int> &v) const;
|
2019-04-27 19:50:25 -03:00
|
|
|
template bool Vector2<int>::operator !=(const Vector2<int> &v) const;
|
2021-05-04 08:12:23 -03:00
|
|
|
|