Angle class for radians and degrees

I want the angle class to be initialized in radians or degrees, but I don't know if this is a good idea. I am thinking of something like this:

Class diagram

class Radian;
class Degree;

/**
  * My angle class.
  **/
class Angle
{
    private:
        float m_radian;

    public:
        explicit Angle(const Radian& rad);

        explicit Angle(const Degree& deg);

        float GetRadian(void) const
        {
            return this->m_radian;
        }

        float GetDegree(void) const
        {
            return ToDegree(this->m_radian);
        }

        bool IsEqual(const Angle& angle, float epsilon = 0.001f) const
        {
            return Abs<float>(this->m_radian - angle.m_radian) < epsilon;
        }

        void Set(const Angle& ang);

    protected:
        Angle(void) : m_radian(0.0f)
        {}

        Angle(float rad) : m_radian(rad)
        {}
};

class Radian : public Angle
{
    public:
        Radian(void)
        {}

        Radian(float r) : Angle(r)
        {}
};

class Degree : public Angle
{
    public:
        Degree(void)
        {}

        Degree(float d) : Angle(ToRadian(d))
        {}
};

/// Trigonometric functions.
float Sin(const Angle& angle);
...

This way, I will not confuse radians and degrees in my code. But is this a good idea, or should I use a different design?

+5
source share
4 answers

I do not see the need for inheritance here. As for using your class, all that matters is that you get the angle - regardless of whether it was in degrees or radians, it doesn't matter to start with it.

: . . , : . :

Angle a(1.2345, Angle::Radians);
std::cout << a.radians() << a.degrees() << sin(a);

, , : . .

Angle r = Radians(2.3);
Angle d = Degrees(180);

- , . , !

+8

. , - . , , , - Angle .

:

class Angle
{
    public:
        static Angle fromRadians( float v );
        static Angle fromDegrees( float v );
        // ...

    private:
        Angle( float rad );
        // ...
};

factory, . , :

void f( Angle::fromDegrees( 3.0 ), Angle::fromRadians( 17.0 ) );
+13

:

class Radians {
    explicit Radians(float a) : angle_(a) {}
    Radians(Degrees a)        : angle_(a * PI/180.f) {}
    operator float()          { return angle_; }
private:
    float angle_;
}

class Degrees {
    explicit Degrees(float a) : angle_(a) {}
    Degrees(Radians a)        : angle_(a * 180.f/PI) {}
    operator float()          { return angle_; }
private:
    float angle_;
}

, . Sin, , , . , :

float Sin(Radians x);
float Sin(Degrees x);

( , - , ):

float Sin(Radians x);

, "" (, , ), .

Having an abstract base class Angleincreases syntax noise (you need to use links everywhere) and is likely to decrease performance. This solution also allows you to store your corners in the desired units, rather than hoping that you get a โ€œquick pathโ€.

+5
source

I would use angle as an object, degree and radian as a different getter installer for the same value of the internal angle.

+1
source

All Articles