Unity conversion between radians and degrees

This topic is 4379 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Hello, Community! I just had an idea on how to make radian/degree conversion easier in my engine. What if I encapsulated each in a class that automatically makes the conversion. However, in a previous post, the concept of making a class based off of intrisic was doomed to failure. ex:
class Radian
{

public:
Radian ( const Degree& d );
const Radian& operator = ( const Real& f ) { mRad = f; return *this; }
const Radian& operator = ( const Degree& d );

Real GetDegrees() const; // see bottom of this file
Real GetAngleUnits() const;

Radian operator + ( const Degree& d ) const;
Radian& operator += ( const Degree& d );
Radian operator - ( const Degree& d ) const;
Radian& operator -= ( const Degree& d );
Radian operator * ( Real f ) const { return Radian ( mRad * f ); }
Radian& operator *= ( Real f ) { mRad *= f; return *this; }
Radian operator / ( Real f ) const { return Radian ( mRad / f ); }
Radian& operator /= ( Real f ) { mRad /= f; return *this; }

};


Where 'Real' can be typedef'd to float or double; PS. This is from the OGRE engine [smile] I'm just curious to see if it sounds like a good idea, exorcist_bob

Share on other sites
Why not just make the interface to all your engine's methods take radians, rather than an arbitrary combination of different angular units? Then you don't need a separate class at all.

Share on other sites
The reason for it is for extensibilities sake, so that if someone else builds a module that returns degrees, I can easily convert it over.

Share on other sites
You don't need full blown classes for that. Just have a rad_2_deg() and deg_2_rad() function and you're set.

Share on other sites
Quote:
 Original post by exorcist_bobThe reason for it is for extensibilities sake, so that if someone else builds a module that returns degrees, I can easily convert it over.

YAGNI ?

What's wrong with:
namespace {  const float pi = 3.14159f;    inline float degToRad(float rad)  {    return rad * 180.0f / pi;  }  inline float radToDeg(float deg)  {    return deg * pi / 180.0f;  }}

or even:

I see. Thanks!

Share on other sites
Quote:
 Original post by exorcist_bobThe reason for it is for extensibilities sake, so that if someone else builds a module that returns degrees, I can easily convert it over.

As the engine's interface designer, you get to specify what is reasonable and what is not. My opinion is that if you already support radians, you need not support degrees, and vice versa. That is, it is not unreasonable to declare "my interface shall only take radians, and your modules must comply accordingly".

1. 1
Rutin
21
2. 2
3. 3
JoeJ
18
4. 4
5. 5

• 13
• 38
• 23
• 13
• 13
• Forum Statistics

• Total Topics
631715
• Total Posts
3001868
×