* 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:
Stephan Aßmus 2006-11-29 20:29:03 +00:00
parent c20e9eefcd
commit 54c22262f6
2 changed files with 140 additions and 178 deletions

View File

@ -31,32 +31,26 @@
#define ANGLE_PI 3.14159265358979323846
#endif
bool tables_initialized=false;
float sintable[360], costable[360], tantable[360];
bool sTablesInitialized = false;
float sSinTable[360];
float sCosTable[360];
float sTanTable[360];
/*!
\brief Constructor
\param angle Value in degrees
*/
Angle::Angle(float angle)
: fAngleValue(angle)
{
fAngleValue=angle;
if(tables_initialized==false)
{
InitTrigTables();
tables_initialized=true;
}
_InitTrigTables();
}
//! Constructor
Angle::Angle()
: fAngleValue(0)
{
fAngleValue=0;
if(tables_initialized==false)
{
InitTrigTables();
tables_initialized=true;
}
_InitTrigTables();
}
//! Empty destructor
@ -65,28 +59,24 @@ Angle::~Angle()
}
//! 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
// within those bounds
if (fAngleValue > 360) {
while (fAngleValue > 360)
fAngleValue -= 360;
return;
} else if (fAngleValue < 0) {
while (fAngleValue < 0)
fAngleValue += 360;
}
fAngleValue = fmodf(fAngleValue, 360);
if (fAngleValue < 0)
fAngleValue += 360;
}
/*!
\brief Obtains 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,26 +84,26 @@ float Angle::Sine(void)
\param value Number between 0 and 1 inclusive
\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
// reverse-lookup any value out of range causes the function to return 0
uint16 i=90;
// Filter out bad values
if(value<0)
value*=-1;
value = fabs(value);
if(value > 1)
if (value > 1)
return Angle(0);
while(value < sintable[i])
uint16 i = 90;
while (value < sSinTable[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
if( (value - sintable[i]) > (sintable[i+1] - value) )
return Angle(i+1);
if ((value - sSinTable[i]) > (sSinTable[i + 1] - value))
return Angle(i + 1);
return Angle(i); // value is closer to previous
}
@ -123,9 +113,10 @@ Angle Angle::InvSine(float value)
\brief Obtains 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,26 +124,26 @@ float Angle::Cosine(void)
\param value Number between 0 and 1 inclusive
\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
// reverse-lookup any value out of range causes the function to return 0
uint16 i=90;
// Filter out bad values
if(value<0)
value*=-1;
value = fabs(value);
if(value > 1)
if (value > 1)
return 0;
while(value > costable[i])
uint16 i = 90;
while (value > sCosTable[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
if( (value - costable[i]) < (costable[i+1] - value) )
return Angle(i+1);
if ((value - sCosTable[i]) < (sCosTable[i + 1] - value))
return Angle(i + 1);
return Angle(i); // value is closer to previous
}
@ -161,16 +152,16 @@ Angle Angle::InvCosine(float value)
\brief Obtains 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(status)
*status=0;
if (fAngleValue == 90 || fAngleValue == 270) {
if (status)
*status = 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
\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
if(value<0)
value*=-1;
value = fabs(value);
if(value > 1)
if (value > 1)
return Angle(0);
while(value > tantable[i])
uint16 i = 90;
while (value > sTanTable[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); // value is closer to previous
@ -206,17 +196,18 @@ Angle Angle::InvTangent(float value)
- \c 3: 180 <= angle < 270
- \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
// which the checks are done.
if(fAngleValue < 90)
if (fAngleValue < 90)
return 1;
if(fAngleValue < 180)
if (fAngleValue < 180)
return 2;
if(fAngleValue < 270)
if (fAngleValue < 270)
return 3;
return 4;
@ -226,189 +217,159 @@ uint8 Angle::Quadrant(void)
\brief Obtains the angle constrained to between 0 and 180 inclusive
\return The constrained value
*/
Angle Angle::Constrain180(void)
Angle
Angle::Constrain180()
{
// Constrains angle to 0 <= angle < 180
float val=fAngleValue;
if(fAngleValue<180)
if (fAngleValue < 180)
return Angle(fAngleValue);
while(!(val<180))
val-=90;
return Angle(val);
float value = fmodf(fAngleValue, 180);;
if (value < 0)
value += 180;
return Angle(value);
}
/*!
\brief Obtains the angle constrained to between 0 and 90 inclusive
\return The constrained value
*/
Angle Angle::Constrain90(void)
Angle
Angle::Constrain90()
{
// Constrains angle to 0 <= angle < 90
float val=fAngleValue;
if(fAngleValue<90)
if (fAngleValue < 90)
return Angle(fAngleValue);
while(!(val<90))
val-=90;
return Angle(val);
float value = fmodf(fAngleValue, 90);;
if (value < 0)
value += 90;
return Angle(value);
}
/*!
\brief Sets the angle's value and normalizes the value
\param angle Value in degrees
*/
void Angle::SetValue(float angle)
void
Angle::SetValue(float angle)
{
fAngleValue=angle;
fAngleValue = angle;
Normalize();
}
/*!
\brief Returns the value of the angle
\return The angle's value in degrees
*/
float Angle::Value(void) const
float
Angle::Value() const
{
return fAngleValue;
}
//! Initializes the global trig tables
void Angle::InitTrigTables(void)
void
Angle::_InitTrigTables()
{
int8 i;
double sval,cval,tval,current_radian;
for(i=0;i<90; i++)
{
current_radian=(i * ANGLE_PI)/180.0;
if (sTablesInitialized)
return;
sTablesInitialized = true;
for(int32 i = 0; i < 90; i++) {
double currentRadian = (i * ANGLE_PI) / 180.0;
// Get these so that we can do some superfast assignments
sval=(float)sin(current_radian);
cval=(float)cos(current_radian);
double sinValue = sin(currentRadian);
double cosValue = cos(currentRadian);
// Do 4 assignments, taking advantage of sin/cos symmetry
sintable[i]=sval;
sintable[i+90]=cval;
sintable[i+180]=sval * -1;
sintable[i+270]=cval * -1;
sSinTable[i] = sinValue;
sSinTable[i + 90] = cosValue;
sSinTable[i + 180] = sinValue * -1;
sSinTable[i + 270] = cosValue * -1;
costable[i]=cval;
costable[i+90]=sval * -1;
costable[i+180]=cval * -1;
costable[i+270]=sval;
sCosTable[i] = cosValue;
sCosTable[i + 90] = sinValue * -1;
sCosTable[i + 180] = cosValue * -1;
sCosTable[i + 270] = sinValue;
tval=sval/cval;
double tanValue = sinValue / cosValue;
tantable[i]=tval;
tantable[i+90]=tval;
tantable[i+180]=tval;
tantable[i+270]=tval;
sTanTable[i] = tanValue;
sTanTable[i + 90] = tanValue;
sTanTable[i + 180] = tanValue;
sTanTable[i + 270] = tanValue;
}
}
/*!
\brief Overloaded assignment operator
\param from Angle to copy the value from
\return The angle's new value
*/
Angle & Angle::operator=(const Angle &from)
Angle&
Angle::operator=(const Angle &from)
{
fAngleValue=from.fAngleValue;
fAngleValue = from.fAngleValue;
return *this;
}
/*!
\brief Overloaded assignment operator
\param from New value of the angle
\return The angle's new value
*/
Angle & Angle::operator=(const float &from)
Angle&
Angle::operator=(const float &from)
{
fAngleValue=from;
fAngleValue = from;
return *this;
}
/*!
\brief Overloaded assignment operator
\param from New value of the angle
\return The angle's new value
*/
Angle & Angle::operator=(const long &from)
Angle&
Angle::operator=(const long &from)
{
fAngleValue=(float)from;
fAngleValue = (float)from;
return *this;
}
/*!
\brief Overloaded assignment operator
\param from New value of the angle
\return The angle's new value
*/
Angle & Angle::operator=(const int &from)
Angle&
Angle::operator=(const int &from)
{
fAngleValue=(float)from;
fAngleValue = (float)from;
return *this;
}
/*!
\brief Overloaded equivalence operator
\param The angle to compare to
\return True if equal, false if not
*/
bool Angle::operator==(const Angle &from)
bool
Angle::operator==(const Angle &from)
{
return (fAngleValue==from.fAngleValue)?true:false;
return (fAngleValue == from.fAngleValue);
}
/*!
\brief Overloaded inequality operator
\param The angle to compare to
\return True if not equal, false if not
*/
bool Angle::operator!=(const Angle &from)
bool
Angle::operator!=(const Angle &from)
{
return (fAngleValue!=from.fAngleValue)?true:false;
return (fAngleValue != from.fAngleValue);
}
/*!
\brief Overloaded greater than operator
\param The angle to compare to
\return True if greater, false if not
*/
bool Angle::operator>(const Angle &from)
bool
Angle::operator>(const Angle &from)
{
return (fAngleValue>from.fAngleValue)?true:false;
return (fAngleValue > from.fAngleValue);
}
/*!
\brief Overloaded less than operator
\param The angle to compare to
\return True if less than, false if not
*/
bool Angle::operator<(const Angle &from)
bool
Angle::operator<(const Angle &from)
{
return (fAngleValue<from.fAngleValue)?true:false;
return (fAngleValue < from.fAngleValue);
}
/*!
\brief Overloaded greater than or equal to operator
\param The angle to compare to
\return True if greater than or equal to, false if not
*/
bool Angle::operator>=(const Angle &from)
bool
Angle::operator>=(const Angle &from)
{
return (fAngleValue>=from.fAngleValue)?true:false;
return (fAngleValue >= from.fAngleValue);
}
/*!
\brief Overloaded less than or equal to operator
\param The angle to compare to
\return True if less than or equal to, false if not
*/
bool Angle::operator<=(const Angle &from)
bool
Angle::operator<=(const Angle &from)
{
return (fAngleValue<=from.fAngleValue)?true:false;
return (fAngleValue <= from.fAngleValue);
}

View File

@ -70,8 +70,9 @@ virtual ~Angle();
bool operator<=(const Angle &from);
protected:
void _InitTrigTables(void);
float fAngleValue;
void InitTrigTables(void);
};
#endif