| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271 |
- #include "Base.h"
- #include "Vector2.h"
- namespace gameplay
- {
- Vector2::Vector2()
- : x(0.0f), y(0.0f)
- {
- }
- Vector2::Vector2(float x, float y)
- {
- set(x, y);
- }
- Vector2::Vector2(float* array)
- {
- set(array);
- }
- Vector2::Vector2(const Vector2& p1, const Vector2& p2)
- {
- set(p1, p2);
- }
- Vector2::Vector2(const Vector2& copy)
- {
- set(copy);
- }
- Vector2::~Vector2()
- {
- }
- const Vector2& Vector2::zero()
- {
- static Vector2 value(0.0f, 0.0f);
- return value;
- }
- const Vector2& Vector2::one()
- {
- static Vector2 value(1.0f, 1.0f);
- return value;
- }
- const Vector2& Vector2::unitX()
- {
- static Vector2 value(1.0f, 0.0f);
- return value;
- }
- const Vector2& Vector2::unitY()
- {
- static Vector2 value(0.0f, 1.0f);
- return value;
- }
- bool Vector2::isZero() const
- {
- return x == 0.0f && y == 0.0f;
- }
- bool Vector2::isOne() const
- {
- return x == 1.0f && y == 1.0f;
- }
- float Vector2::angle(const Vector2& v1, const Vector2& v2)
- {
- float dz = v1.x * v2.y - v1.y * v2.x;
- return atan2f(fabsf(dz) + MATH_FLOAT_SMALL, dot(v1, v2));
- }
- void Vector2::add(const Vector2& v)
- {
- x += v.x;
- y += v.y;
- }
- void Vector2::add(const Vector2& v1, const Vector2& v2, Vector2* dst)
- {
- assert(dst);
- dst->x = v1.x + v2.x;
- dst->y = v1.y + v2.y;
- }
- void Vector2::clamp(const Vector2& min, const Vector2& max)
- {
- assert(!( min.x > max.x || min.y > max.y ));
- // Clamp the x value.
- if ( x < min.x )
- x = min.x;
- if ( x > max.x )
- x = max.x;
- // Clamp the y value.
- if ( y < min.y )
- y = min.y;
- if ( y > max.y )
- y = max.y;
- }
- void Vector2::clamp(const Vector2& v, const Vector2& min, const Vector2& max, Vector2* dst)
- {
- assert(dst);
- assert(!( min.x > max.x || min.y > max.y ));
- // Clamp the x value.
- dst->x = v.x;
- if ( dst->x < min.x )
- dst->x = min.x;
- if ( dst->x > max.x )
- dst->x = max.x;
- // Clamp the y value.
- dst->y = v.y;
- if ( dst->y < min.y )
- dst->y = min.y;
- if ( dst->y > max.y )
- dst->y = max.y;
- }
- float Vector2::distance(const Vector2& v) const
- {
- float dx = v.x - x;
- float dy = v.y - y;
- return sqrt(dx * dx + dy * dy);
- }
- float Vector2::distanceSquared(const Vector2& v) const
- {
- float dx = v.x - x;
- float dy = v.y - y;
- return (dx * dx + dy * dy);
- }
- float Vector2::dot(const Vector2& v)
- {
- return (x * v.x + y * v.y);
- }
- float Vector2::dot(const Vector2& v1, const Vector2& v2)
- {
- return (v1.x * v2.x + v1.y * v2.y);
- }
- float Vector2::length() const
- {
- return sqrt(x * x + y * y);
- }
- float Vector2::lengthSquared() const
- {
- return (x * x + y * y);
- }
- void Vector2::negate()
- {
- x = -x;
- y = -y;
- }
- void Vector2::normalize()
- {
- normalize(this);
- }
- void Vector2::normalize(Vector2* dst)
- {
- assert(dst);
- if (dst != this)
- {
- dst->x = x;
- dst->y = y;
- }
- float n = x * x + y * y;
- // Already normalized.
- if (n == 1.0f)
- return;
- n = sqrt(n);
- // Too close to zero.
- if (n < MATH_TOLERANCE)
- return;
- n = 1.0f / n;
- dst->x *= n;
- dst->y *= n;
- }
- void Vector2::scale(float scalar)
- {
- x *= scalar;
- y *= scalar;
- }
- void Vector2::scale(const Vector2& scale)
- {
- x *= scale.x;
- y *= scale.y;
- }
- void Vector2::rotate(const Vector2& point, float angle)
- {
- float sinAngle = sin(angle);
- float cosAngle = cos(angle);
- if (point.isZero())
- {
- float tempX = x * cosAngle - y * sinAngle;
- y = y * cosAngle + x * sinAngle;
- x = tempX;
- }
- else
- {
- float tempX = x - point.x;
- float tempY = y - point.y;
- x = tempX * cosAngle - tempY * sinAngle + point.x;
- y = tempY * cosAngle + tempX * sinAngle + point.y;
- }
- }
- void Vector2::set(float x, float y)
- {
- this->x = x;
- this->y = y;
- }
- void Vector2::set(float* array)
- {
- assert(array);
- x = array[0];
- y = array[1];
- }
- void Vector2::set(const Vector2& v)
- {
- this->x = v.x;
- this->y = v.y;
- }
- void Vector2::set(const Vector2& p1, const Vector2& p2)
- {
- x = p2.x - p1.x;
- y = p2.y - p1.y;
- }
- void Vector2::subtract(const Vector2& v)
- {
- x -= v.x;
- y -= v.y;
- }
- void Vector2::subtract(const Vector2& v1, const Vector2& v2, Vector2* dst)
- {
- assert(dst);
- dst->x = v1.x - v2.x;
- dst->y = v1.y - v2.y;
- }
- }
|