Ion: math/angle.h Source File

1 

18 #ifndef ION_MATH_ANGLE_H_

19 #define ION_MATH_ANGLE_H_

20 

21 #include <cmath>

22 #include <istream>

23 #include <ostream>

24 

26 

27 namespace ion {

28 namespace math {

29 

32 template <typename T>

34  public:

37 

40  template <typename U>

42  : radians_(static_cast<T>(other.Radians())) {}

43 

46  return Angle(angle);

47  }

48 

51  return Angle(DegreesToRadians(angle));

52  }

53 

55  T Radians() const { return radians_; }

56  T Degrees() const { return RadiansToDegrees(radians_); }

57 

59 

62 

68 

71  return FromRadians(a0.radians_ + a1.radians_);

72  }

74  return FromRadians(a0.radians_ - a1.radians_);

75  }

78  }

81  }

84  }

85 

88  return a0.radians_ == a1.radians_;

89  }

91  return a0.radians_ != a1.radians_;

92  }

93 

96  return a0.radians_ < a1.radians_;

97  }

99  return a0.radians_ > a1.radians_;

100  }

102  return a0.radians_ <= a1.radians_;

103  }

105  return a0.radians_ >= a1.radians_;

106  }

107 

108  private:

109  explicit Angle(const T angle_rad) {

110  radians_ = angle_rad;

111  }

112 

113  static T RadiansToDegrees(const T& radians) {

114  static const T kRadToDeg = 180 / static_cast<T>(M_PI);

115  return radians * kRadToDeg;

116  }

117 

118  static T DegreesToRadians(const T& degrees) {

119  static const T kDegToRad = static_cast<T>(M_PI) / 180;

120  return degrees * kDegToRad;

121  }

122 

123  T radians_;

125 };

126 

127 

129 template <typename T>

130 std::ostream& operator<<(std::ostream& out, const Angle<T>& a) {

131  return out << a.Degrees() << " deg";

132 }

133 

134 template <typename T>

136  T angle;

137  if (in >> angle) {

140  } else {

141  in.clear();

144  }

145  }

146 

147  return in;

148 }

149 

151 

154 

155 

158 

159 }

160 }

161 

162 #endif // ION_MATH_ANGLE_H_

friend bool operator!=(const Angle &a0, const Angle &a1)

void operator-=(const Angle &a)

Angle< float > Anglef

Type-specific typedefs.

friend const Angle operator*(T s, const Angle &a)

void operator+=(const Angle &a)

Self-modifying operators.

friend bool operator>=(const Angle &a0, const Angle &a1)

A simple class to represent angles.

T Radians() const

Get the angle in degrees or radians.

const Angle operator-() const

Unary negation operator.

friend bool operator<=(const Angle &a0, const Angle &a1)

friend const Angle operator-(const Angle &a0, const Angle &a1)

std::istream & GetExpectedString(std::istream &in, const std::string &expected)

Attempts to read a string from the stream and returns the stream.

friend const Angle operator+(const Angle &a0, const Angle &a1)

Binary operators.

static Angle FromRadians(const T &angle)

Create a angle from radians (no conversion).

friend const Angle operator/(const Angle &a, T s)

friend bool operator==(const Angle &a0, const Angle &a1)

Exact equality and inequality comparisons.

static Angle FromDegrees(const T &angle)

Create a angle from degrees (requires conversion).

friend const Angle operator*(const Angle &a, T s)

Angle(const Angle< U > other)

Copy constructor from an instance of any value type that is compatible (via static_cast) with this in...

Angle()

The default constructor creates an angle of 0 (in any unit).

std::istream & operator>>(std::istream &in, Angle< T > &a)

friend bool operator<(const Angle &a0, const Angle &a1)

Comparisons.

friend bool operator>(const Angle &a0, const Angle &a1)