Loading...
Searching...
No Matches
vector2d.h
Go to the documentation of this file.
// Copyright (C) 2002-2012 Nikolaus Gebhardt
// This file is part of the "Irrlicht Engine".
// For conditions of distribution and use, see copyright notice in irrlicht.h
#ifndef __IRR_POINT_2D_H_INCLUDED__
#define __IRR_POINT_2D_H_INCLUDED__
#include "irrMath.h"
#include "dimension2d.h"
namespace irr
{
namespace core
{
template <class T>
class vector2d
{
public:
vector2d() : X(0), Y(0) {}
vector2d(T nx, T ny) : X(nx), Y(ny) {}
explicit vector2d(T n) : X(n), Y(n) {}
vector2d(const vector2d<T>& other) : X(other.X), Y(other.Y) {}
vector2d(const dimension2d<T>& other) : X(other.Width), Y(other.Height) {}
// operators
vector2d<T> operator-() const { return vector2d<T>(-X, -Y); }
vector2d<T>& operator=(const vector2d<T>& other) { X = other.X; Y = other.Y; return *this; }
vector2d<T>& operator=(const dimension2d<T>& other) { X = other.Width; Y = other.Height; return *this; }
vector2d<T> operator+(const vector2d<T>& other) const { return vector2d<T>(X + other.X, Y + other.Y); }
vector2d<T> operator+(const dimension2d<T>& other) const { return vector2d<T>(X + other.Width, Y + other.Height); }
vector2d<T>& operator+=(const vector2d<T>& other) { X+=other.X; Y+=other.Y; return *this; }
vector2d<T> operator+(const T v) const { return vector2d<T>(X + v, Y + v); }
vector2d<T>& operator+=(const T v) { X+=v; Y+=v; return *this; }
vector2d<T>& operator+=(const dimension2d<T>& other) { X += other.Width; Y += other.Height; return *this; }
vector2d<T> operator-(const vector2d<T>& other) const { return vector2d<T>(X - other.X, Y - other.Y); }
vector2d<T> operator-(const dimension2d<T>& other) const { return vector2d<T>(X - other.Width, Y - other.Height); }
vector2d<T>& operator-=(const vector2d<T>& other) { X-=other.X; Y-=other.Y; return *this; }
vector2d<T> operator-(const T v) const { return vector2d<T>(X - v, Y - v); }
vector2d<T>& operator-=(const T v) { X-=v; Y-=v; return *this; }
vector2d<T>& operator-=(const dimension2d<T>& other) { X -= other.Width; Y -= other.Height; return *this; }
vector2d<T> operator*(const vector2d<T>& other) const { return vector2d<T>(X * other.X, Y * other.Y); }
vector2d<T>& operator*=(const vector2d<T>& other) { X*=other.X; Y*=other.Y; return *this; }
vector2d<T> operator*(const T v) const { return vector2d<T>(X * v, Y * v); }
vector2d<T>& operator*=(const T v) { X*=v; Y*=v; return *this; }
vector2d<T> operator/(const vector2d<T>& other) const { return vector2d<T>(X / other.X, Y / other.Y); }
vector2d<T>& operator/=(const vector2d<T>& other) { X/=other.X; Y/=other.Y; return *this; }
vector2d<T> operator/(const T v) const { return vector2d<T>(X / v, Y / v); }
vector2d<T>& operator/=(const T v) { X/=v; Y/=v; return *this; }
bool operator<=(const vector2d<T>&other) const
{
return (X<other.X || core::equals(X, other.X)) ||
(core::equals(X, other.X) && (Y<other.Y || core::equals(Y, other.Y)));
}
bool operator>=(const vector2d<T>&other) const
{
return (X>other.X || core::equals(X, other.X)) ||
(core::equals(X, other.X) && (Y>other.Y || core::equals(Y, other.Y)));
}
bool operator<(const vector2d<T>&other) const
{
return (X<other.X && !core::equals(X, other.X)) ||
(core::equals(X, other.X) && Y<other.Y && !core::equals(Y, other.Y));
}
bool operator>(const vector2d<T>&other) const
{
return (X>other.X && !core::equals(X, other.X)) ||
(core::equals(X, other.X) && Y>other.Y && !core::equals(Y, other.Y));
}
bool operator==(const vector2d<T>& other) const { return equals(other); }
bool operator!=(const vector2d<T>& other) const { return !equals(other); }
// functions
bool equals(const vector2d<T>& other) const
{
return core::equals(X, other.X) && core::equals(Y, other.Y);
}
vector2d<T>& set(T nx, T ny) {X=nx; Y=ny; return *this; }
vector2d<T>& set(const vector2d<T>& p) { X=p.X; Y=p.Y; return *this; }
T getLength() const { return core::squareroot( X*X + Y*Y ); }
T getLengthSQ() const { return X*X + Y*Y; }
T dotProduct(const vector2d<T>& other) const
{
return X*other.X + Y*other.Y;
}
T getDistanceFrom(const vector2d<T>& other) const
{
return vector2d<T>(X - other.X, Y - other.Y).getLength();
}
T getDistanceFromSQ(const vector2d<T>& other) const
{
return vector2d<T>(X - other.X, Y - other.Y).getLengthSQ();
}
vector2d<T>& rotateBy(f64 degrees, const vector2d<T>& center=vector2d<T>())
{
degrees *= DEGTORAD64;
const f64 cs = cos(degrees);
const f64 sn = sin(degrees);
X -= center.X;
Y -= center.Y;
set((T)(X*cs - Y*sn), (T)(X*sn + Y*cs));
X += center.X;
Y += center.Y;
return *this;
}
vector2d<T>& normalize()
{
f32 length = (f32)(X*X + Y*Y);
if ( length == 0 )
return *this;
length = core::reciprocal_squareroot ( length );
X = (T)(X * length);
Y = (T)(Y * length);
return *this;
}
f64 getAngleTrig() const
{
if (Y == 0)
return X < 0 ? 180 : 0;
else
if (X == 0)
return Y < 0 ? 270 : 90;
if ( Y > 0)
if (X > 0)
return atan((irr::f64)Y/(irr::f64)X) * RADTODEG64;
else
return 180.0-atan((irr::f64)Y/-(irr::f64)X) * RADTODEG64;
else
if (X > 0)
return 360.0-atan(-(irr::f64)Y/(irr::f64)X) * RADTODEG64;
else
return 180.0+atan(-(irr::f64)Y/-(irr::f64)X) * RADTODEG64;
}
inline f64 getAngle() const
{
if (Y == 0) // corrected thanks to a suggestion by Jox
return X < 0 ? 180 : 0;
else if (X == 0)
return Y < 0 ? 90 : 270;
// don't use getLength here to avoid precision loss with s32 vectors
// avoid floating-point trouble as sqrt(y*y) is occasionally larger than y, so clamp
const f64 tmp = core::clamp(Y / sqrt((f64)(X*X + Y*Y)), -1.0, 1.0);
const f64 angle = atan( core::squareroot(1 - tmp*tmp) / tmp) * RADTODEG64;
if (X>0 && Y>0)
return angle + 270;
else
if (X>0 && Y<0)
return angle + 90;
else
if (X<0 && Y<0)
return 90 - angle;
else
if (X<0 && Y>0)
return 270 - angle;
return angle;
}
inline f64 getAngleWith(const vector2d<T>& b) const
{
f64 tmp = (f64)(X*b.X + Y*b.Y);
if (tmp == 0.0)
return 90.0;
tmp = tmp / core::squareroot((f64)((X*X + Y*Y) * (b.X*b.X + b.Y*b.Y)));
if (tmp < 0.0)
tmp = -tmp;
if ( tmp > 1.0 ) // avoid floating-point trouble
tmp = 1.0;
return atan(sqrt(1 - tmp*tmp) / tmp) * RADTODEG64;
}
bool isBetweenPoints(const vector2d<T>& begin, const vector2d<T>& end) const
{
if (begin.X != end.X)
{
return ((begin.X <= X && X <= end.X) ||
(begin.X >= X && X >= end.X));
}
else
{
return ((begin.Y <= Y && Y <= end.Y) ||
(begin.Y >= Y && Y >= end.Y));
}
}
vector2d<T> getInterpolated(const vector2d<T>& other, f64 d) const
{
f64 inv = 1.0f - d;
return vector2d<T>((T)(other.X*inv + X*d), (T)(other.Y*inv + Y*d));
}
vector2d<T> getInterpolated_quadratic(const vector2d<T>& v2, const vector2d<T>& v3, f64 d) const
{
// this*(1-d)*(1-d) + 2 * v2 * (1-d) + v3 * d * d;
const f64 inv = 1.0f - d;
const f64 mul0 = inv * inv;
const f64 mul1 = 2.0f * d * inv;
const f64 mul2 = d * d;
return vector2d<T> ( (T)(X * mul0 + v2.X * mul1 + v3.X * mul2),
(T)(Y * mul0 + v2.Y * mul1 + v3.Y * mul2));
}
vector2d<T>& interpolate(const vector2d<T>& a, const vector2d<T>& b, f64 d)
{
X = (T)((f64)b.X + ( ( a.X - b.X ) * d ));
Y = (T)((f64)b.Y + ( ( a.Y - b.Y ) * d ));
return *this;
}
T X;
T Y;
};
typedef vector2d<f32> vector2df;
typedef vector2d<s32> vector2di;
template<class S, class T>
vector2d<T> operator*(const S scalar, const vector2d<T>& vector) { return vector*scalar; }
// These methods are declared in dimension2d, but need definitions of vector2d
template<class T>
dimension2d<T>::dimension2d(const vector2d<T>& other) : Width(other.X), Height(other.Y) { }
template<class T>
bool dimension2d<T>::operator==(const vector2d<T>& other) const { return Width == other.X && Height == other.Y; }
} // end namespace core
} // end namespace irr
#endif
irr::core::dimension2d
Specifies a 2 dimensional size.
Definition dimension2d.h:21
irr::core::dimension2d::Width
T Width
Width of the dimension.
Definition dimension2d.h:204
irr::core::dimension2d::Height
T Height
Height of the dimension.
Definition dimension2d.h:206
irr::core::dimension2d::operator==
bool operator==(const dimension2d< T > &other) const
Equality operator.
Definition dimension2d.h:46
irr::core::dimension2d::dimension2d
dimension2d()
Default constructor for empty dimension.
Definition dimension2d.h:24
irr::core::vector2d
2d vector template class with lots of operators and methods.
Definition vector2d.h:22
irr::core::vector2d::operator+=
vector2d< T > & operator+=(const vector2d< T > &other)
Definition vector2d.h:45
irr::core::vector2d::operator*
vector2d< T > operator*(const T v) const
Definition vector2d.h:59
irr::core::vector2d::operator-=
vector2d< T > & operator-=(const vector2d< T > &other)
Definition vector2d.h:52
irr::core::vector2d::operator-
vector2d< T > operator-() const
Definition vector2d.h:37
irr::core::vector2d::operator<
bool operator<(const vector2d< T > &other) const
sort in order X, Y. Difference must be above rounding tolerance.
Definition vector2d.h:82
irr::core::vector2d::operator-
vector2d< T > operator-(const vector2d< T > &other) const
Definition vector2d.h:50
irr::core::vector2d::interpolate
vector2d< T > & interpolate(const vector2d< T > &a, const vector2d< T > &b, f64 d)
Sets this vector to the linearly interpolated vector between a and b.
Definition vector2d.h:308
irr::core::vector2d::operator-=
vector2d< T > & operator-=(const dimension2d< T > &other)
Definition vector2d.h:55
irr::core::vector2d::operator+
vector2d< T > operator+(const dimension2d< T > &other) const
Definition vector2d.h:44
irr::core::vector2d::operator+
vector2d< T > operator+(const vector2d< T > &other) const
Definition vector2d.h:43
irr::core::vector2d::getDistanceFrom
T getDistanceFrom(const vector2d< T > &other) const
Gets distance from another point.
Definition vector2d.h:133
irr::core::vector2d::operator-
vector2d< T > operator-(const dimension2d< T > &other) const
Definition vector2d.h:51
irr::core::vector2d::operator/
vector2d< T > operator/(const vector2d< T > &other) const
Definition vector2d.h:62
irr::core::vector2d::operator*=
vector2d< T > & operator*=(const T v)
Definition vector2d.h:60
irr::core::vector2d::getAngleTrig
f64 getAngleTrig() const
Calculates the angle of this vector in degrees in the trigonometric sense.
Definition vector2d.h:185
irr::core::vector2d::operator/=
vector2d< T > & operator/=(const T v)
Definition vector2d.h:65
irr::core::vector2d::operator*=
vector2d< T > & operator*=(const vector2d< T > &other)
Definition vector2d.h:58
irr::core::vector2d::operator/
vector2d< T > operator/(const T v) const
Definition vector2d.h:64
irr::core::vector2d::operator=
vector2d< T > & operator=(const vector2d< T > &other)
Definition vector2d.h:39
irr::core::vector2d::vector2d
vector2d()
Default constructor (null vector).
Definition vector2d.h:25
irr::core::vector2d::operator>
bool operator>(const vector2d< T > &other) const
sort in order X, Y. Difference must be above rounding tolerance.
Definition vector2d.h:89
irr::core::vector2d::operator+
vector2d< T > operator+(const T v) const
Definition vector2d.h:46
irr::core::vector2d::set
vector2d< T > & set(T nx, T ny)
Definition vector2d.h:109
irr::core::vector2d::operator*
vector2d< T > operator*(const vector2d< T > &other) const
Definition vector2d.h:57
irr::core::vector2d::getAngleWith
f64 getAngleWith(const vector2d< T > &b) const
Calculates the angle between this vector and another one in degree.
Definition vector2d.h:238
irr::core::vector2d::getLength
T getLength() const
Gets the length of the vector.
Definition vector2d.h:114
irr::core::vector2d::vector2d
vector2d(T n)
Constructor with the same value for both members.
Definition vector2d.h:29
irr::core::vector2d::vector2d
vector2d(const vector2d< T > &other)
Copy constructor.
Definition vector2d.h:31
irr::core::vector2d< f32 >::X
f32 X
Definition vector2d.h:316
irr::core::vector2d::getInterpolated
vector2d< T > getInterpolated(const vector2d< T > &other, f64 d) const
Creates an interpolated vector between this vector and another vector.
Definition vector2d.h:278
irr::core::vector2d::equals
bool equals(const vector2d< T > &other) const
Checks if this vector equals the other one.
Definition vector2d.h:104
irr::core::vector2d::operator=
vector2d< T > & operator=(const dimension2d< T > &other)
Definition vector2d.h:41
irr::core::vector2d< f32 >::Y
f32 Y
Definition vector2d.h:319
irr::core::vector2d::getInterpolated_quadratic
vector2d< T > getInterpolated_quadratic(const vector2d< T > &v2, const vector2d< T > &v3, f64 d) const
Creates a quadratically interpolated vector between this and two other vectors.
Definition vector2d.h:290
irr::core::vector2d::operator+=
vector2d< T > & operator+=(const dimension2d< T > &other)
Definition vector2d.h:48
irr::core::vector2d::operator!=
bool operator!=(const vector2d< T > &other) const
Definition vector2d.h:96
irr::core::vector2d::getDistanceFromSQ
T getDistanceFromSQ(const vector2d< T > &other) const
Returns squared distance from another point.
Definition vector2d.h:142
irr::core::vector2d::rotateBy
vector2d< T > & rotateBy(f64 degrees, const vector2d< T > &center=vector2d< T >())
rotates the point anticlockwise around a center by an amount of degrees.
Definition vector2d.h:151
irr::core::vector2d::operator-=
vector2d< T > & operator-=(const T v)
Definition vector2d.h:54
irr::core::vector2d::operator>=
bool operator>=(const vector2d< T > &other) const
sort in order X, Y. Equality with rounding tolerance.
Definition vector2d.h:75
irr::core::vector2d::getLengthSQ
T getLengthSQ() const
Get the squared length of this vector.
Definition vector2d.h:119
irr::core::vector2d::set
vector2d< T > & set(const vector2d< T > &p)
Definition vector2d.h:110
irr::core::vector2d::operator<=
bool operator<=(const vector2d< T > &other) const
sort in order X, Y. Equality with rounding tolerance.
Definition vector2d.h:68
irr::core::vector2d::dotProduct
T dotProduct(const vector2d< T > &other) const
Get the dot product of this vector with another.
Definition vector2d.h:124
irr::core::vector2d::vector2d
vector2d(const dimension2d< T > &other)
Definition vector2d.h:33
irr::core::vector2d::getAngle
f64 getAngle() const
Calculates the angle of this vector in degrees in the counter trigonometric sense.
Definition vector2d.h:208
irr::core::vector2d::vector2d
vector2d(T nx, T ny)
Constructor with two different values.
Definition vector2d.h:27
irr::core::vector2d::operator-
vector2d< T > operator-(const T v) const
Definition vector2d.h:53
irr::core::vector2d::operator/=
vector2d< T > & operator/=(const vector2d< T > &other)
Definition vector2d.h:63
irr::core::vector2d::normalize
vector2d< T > & normalize()
Normalize the vector.
Definition vector2d.h:170
irr::core::vector2d::isBetweenPoints
bool isBetweenPoints(const vector2d< T > &begin, const vector2d< T > &end) const
Returns if this vector interpreted as a point is on a line between two other points.
Definition vector2d.h:259
irr::core::vector2d::operator+=
vector2d< T > & operator+=(const T v)
Definition vector2d.h:47
irr::core::vector2d::operator==
bool operator==(const vector2d< T > &other) const
Definition vector2d.h:95
dimension2d.h
irrMath.h
irr::core
Basic classes such as vectors, planes, arrays, lists, and so on can be found in this namespace.
Definition aabbox3d.h:15
irr::core::vector2df
vector2d< f32 > vector2df
Typedef for f32 2d vector.
Definition vector2d.h:323
irr::core::operator*
CMatrix4< T > operator*(const T scalar, const CMatrix4< T > &mat)
Definition matrix4.h:2228
irr::core::clamp
const T clamp(const T &value, const T &low, const T &high)
clamps a value between low and high
Definition irrMath.h:166
irr::core::DEGTORAD64
const f64 DEGTORAD64
64bit constant for converting from degrees to radians (formally known as GRAD_PI2)
Definition irrMath.h:80
irr::core::vector2di
vector2d< s32 > vector2di
Typedef for integer 2d vector.
Definition vector2d.h:326
irr::core::equals
bool equals(const f64 a, const f64 b, const f64 tolerance=ROUNDING_ERROR_f64)
returns if a equals b, taking possible rounding errors into account
Definition irrMath.h:185
irr::core::reciprocal_squareroot
REALINLINE f64 reciprocal_squareroot(const f64 x)
Definition irrMath.h:497
irr::core::RADTODEG64
const f64 RADTODEG64
64bit constant for converting from radians to degrees
Definition irrMath.h:83
irr::core::squareroot
REALINLINE f32 squareroot(const f32 f)
Definition irrMath.h:471
irr
Everything in the Irrlicht Engine can be found in this namespace.
Definition aabbox3d.h:13
irr::f32
float f32
32 bit floating point variable.
Definition irrTypes.h:104
irr::f64
double f64
64 bit floating point variable.
Definition irrTypes.h:108
- Irrlicht Engine Documentation © 2003-2012 by Nikolaus Gebhardt. Generated on for Irrlicht 3D Engine by Doxygen 1.15.0