123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453 |
- #pragma GCC optimize("O2")
- #include "AP_Math.h"
- template <typename T>
- float Vector2<T>::length_squared() const
- {
- return (float)(x*x + y*y);
- }
- template <typename T>
- float Vector2<T>::length(void) const
- {
- return norm(x, y);
- }
- template <typename T>
- T Vector2<T>::operator *(const Vector2<T> &v) const
- {
- return x*v.x + y*v.y;
- }
- template <typename T>
- T Vector2<T>::operator %(const Vector2<T> &v) const
- {
- return x*v.y - y*v.x;
- }
- 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
- {
- return (is_equal(x,v.x) && is_equal(y,v.y));
- }
- template <typename T>
- bool Vector2<T>::operator !=(const Vector2<T> &v) const
- {
- return (!is_equal(x,v.x) || !is_equal(y,v.y));
- }
- template <typename T>
- float Vector2<T>::angle(const Vector2<T> &v2) const
- {
- const float len = this->length() * v2.length();
- if (len <= 0) {
- return 0.0f;
- }
- const float cosv = ((*this)*v2) / len;
- if (cosv >= 1) {
- return 0.0f;
- }
- if (cosv <= -1) {
- return M_PI;
- }
- return acosf(cosv);
- }
- template <typename T>
- float Vector2<T>::angle(void) const
- {
- return M_PI_2 + atan2f(-x, y);
- }
- 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)
- {
-
- 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;
- const float r1xr2 = r1 % r2;
- const float q_pxr = ss2_ss1 % r1;
- if (fabsf(r1xr2) < FLT_EPSILON) {
-
- return false;
- } else {
-
-
- const float t = (ss2_ss1 % r2) / r1xr2;
- const float u = q_pxr / r1xr2;
- if ((u >= 0) && (u <= 1) && (t >= 0) && (t <= 1)) {
-
-
-
- intersection = seg1_start + (r1*t);
- return true;
- } else {
-
- return false;
- }
- }
- }
- template <typename T>
- bool Vector2<T>::circle_segment_intersection(const Vector2<T>& seg_start, const Vector2<T>& seg_end, const Vector2<T>& circle_center, float radius, Vector2<T>& intersection)
- {
-
- const Vector2f seg_start_local = seg_start - circle_center;
-
- const Vector2f seg_end_minus_start = seg_end - seg_start;
- const float a = sq(seg_end_minus_start.x) + sq(seg_end_minus_start.y);
- const float b = 2 * ((seg_end_minus_start.x * seg_start_local.x) + (seg_end_minus_start.y * seg_start_local.y));
- const float c = sq(seg_start_local.x) + sq(seg_start_local.y) - sq(radius);
- const float delta = sq(b) - (4.0f * a * c);
-
- if (::is_zero(a)) {
- return false;
- }
- if (isnan(a) || isnan(b) || isnan(c) || isnan(delta)) {
- return false;
- }
-
- if (delta < 0.0f) {
- return false;
- }
- const float delta_sqrt = sqrtf(delta);
- const float t1 = (-b + delta_sqrt) / (2.0f * a);
- const float t2 = (-b - delta_sqrt) / (2.0f * a);
-
-
-
-
-
-
-
-
-
- if ((t1 >= 0.0f) && (t1 <= 1.0f)) {
-
-
- intersection = seg_start + (seg_end_minus_start * t1);
- return true;
- }
-
- if ((t2 >= 0.0f) && (t2 <= 1.0f)) {
-
- intersection = seg_start + (seg_end_minus_start * t2);
- return true;
- }
-
- return false;
- }
- template <typename T>
- void Vector2<T>::normalize()
- {
- *this /= length();
- }
- template <typename T>
- Vector2<T> Vector2<T>::normalized() const
- {
- return *this/length();
- }
- template <typename T>
- void Vector2<T>::reflect(const Vector2<T> &n)
- {
- const Vector2<T> orig(*this);
- project(n);
- *this = *this*2 - orig;
- }
- template <typename T>
- void Vector2<T>::project(const Vector2<T> &v)
- {
- *this= v * (*this * v)/(v*v);
- }
- template <typename T>
- Vector2<T> Vector2<T>::projected(const Vector2<T> &v)
- {
- return v * (*this * v)/(v*v);
- }
- 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;
- }
- template <typename T>
- Vector2<T> Vector2<T>::closest_point(const Vector2<T> &p, const Vector2<T> &v, const Vector2<T> &w)
- {
-
- const float l2 = (v - w).length_squared();
- if (l2 < FLT_EPSILON) {
-
- return v;
- }
-
-
-
-
- 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;
- }
- }
- template <typename T>
- Vector2<T> Vector2<T>::closest_point(const Vector2<T> &p, const Vector2<T> &w)
- {
-
- const float l2 = w.length_squared();
- if (l2 < FLT_EPSILON) {
-
- return w;
- }
- const float t = (p * w) / l2;
- if (t <= 0) {
- return Vector2<T>(0,0);
- } else if (t >= 1) {
- return w;
- } else {
- return w*t;
- }
- }
- template <typename T>
- float Vector2<T>::closest_distance_between_line_and_point_squared(const Vector2<T> &w1,
- const Vector2<T> &w2,
- const Vector2<T> &p)
- {
- return closest_distance_between_radial_and_point_squared(w2-w1, p-w1);
- }
- template <typename T>
- float Vector2<T>::closest_distance_between_line_and_point(const Vector2<T> &w1,
- const Vector2<T> &w2,
- const Vector2<T> &p)
- {
- return sqrtf(closest_distance_between_line_and_point_squared(w1, w2, p));
- }
- template <typename T>
- float Vector2<T>::closest_distance_between_lines_squared(const Vector2<T> &a1,
- const Vector2<T> &a2,
- const Vector2<T> &b1,
- const Vector2<T> &b2)
- {
- const float dist1 = Vector2<T>::closest_distance_between_line_and_point_squared(b1,b2,a1);
- const float dist2 = Vector2<T>::closest_distance_between_line_and_point_squared(b1,b2,a2);
- const float dist3 = Vector2<T>::closest_distance_between_line_and_point_squared(a1,a2,b1);
- const float dist4 = Vector2<T>::closest_distance_between_line_and_point_squared(a1,a2,b2);
- const float m1 = MIN(dist1,dist2);
- const float m2 = MIN(dist3,dist4);
- return MIN(m1,m2);
- }
- template <typename T>
- float Vector2<T>::closest_distance_between_radial_and_point_squared(const Vector2<T> &w,
- const Vector2<T> &p)
- {
- const Vector2<T> closest = closest_point(p, w);
- return (closest - p).length_squared();
- }
- template <typename T>
- float Vector2<T>::closest_distance_between_radial_and_point(const Vector2<T> &w,
- const Vector2<T> &p)
- {
- return sqrtf(closest_distance_between_radial_and_point_squared(w,p));
- }
- template float Vector2<float>::length_squared(void) const;
- template float Vector2<float>::length(void) const;
- template Vector2<float> Vector2<float>::normalized() const;
- template void Vector2<float>::normalize();
- template float Vector2<float>::operator *(const Vector2<float> &v) const;
- template float Vector2<float>::operator %(const Vector2<float> &v) const;
- template Vector2<float> &Vector2<float>::operator *=(const float num);
- template Vector2<float> &Vector2<float>::operator /=(const float num);
- template Vector2<float> &Vector2<float>::operator -=(const Vector2<float> &v);
- template Vector2<float> &Vector2<float>::operator +=(const Vector2<float> &v);
- template Vector2<float> Vector2<float>::operator /(const float num) const;
- template Vector2<float> Vector2<float>::operator *(const float num) const;
- template Vector2<float> Vector2<float>::operator +(const Vector2<float> &v) const;
- template Vector2<float> Vector2<float>::operator -(const Vector2<float> &v) const;
- template Vector2<float> Vector2<float>::operator -(void) const;
- template bool Vector2<float>::operator ==(const Vector2<float> &v) const;
- template bool Vector2<float>::operator !=(const Vector2<float> &v) const;
- template bool Vector2<float>::is_nan(void) const;
- template bool Vector2<float>::is_inf(void) const;
- template float Vector2<float>::angle(const Vector2<float> &v) const;
- template float Vector2<float>::angle(void) const;
- template bool Vector2<float>::segment_intersection(const Vector2<float>& seg1_start, const Vector2<float>& seg1_end, const Vector2<float>& seg2_start, const Vector2<float>& seg2_end, Vector2<float>& intersection);
- template bool Vector2<float>::circle_segment_intersection(const Vector2<float>& seg_start, const Vector2<float>& seg_end, const Vector2<float>& circle_center, float radius, Vector2<float>& intersection);
- template Vector2<float> Vector2<float>::perpendicular(const Vector2<float> &pos_delta, const Vector2<float> &v1);
- template Vector2<float> Vector2<float>::closest_point(const Vector2<float> &p, const Vector2<float> &v, const Vector2<float> &w);
- template float Vector2<float>::closest_distance_between_radial_and_point_squared(const Vector2<float> &w, const Vector2<float> &p);
- template float Vector2<float>::closest_distance_between_radial_and_point(const Vector2<float> &w, const Vector2<float> &p);
- template float Vector2<float>::closest_distance_between_line_and_point(const Vector2<float> &w1, const Vector2<float> &w2, const Vector2<float> &p);
- template float Vector2<float>::closest_distance_between_line_and_point_squared(const Vector2<float> &w1, const Vector2<float> &w2, const Vector2<float> &p);
- template float Vector2<float>::closest_distance_between_lines_squared(const Vector2<float> &a1,const Vector2<float> &a2,const Vector2<float> &b1,const Vector2<float> &b2);
- template void Vector2<float>::reflect(const Vector2<float> &n);
- template bool Vector2<long>::operator ==(const Vector2<long> &v) const;
- template bool Vector2<int>::operator ==(const Vector2<int> &v) const;
|