* cleanup (it is still used btw, at least in ServerFont)
git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@19395 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
parent
c20e9eefcd
commit
54c22262f6
@ -31,32 +31,26 @@
|
|||||||
#define ANGLE_PI 3.14159265358979323846
|
#define ANGLE_PI 3.14159265358979323846
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
bool tables_initialized=false;
|
bool sTablesInitialized = false;
|
||||||
float sintable[360], costable[360], tantable[360];
|
float sSinTable[360];
|
||||||
|
float sCosTable[360];
|
||||||
|
float sTanTable[360];
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
\brief Constructor
|
\brief Constructor
|
||||||
\param angle Value in degrees
|
\param angle Value in degrees
|
||||||
*/
|
*/
|
||||||
Angle::Angle(float angle)
|
Angle::Angle(float angle)
|
||||||
|
: fAngleValue(angle)
|
||||||
{
|
{
|
||||||
fAngleValue=angle;
|
_InitTrigTables();
|
||||||
if(tables_initialized==false)
|
|
||||||
{
|
|
||||||
InitTrigTables();
|
|
||||||
tables_initialized=true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//! Constructor
|
//! Constructor
|
||||||
Angle::Angle()
|
Angle::Angle()
|
||||||
|
: fAngleValue(0)
|
||||||
{
|
{
|
||||||
fAngleValue=0;
|
_InitTrigTables();
|
||||||
if(tables_initialized==false)
|
|
||||||
{
|
|
||||||
InitTrigTables();
|
|
||||||
tables_initialized=true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//! Empty destructor
|
//! Empty destructor
|
||||||
@ -65,28 +59,24 @@ Angle::~Angle()
|
|||||||
}
|
}
|
||||||
|
|
||||||
//! Constrains angle to 0 <= angle <= 360
|
//! Constrains angle to 0 <= angle <= 360
|
||||||
void Angle::Normalize(void)
|
void
|
||||||
|
Angle::Normalize()
|
||||||
{
|
{
|
||||||
// if the value of the angle is >=360 or <0, make it so that it is
|
// if the value of the angle is >=360 or <0, make it so that it is
|
||||||
// within those bounds
|
// within those bounds
|
||||||
|
fAngleValue = fmodf(fAngleValue, 360);
|
||||||
if (fAngleValue > 360) {
|
if (fAngleValue < 0)
|
||||||
while (fAngleValue > 360)
|
|
||||||
fAngleValue -= 360;
|
|
||||||
return;
|
|
||||||
} else if (fAngleValue < 0) {
|
|
||||||
while (fAngleValue < 0)
|
|
||||||
fAngleValue += 360;
|
fAngleValue += 360;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
\brief Obtains the sine of the angle
|
\brief Obtains the sine of the angle
|
||||||
\return The sine of the angle
|
\return The sine of the angle
|
||||||
*/
|
*/
|
||||||
float Angle::Sine(void)
|
float
|
||||||
|
Angle::Sine()
|
||||||
{
|
{
|
||||||
return sintable[(int)fAngleValue];
|
return sSinTable[(int)fAngleValue];
|
||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
@ -94,25 +84,25 @@ float Angle::Sine(void)
|
|||||||
\param value Number between 0 and 1 inclusive
|
\param value Number between 0 and 1 inclusive
|
||||||
\return The angle obtained or 0 if value passed was invalid
|
\return The angle obtained or 0 if value passed was invalid
|
||||||
*/
|
*/
|
||||||
Angle Angle::InvSine(float value)
|
Angle
|
||||||
|
Angle::InvSine(float value)
|
||||||
{
|
{
|
||||||
// Returns the inverse sine of a value in the range 0 <= value <= 1 via
|
// Returns the inverse sine of a value in the range 0 <= value <= 1 via
|
||||||
// reverse-lookup any value out of range causes the function to return 0
|
// reverse-lookup any value out of range causes the function to return 0
|
||||||
uint16 i=90;
|
|
||||||
|
|
||||||
// Filter out bad values
|
// Filter out bad values
|
||||||
if(value<0)
|
value = fabs(value);
|
||||||
value*=-1;
|
|
||||||
|
|
||||||
if (value > 1)
|
if (value > 1)
|
||||||
return Angle(0);
|
return Angle(0);
|
||||||
|
|
||||||
while(value < sintable[i])
|
uint16 i = 90;
|
||||||
|
while (value < sSinTable[i])
|
||||||
i--;
|
i--;
|
||||||
|
|
||||||
// current sintable[i] is less than value. Pick the degree value which is closer
|
// current sSinTable[i] is less than value. Pick the degree value which is closer
|
||||||
// to the passed value
|
// to the passed value
|
||||||
if( (value - sintable[i]) > (sintable[i+1] - value) )
|
if ((value - sSinTable[i]) > (sSinTable[i + 1] - value))
|
||||||
return Angle(i + 1);
|
return Angle(i + 1);
|
||||||
|
|
||||||
return Angle(i); // value is closer to previous
|
return Angle(i); // value is closer to previous
|
||||||
@ -123,9 +113,10 @@ Angle Angle::InvSine(float value)
|
|||||||
\brief Obtains the cosine of the angle
|
\brief Obtains the cosine of the angle
|
||||||
\return The cosine of the angle
|
\return The cosine of the angle
|
||||||
*/
|
*/
|
||||||
float Angle::Cosine(void)
|
float
|
||||||
|
Angle::Cosine(void)
|
||||||
{
|
{
|
||||||
return costable[(int)fAngleValue];
|
return sCosTable[(int)fAngleValue];
|
||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
@ -133,25 +124,25 @@ float Angle::Cosine(void)
|
|||||||
\param value Number between 0 and 1 inclusive
|
\param value Number between 0 and 1 inclusive
|
||||||
\return The angle obtained or 0 if value passed was invalid
|
\return The angle obtained or 0 if value passed was invalid
|
||||||
*/
|
*/
|
||||||
Angle Angle::InvCosine(float value)
|
Angle
|
||||||
|
Angle::InvCosine(float value)
|
||||||
{
|
{
|
||||||
// Returns the inverse cosine of a value in the range 0 <= value <= 1 via
|
// Returns the inverse cosine of a value in the range 0 <= value <= 1 via
|
||||||
// reverse-lookup any value out of range causes the function to return 0
|
// reverse-lookup any value out of range causes the function to return 0
|
||||||
uint16 i=90;
|
|
||||||
|
|
||||||
// Filter out bad values
|
// Filter out bad values
|
||||||
if(value<0)
|
value = fabs(value);
|
||||||
value*=-1;
|
|
||||||
|
|
||||||
if (value > 1)
|
if (value > 1)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
while(value > costable[i])
|
uint16 i = 90;
|
||||||
|
while (value > sCosTable[i])
|
||||||
i--;
|
i--;
|
||||||
|
|
||||||
// current costable[i] is less than value. Pick the degree value which is closer
|
// current sCosTable[i] is less than value. Pick the degree value which is closer
|
||||||
// to the passed value
|
// to the passed value
|
||||||
if( (value - costable[i]) < (costable[i+1] - value) )
|
if ((value - sCosTable[i]) < (sCosTable[i + 1] - value))
|
||||||
return Angle(i + 1);
|
return Angle(i + 1);
|
||||||
|
|
||||||
return Angle(i); // value is closer to previous
|
return Angle(i); // value is closer to previous
|
||||||
@ -161,16 +152,16 @@ Angle Angle::InvCosine(float value)
|
|||||||
\brief Obtains the tangent of the angle
|
\brief Obtains the tangent of the angle
|
||||||
\return The tangent of the angle
|
\return The tangent of the angle
|
||||||
*/
|
*/
|
||||||
float Angle::Tangent(int *status)
|
float
|
||||||
{
|
Angle::Tangent(int *status)
|
||||||
if(fAngleValue==90 || fAngleValue==270)
|
|
||||||
{
|
{
|
||||||
|
if (fAngleValue == 90 || fAngleValue == 270) {
|
||||||
if (status)
|
if (status)
|
||||||
*status = 0;
|
*status = 0;
|
||||||
return 0.0;
|
return 0.0;
|
||||||
}
|
}
|
||||||
|
|
||||||
return tantable[(int)fAngleValue];
|
return sTanTable[(int)fAngleValue];
|
||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
@ -178,21 +169,20 @@ float Angle::Tangent(int *status)
|
|||||||
\param value Number between 0 and 1 inclusive
|
\param value Number between 0 and 1 inclusive
|
||||||
\return The angle found or 0 if value was invalid
|
\return The angle found or 0 if value was invalid
|
||||||
*/
|
*/
|
||||||
Angle Angle::InvTangent(float value)
|
Angle
|
||||||
|
Angle::InvTangent(float value)
|
||||||
{
|
{
|
||||||
uint16 i=90;
|
|
||||||
|
|
||||||
// Filter out bad values
|
// Filter out bad values
|
||||||
if(value<0)
|
value = fabs(value);
|
||||||
value*=-1;
|
|
||||||
|
|
||||||
if (value > 1)
|
if (value > 1)
|
||||||
return Angle(0);
|
return Angle(0);
|
||||||
|
|
||||||
while(value > tantable[i])
|
uint16 i = 90;
|
||||||
|
while (value > sTanTable[i])
|
||||||
i--;
|
i--;
|
||||||
|
|
||||||
if( (value - tantable[i]) < (tantable[i+1] - value) )
|
if( (value - sTanTable[i]) < (sTanTable[i+1] - value) )
|
||||||
return Angle(i+1);
|
return Angle(i+1);
|
||||||
|
|
||||||
return Angle(i); // value is closer to previous
|
return Angle(i); // value is closer to previous
|
||||||
@ -206,7 +196,8 @@ Angle Angle::InvTangent(float value)
|
|||||||
- \c 3: 180 <= angle < 270
|
- \c 3: 180 <= angle < 270
|
||||||
- \c 4: 270 <= angle < 360
|
- \c 4: 270 <= angle < 360
|
||||||
*/
|
*/
|
||||||
uint8 Angle::Quadrant(void)
|
uint8
|
||||||
|
Angle::Quadrant()
|
||||||
{
|
{
|
||||||
// We can get away with not doing extra value checks because of the order in
|
// We can get away with not doing extra value checks because of the order in
|
||||||
// which the checks are done.
|
// which the checks are done.
|
||||||
@ -226,189 +217,159 @@ uint8 Angle::Quadrant(void)
|
|||||||
\brief Obtains the angle constrained to between 0 and 180 inclusive
|
\brief Obtains the angle constrained to between 0 and 180 inclusive
|
||||||
\return The constrained value
|
\return The constrained value
|
||||||
*/
|
*/
|
||||||
Angle Angle::Constrain180(void)
|
Angle
|
||||||
|
Angle::Constrain180()
|
||||||
{
|
{
|
||||||
// Constrains angle to 0 <= angle < 180
|
// Constrains angle to 0 <= angle < 180
|
||||||
float val=fAngleValue;
|
|
||||||
|
|
||||||
if (fAngleValue < 180)
|
if (fAngleValue < 180)
|
||||||
return Angle(fAngleValue);
|
return Angle(fAngleValue);
|
||||||
|
|
||||||
while(!(val<180))
|
float value = fmodf(fAngleValue, 180);;
|
||||||
val-=90;
|
if (value < 0)
|
||||||
return Angle(val);
|
value += 180;
|
||||||
|
return Angle(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
\brief Obtains the angle constrained to between 0 and 90 inclusive
|
\brief Obtains the angle constrained to between 0 and 90 inclusive
|
||||||
\return The constrained value
|
\return The constrained value
|
||||||
*/
|
*/
|
||||||
Angle Angle::Constrain90(void)
|
Angle
|
||||||
|
Angle::Constrain90()
|
||||||
{
|
{
|
||||||
// Constrains angle to 0 <= angle < 90
|
// Constrains angle to 0 <= angle < 90
|
||||||
float val=fAngleValue;
|
|
||||||
|
|
||||||
if (fAngleValue < 90)
|
if (fAngleValue < 90)
|
||||||
return Angle(fAngleValue);
|
return Angle(fAngleValue);
|
||||||
|
|
||||||
while(!(val<90))
|
float value = fmodf(fAngleValue, 90);;
|
||||||
val-=90;
|
if (value < 0)
|
||||||
return Angle(val);
|
value += 90;
|
||||||
|
return Angle(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
\brief Sets the angle's value and normalizes the value
|
\brief Sets the angle's value and normalizes the value
|
||||||
\param angle Value in degrees
|
\param angle Value in degrees
|
||||||
*/
|
*/
|
||||||
void Angle::SetValue(float angle)
|
void
|
||||||
|
Angle::SetValue(float angle)
|
||||||
{
|
{
|
||||||
fAngleValue = angle;
|
fAngleValue = angle;
|
||||||
Normalize();
|
Normalize();
|
||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
|
||||||
\brief Returns the value of the angle
|
float
|
||||||
\return The angle's value in degrees
|
Angle::Value() const
|
||||||
*/
|
|
||||||
float Angle::Value(void) const
|
|
||||||
{
|
{
|
||||||
return fAngleValue;
|
return fAngleValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
//! Initializes the global trig tables
|
//! Initializes the global trig tables
|
||||||
void Angle::InitTrigTables(void)
|
void
|
||||||
|
Angle::_InitTrigTables()
|
||||||
{
|
{
|
||||||
int8 i;
|
if (sTablesInitialized)
|
||||||
double sval,cval,tval,current_radian;
|
return;
|
||||||
|
sTablesInitialized = true;
|
||||||
|
|
||||||
for(i=0;i<90; i++)
|
for(int32 i = 0; i < 90; i++) {
|
||||||
{
|
double currentRadian = (i * ANGLE_PI) / 180.0;
|
||||||
current_radian=(i * ANGLE_PI)/180.0;
|
|
||||||
|
|
||||||
// Get these so that we can do some superfast assignments
|
// Get these so that we can do some superfast assignments
|
||||||
sval=(float)sin(current_radian);
|
double sinValue = sin(currentRadian);
|
||||||
cval=(float)cos(current_radian);
|
double cosValue = cos(currentRadian);
|
||||||
|
|
||||||
// Do 4 assignments, taking advantage of sin/cos symmetry
|
// Do 4 assignments, taking advantage of sin/cos symmetry
|
||||||
sintable[i]=sval;
|
sSinTable[i] = sinValue;
|
||||||
sintable[i+90]=cval;
|
sSinTable[i + 90] = cosValue;
|
||||||
sintable[i+180]=sval * -1;
|
sSinTable[i + 180] = sinValue * -1;
|
||||||
sintable[i+270]=cval * -1;
|
sSinTable[i + 270] = cosValue * -1;
|
||||||
|
|
||||||
costable[i]=cval;
|
sCosTable[i] = cosValue;
|
||||||
costable[i+90]=sval * -1;
|
sCosTable[i + 90] = sinValue * -1;
|
||||||
costable[i+180]=cval * -1;
|
sCosTable[i + 180] = cosValue * -1;
|
||||||
costable[i+270]=sval;
|
sCosTable[i + 270] = sinValue;
|
||||||
|
|
||||||
tval=sval/cval;
|
double tanValue = sinValue / cosValue;
|
||||||
|
|
||||||
tantable[i]=tval;
|
sTanTable[i] = tanValue;
|
||||||
tantable[i+90]=tval;
|
sTanTable[i + 90] = tanValue;
|
||||||
tantable[i+180]=tval;
|
sTanTable[i + 180] = tanValue;
|
||||||
tantable[i+270]=tval;
|
sTanTable[i + 270] = tanValue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
|
||||||
\brief Overloaded assignment operator
|
Angle&
|
||||||
\param from Angle to copy the value from
|
Angle::operator=(const Angle &from)
|
||||||
\return The angle's new value
|
|
||||||
*/
|
|
||||||
Angle & Angle::operator=(const Angle &from)
|
|
||||||
{
|
{
|
||||||
fAngleValue = from.fAngleValue;
|
fAngleValue = from.fAngleValue;
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
|
||||||
\brief Overloaded assignment operator
|
Angle&
|
||||||
\param from New value of the angle
|
Angle::operator=(const float &from)
|
||||||
\return The angle's new value
|
|
||||||
*/
|
|
||||||
Angle & Angle::operator=(const float &from)
|
|
||||||
{
|
{
|
||||||
fAngleValue = from;
|
fAngleValue = from;
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
|
||||||
\brief Overloaded assignment operator
|
Angle&
|
||||||
\param from New value of the angle
|
Angle::operator=(const long &from)
|
||||||
\return The angle's new value
|
|
||||||
*/
|
|
||||||
Angle & Angle::operator=(const long &from)
|
|
||||||
{
|
{
|
||||||
fAngleValue = (float)from;
|
fAngleValue = (float)from;
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
|
||||||
\brief Overloaded assignment operator
|
Angle&
|
||||||
\param from New value of the angle
|
Angle::operator=(const int &from)
|
||||||
\return The angle's new value
|
|
||||||
*/
|
|
||||||
Angle & Angle::operator=(const int &from)
|
|
||||||
{
|
{
|
||||||
fAngleValue = (float)from;
|
fAngleValue = (float)from;
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
|
||||||
\brief Overloaded equivalence operator
|
bool
|
||||||
\param The angle to compare to
|
Angle::operator==(const Angle &from)
|
||||||
\return True if equal, false if not
|
|
||||||
*/
|
|
||||||
bool Angle::operator==(const Angle &from)
|
|
||||||
{
|
{
|
||||||
return (fAngleValue==from.fAngleValue)?true:false;
|
return (fAngleValue == from.fAngleValue);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
|
||||||
\brief Overloaded inequality operator
|
bool
|
||||||
\param The angle to compare to
|
Angle::operator!=(const Angle &from)
|
||||||
\return True if not equal, false if not
|
|
||||||
*/
|
|
||||||
bool Angle::operator!=(const Angle &from)
|
|
||||||
{
|
{
|
||||||
return (fAngleValue!=from.fAngleValue)?true:false;
|
return (fAngleValue != from.fAngleValue);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
|
||||||
\brief Overloaded greater than operator
|
bool
|
||||||
\param The angle to compare to
|
Angle::operator>(const Angle &from)
|
||||||
\return True if greater, false if not
|
|
||||||
*/
|
|
||||||
bool Angle::operator>(const Angle &from)
|
|
||||||
{
|
{
|
||||||
return (fAngleValue>from.fAngleValue)?true:false;
|
return (fAngleValue > from.fAngleValue);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
|
||||||
\brief Overloaded less than operator
|
bool
|
||||||
\param The angle to compare to
|
Angle::operator<(const Angle &from)
|
||||||
\return True if less than, false if not
|
|
||||||
*/
|
|
||||||
bool Angle::operator<(const Angle &from)
|
|
||||||
{
|
{
|
||||||
return (fAngleValue<from.fAngleValue)?true:false;
|
return (fAngleValue < from.fAngleValue);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
|
||||||
\brief Overloaded greater than or equal to operator
|
bool
|
||||||
\param The angle to compare to
|
Angle::operator>=(const Angle &from)
|
||||||
\return True if greater than or equal to, false if not
|
|
||||||
*/
|
|
||||||
bool Angle::operator>=(const Angle &from)
|
|
||||||
{
|
{
|
||||||
return (fAngleValue>=from.fAngleValue)?true:false;
|
return (fAngleValue >= from.fAngleValue);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
|
||||||
\brief Overloaded less than or equal to operator
|
bool
|
||||||
\param The angle to compare to
|
Angle::operator<=(const Angle &from)
|
||||||
\return True if less than or equal to, false if not
|
|
||||||
*/
|
|
||||||
bool Angle::operator<=(const Angle &from)
|
|
||||||
{
|
{
|
||||||
return (fAngleValue<=from.fAngleValue)?true:false;
|
return (fAngleValue <= from.fAngleValue);
|
||||||
}
|
}
|
||||||
|
@ -70,8 +70,9 @@ virtual ~Angle();
|
|||||||
bool operator<=(const Angle &from);
|
bool operator<=(const Angle &from);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
void _InitTrigTables(void);
|
||||||
|
|
||||||
float fAngleValue;
|
float fAngleValue;
|
||||||
void InitTrigTables(void);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
Reference in New Issue
Block a user