Sign in to follow this  

Unity conversion between radians and degrees

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

If you intended to correct an error in the post then please contact us.

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
{
	Real mRad;

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

	Real GetDegrees() const; // see bottom of this file
	Real GetRadians() const { return mRad; }
	Real GetAngleUnits() const;

	Radian operator + ( const Radian& r ) const { return Radian ( mRad + r.mRad ); }
	Radian operator + ( const Degree& d ) const;
	Radian& operator += ( const Radian& r ) { mRad += r.mRad; return *this; }
	Radian& operator += ( const Degree& d );
	Radian operator - () { return Radian(-mRad); }
	Radian operator - ( const Radian& r ) const { return Radian ( mRad - r.mRad ); }
	Radian operator - ( const Degree& d ) const;
	Radian& operator -= ( const Radian& r ) { mRad -= r.mRad; return *this; }
	Radian& operator -= ( const Degree& d );
	Radian operator * ( Real f ) const { return Radian ( mRad * f ); }
    Radian operator * ( const Radian& f ) const { return Radian ( mRad * f.mRad ); }
	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; }

	bool operator <  ( const Radian& r ) const { return mRad <  r.mRad; }
	bool operator <= ( const Radian& r ) const { return mRad <= r.mRad; }
	bool operator == ( const Radian& r ) const { return mRad == r.mRad; }
	bool operator != ( const Radian& r ) const { return mRad != r.mRad; }
	bool operator >= ( const Radian& r ) const { return mRad >= r.mRad; }
	bool operator >  ( const Radian& r ) const { return mRad >  r.mRad; }
};

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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by exorcist_bob
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.


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;
}
}

Share this post


Link to post
Share on other sites
Quote:
Original post by exorcist_bob
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.

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".

Share this post


Link to post
Share on other sites

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

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this