Source: ../../extras/math/math.h
|
|
|
|
// Copyright (C) 1999-2000 Open Source Telecom Corporation.
//
// This program 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 2 of the License, or
// (at your option) any later version.
//
// This program 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, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
//
// As a special exception to the GNU General Public License, permission is
// granted for additional uses of the text contained in its release
// of Common C++.
//
// The exception is that, if you link the Common C++ library with other
// files to produce an executable, this does not by itself cause the
// resulting executable to be covered by the GNU General Public License.
// Your use of that executable is in no way restricted on account of
// linking the Common C++ library code into it.
//
// This exception does not however invalidate any other reasons why
// the executable file might be covered by the GNU General Public License.
//
// This exception applies only to the code released under the
// name Common C++. If you copy code from other releases into a copy of
// Common C++, as the General Public License permits, the exception does
// not apply to the code that you add in this way. To avoid misleading
// anyone as to the status of such modified files, you must delete
// this exception notice from them.
//
// If you write modifications of your own for Common C++, it is your choice
// whether to permit this exception to apply to your modifications.
// If you do not wish that, delete this exception notice.
#ifndef __CCXX_MATH_H__
#define __CCXX_MATH_H__
#ifndef __CCXX_CONFIG_H__
#include <cc++/config.h>
#endif
#ifndef __CCXX_MACROS_H__
#include <cc++/macros.h>
#else
#ifdef __CCXX_NAMESPACE_H__
#include <cc++/macros.h>
#endif
#endif
#include <math.h>
/**
* MathematicalScalar
*
* This type is used by classes such as the Vector2D and Vector3D
* If you change this header, most of libMath++ and anything which
* uses it will need recompiling.
*/
typedef float MathematicalScalar;
/**
* MathematicalThreshold
*
* This is used to define when a MathematicalScalar is "As good as" 0
*/
const MathematicalScalar MathematicalThreshold = 0.001f;
/**
* MathematicalZero
*
* This is used to allow code not to worry about 0.0 or 0.0f
*/
const MathematicalScalar MathematicalZero = 0.0f;
/**
* MathematicalSqrt
*
* This define maps to the relevant sqrt in the <math.h> library
*/
#define MathematicalSqrt sqrtf
/**
* MathematicalFabs
*
* This define maps to the relevant fabs in the <math.h> library
*/
#define MathematicalFabs fabsf
/**
* This class contains all the required maths operations to construct
* and perform operations on a two dimensional vector. It includes
* operations such as the dot and cross product. Normalisation, scaling
* and various other operations.
*
* @author Daniel Silverstone
* @short 2d vector array operations
*/
class Vector2D
{
public:
/**
* Default constructor initialises X and Y to zero.
*/
Vector2D() : X(MathematicalZero), Y(MathematicalZero) { ; }
/**
* This constructor takes two MathematicalScalar's and initialises X and Y
*/
Vector2D (const MathematicalScalar _X, const MathematicalScalar _Y)
: X(_X), Y(_Y) { ; }
/**
* This constructor takes two uint32's and initialises X and Y
*/
Vector2D (const uint32 _X, const uint32 _Y)
: X((MathematicalScalar)_X), Y((MathematicalScalar)_Y) { ; }
/**
* This constructor takes two int32's and initialises X and Y
*/
Vector2D (const int32 _X, const int32 _Y)
: X((MathematicalScalar)_X), Y((MathematicalScalar)_Y) { ; }
/**
* This constructor takes two uint64's and initialises X and Y
*/
Vector2D (const uint64 _X, const uint64 _Y)
: X((MathematicalScalar)_X), Y((MathematicalScalar)_Y) { ; }
/**
* This constructor takes two int64's and initialises X and Y
*/
Vector2D (const int64 _X, const int64 _Y)
: X((MathematicalScalar)_X), Y((MathematicalScalar)_Y) { ; }
/**
* This constructor copies the values from the parameter Vector2D
*/
Vector2D ( const Vector2D & v)
: X(v.X), Y(v.Y) { ; }
/**
* The following operators allow the standard mathematical operations
* on Vector2D's
*/
void operator= (const Vector2D & v)
{
X = v.X;
Y = v.Y;
}
bool operator==(const Vector2D & v) const
{
return (X == v.X) && (Y == v.Y);
}
bool operator!=(const Vector2D & v) const
{
return (X != v.X) || (Y != v.Y);
}
void operator+=(const Vector2D & v)
{
X += v.X; Y += v.Y;
}
void operator-=(const Vector2D & v)
{
X -= v.X;
Y -= v.Y;
}
void operator*=(const MathematicalScalar v)
{
X *= v;
Y *= v;
}
void operator/=(const MathematicalScalar v)
{
X /= v;
Y /= v;
}
Vector2D operator- (void) const
{
return Vector2D(-X,-Y);
}
Vector2D operator- (const Vector2D & v) const
{
return Vector2D(X-v.X, Y-v.Y);
}
Vector2D operator+ (const Vector2D & v) const
{
return Vector2D(X+v.X, Y+v.Y);
}
Vector2D operator* (const MathematicalScalar v) const
{
return Vector2D(X*v,Y*v);
}
Vector2D operator/ (const MathematicalScalar v) const
{
return Vector2D(X/v,Y/v);
}
MathematicalScalar Length(void) const
{
return MathematicalSqrt((X*X)+(Y*Y));
}
MathematicalScalar SquareLength(void) const
{
return (X*X)+(Y*Y);
}
MathematicalScalar DotProduct(const Vector2D & v) const
{
return (X*v.X) + (Y*v.Y);
}
MathematicalScalar CrossProduct(const Vector2D & v) const
{
return (X*v.Y) - (Y*v.X);
}
MathematicalScalar DistanceTo(const Vector2D & v) const
{
MathematicalScalar dx = X - v.X;
MathematicalScalar dy = Y - v.Y;
return MathematicalSqrt((dx*dx)+(dy*dy));
}
MathematicalScalar SquareDistanceTo(const Vector2D & v) const
{
MathematicalScalar dx = X - v.X;
MathematicalScalar dy = Y - v.Y;
return ((dx*dx)+(dy*dy));
}
Vector2D Normalise(void) const
{
if ((X == MathematicalZero) && (Y == MathematicalZero))
return Vector2D();
MathematicalScalar a = 1/(X*X + Y*Y);
MathematicalScalar b = MathematicalSqrt(a);
return Vector2D(X * b, Y * b);
}
Vector2D Reflect(const Vector2D & v) const
{
MathematicalScalar t = (DotProduct(v) + DotProduct(v)) / v.SquareLength();
return Vector2D((v.X * t) - X, (v.Y * t) - Y);
}
bool CheckNearlyZero() const
{
return ((MathematicalFabs(X) < MathematicalThreshold) &&
(MathematicalFabs(Y) < MathematicalThreshold));
}
/**
* X and Y are not hidden to allow rapid access.
*/
MathematicalScalar X,Y;
};
extern const Vector2D ZERO_VECTOR;
#ifdef __CCXX_NAMESPACE_H__
#undef __CCXX_NAMESPACE_H__
#include <cc++/namespace.h>
#endif
#endif
Generated by: dyfet@home.sys on Wed Jun 28 08:29:07 200. |