| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305 |
- /*
- Copyright (c) 2013 Daniele Bartolini, Michele Rossi
- Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
- 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.
- */
- #pragma once
- #include "Assert.h"
- #include "Types.h"
- #include "MathUtils.h"
- #include "Vec2.h"
- #include "Vec3.h"
- namespace crown
- {
- /// 2D point.
- class Point2
- {
- public:
- int32_t x, y;
- public:
- Point2(); //!< Constructor, does nothing for efficiency
- Point2(int32_t nx, int32_t ny); //! Constructs from two components
- Point2(const int32_t a[2]); //! Constructs from the array
- Point2(const Point2& a); //! Copy constructor
- ~Point2(); //! Destructor
- int32_t operator[](uint32_t i) const; //! Random access by index
- int32_t& operator[](uint32_t i); //! Random access by index
- Point2 operator+(const Point2& a) const; //! Addition
- Point2& operator+=(const Point2& a); //! Addition
- Point2 operator-(const Point2& a) const; //! Subtraction
- Point2& operator-=(const Point2& a); //! Subtraction
- Point2 operator*(int32_t k) const; //! Multiplication by scalar
- Point2& operator*=(int32_t k); //! Multiplication by scalar
- Point2 operator/(int32_t k) const; //! Division by scalar
- Point2& operator/=(int32_t k); //! Division by scalar
- int32_t dot(const Point2& a); //! dot product
- friend Point2 operator*(int32_t k, const Point2& a); //! For simmetry
- bool operator==(const Point2& other) const; //! Equality operator
- bool operator!=(const Point2& other) const; //! Disequality operator
- bool operator<(const Point2& other) const; //! Returns whether all the components of this point32_t are smaller than all of the "other" point32_t
- bool operator>(const Point2& other) const; //! Returns whether all the components of this point32_t are greater than all of the "other" point32_t
- float length() const; //! Returns the point32_t's length
- int32_t squared_length() const; //! Returns the point32_t's squared length
- void negate(); //! Negates the point32_t (i.e. builds the inverse)
- float get_distance_to(const Point2& a); //!< Returns the distance
- float get_angle_between(const Point2& a); //!< Returns the angle in radians
- Point2 operator-() const; //! Negates the point32_t (i.e. builds the inverse)
- void zero(); //! Builds the zero point32_t
- int32_t* to_int_ptr(); //! Returns the point32_ter to the point32_t's data
- const int32_t* to_int_ptr() const; //! Returns the point32_ter to the point32_t's data
- Vec2 to_vec2() const; //! Returns a vector from this point32_t
- Vec3 to_vec3() const; //! Returns a vector from this point32_t
- static const Point2 ZERO;
- static const Point2 ONE;
- static const Point2 XAXIS;
- static const Point2 YAXIS;
- };
- //-----------------------------------------------------------------------------
- inline Point2::Point2() : x(0), y(0)
- {
- }
- //-----------------------------------------------------------------------------
- inline Point2::Point2(int32_t nx, int32_t ny) : x(nx), y(ny)
- {
- }
- //-----------------------------------------------------------------------------
- inline Point2::Point2(const int32_t a[2]) : x(a[0]), y(a[1])
- {
- }
- //-----------------------------------------------------------------------------
- inline Point2::Point2(const Point2& a) : x(a.x), y(a.y)
- {
- }
- //-----------------------------------------------------------------------------
- inline Point2::~Point2()
- {
- }
- //-----------------------------------------------------------------------------
- inline int32_t Point2::operator[](uint32_t i) const
- {
- CE_ASSERT(i < 2, "Index must be < 2");
- return (&x)[i];
- }
- //-----------------------------------------------------------------------------
- inline int32_t& Point2::operator[](uint32_t i)
- {
- CE_ASSERT(i < 2, "Index must be < 2");
- return (&x)[i];
- }
- //-----------------------------------------------------------------------------
- inline Point2 Point2::operator+(const Point2& a) const
- {
- return Point2(x + a.x, y + a.y);
- }
- //-----------------------------------------------------------------------------
- inline Point2& Point2::operator+=(const Point2& a)
- {
- x += a.x;
- y += a.y;
- return *this;
- }
- //-----------------------------------------------------------------------------
- inline Point2 Point2::operator-(const Point2& a) const
- {
- return Point2(x - a.x, y - a.y);
- }
- //-----------------------------------------------------------------------------
- inline Point2& Point2::operator-=(const Point2& a)
- {
- x -= a.x;
- y -= a.y;
- return *this;
- }
- //-----------------------------------------------------------------------------
- inline Point2 Point2::operator*(int32_t k) const
- {
- return Point2(x * k, y * k);
- }
- //-----------------------------------------------------------------------------
- inline Point2& Point2::operator*=(int32_t k)
- {
- x *= k;
- y *= k;
- return *this;
- }
- //-----------------------------------------------------------------------------
- inline Point2 Point2::operator/(int32_t k) const
- {
- CE_ASSERT(k != 0, "Division by zero");
- return Point2(x / k, y / k);
- }
- //-----------------------------------------------------------------------------
- inline Point2& Point2::operator/=(int32_t k)
- {
- CE_ASSERT(k != 0, "Division by zero");
- x /= k;
- y /= k;
- return *this;
- }
- //-----------------------------------------------------------------------------
- inline int32_t Point2::dot(const Point2& a)
- {
- return x * a.x + y * a.y;
- }
- //-----------------------------------------------------------------------------
- inline Point2 operator*(int32_t k, const Point2& a)
- {
- return a * k;
- }
- //-----------------------------------------------------------------------------
- inline bool Point2::operator==(const Point2& other) const
- {
- return x == other.x && y == other.y;
- }
- //-----------------------------------------------------------------------------
- inline bool Point2::operator!=(const Point2& other) const
- {
- return x != other.x || y != other.y;
- }
- //-----------------------------------------------------------------------------
- inline bool Point2::operator<(const Point2& other) const
- {
- return ((x < other.x) && (y < other.y));
- }
- //-----------------------------------------------------------------------------
- inline bool Point2::operator>(const Point2& other) const
- {
- return ((x > other.x) && (y > other.y));
- }
- //-----------------------------------------------------------------------------
- inline float Point2::length() const
- {
- return math::acos((float)(x * x + y * y));
- }
- //-----------------------------------------------------------------------------
- inline int32_t Point2::squared_length() const
- {
- return x * x + y * y;
- }
- //-----------------------------------------------------------------------------
- inline void Point2::negate()
- {
- x = -x;
- y = -y;
- }
- //-----------------------------------------------------------------------------
- inline float Point2::get_distance_to(const Point2& a)
- {
- return (*this - a).length();
- }
- //-----------------------------------------------------------------------------
- inline float Point2::get_angle_between(const Point2& a)
- {
- return math::acos(this->dot(a) / (this->length() * a.length()));
- }
- //-----------------------------------------------------------------------------
- inline Point2 Point2::operator-() const
- {
- return Point2(-x, -y);
- }
- //-----------------------------------------------------------------------------
- inline void Point2::zero()
- {
- x = y = 0;
- }
- //-----------------------------------------------------------------------------
- inline int32_t* Point2::to_int_ptr()
- {
- return &x;
- }
- //-----------------------------------------------------------------------------
- inline const int32_t* Point2::to_int_ptr() const
- {
- return &x;
- }
- //-----------------------------------------------------------------------------
- inline Vec2 Point2::to_vec2() const
- {
- return Vec2((float)x, (float)y);
- }
- //-----------------------------------------------------------------------------
- inline Vec3 Point2::to_vec3() const
- {
- return Vec3((float)x, (float)y, 0.0);
- }
- } // namespace crown
|