License Public Domain
Lines 100
Keywords
angle (3) degree (3) radian (3)
Included in this Library
Permissions
Viewable by Everyone
Editable by All Siafoo Users
Hide
Easily highlight source code for your blog with our Syntax Highlighter. Join Siafoo Now or Learn More

angle.h Atom Feed -1

In Brief Here is the header file for the Angle class. This class allows the user to easily convert between radians and degrees.
# 's
  1/**************************************************************************
2 * Copyright (C) 2006 Michael Sutherland
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
18 *
19 ****************************************************************************/
20
21#ifndef ANGLE_H
22#define ANGLE_H
23
24#define PI 3.141592653589793238
25
26class Angle
27{
28 private: /// variable
29 double m_radians; // angle in radians
30
31 public: /// constructor / destructor
32 Angle(double radians): m_radians(radians) {}
33 Angle(const Angle& angle): m_radians(angle.radians()) {}
34 ~Angle() {}
35 static Angle Radians(double radians) { return Angle(radians); }
36 static Angle Degrees(double degrees) { return Angle(degrees2Radians(degrees)); }
37
38 public: /// accessors
39 double radians() const { return m_radians; }
40 double degrees() const { return radians2Degrees(m_radians); }
41
42 public: /// setting functions
43 void setRadians(double radians) { m_radians = radians; }
44 void setDegrees(double degrees) { m_radians = degrees2Radians(degrees); }
45
46 public: /// overloaded operators
47 Angle operator-() const { return Angle(-m_radians); }
48 Angle& operator=(const Angle& angle) { m_radians = angle.radians(); return *this; }
49 Angle& operator+=(const Angle& angle) { m_radians += angle.radians(); return *this; }
50 Angle& operator-=(const Angle& angle) { m_radians -= angle.radians(); return *this; }
51 Angle& operator*=(double multiplier) { m_radians *= multiplier; return *this; }
52 Angle& operator/=(double divider) { assert(divider); m_radians /= divider; return *this; }
53
54 public: /// member functions
55 /// Untwists an angle (in radians) to range [0, 2*Pi]
56 double Clamp0To2Pi() const;
57 /// Untwists an angle (in radians) to range [-Pi, Pi]
58 double ClampNegPiToPi() const;
59 /// Untwists an angle (in degrees) to range [0, 360]
60 double Clamp0To360() const;
61
62 public: /// static functions
63 static double degrees2Radians(double angle_degrees);
64 static double radians2Degrees(double angle_radians);
65
66};
67
68
69/// some more overloaded operators
70
71inline std::ostream& operator<< (std::ostream& os, const Angle &angle)
72{
73 /// we are going to print everything in degrees, easier to read.
74 return os << angle.degrees();
75}
76
77inline bool operator==(const Angle &lhs, const Angle &rhs)
78{
79 return lhs.radians() == rhs.radians();
80}
81
82inline bool operator!=(const Angle &lhs, const Angle &rhs)
83{
84 return lhs.radians() != rhs.radians();
85}
86
87inline Angle operator+(const Angle& lhs, const Angle& rhs)
88{
89 return Angle(lhs.radians() + rhs.radians());
90}
91
92inline Angle operator-(const Angle& lhs, const Angle& rhs)
93{
94 return Angle(lhs.radians() - rhs.radians());
95}
96
97inline Angle operator*(const double multiplier, const Angle& angle)
98{
99 return Angle(angle.radians() * multiplier);
100}
101
102inline Angle operator*(const Angle& angle, const double multiplier)
103{
104 return Angle(angle.radians() * multiplier);
105}
106
107inline Angle operator/(const Angle& angle, const double divider)
108{
109 assert(divider);
110 return Angle(angle.radians() / divider);
111}
112
113#endif

Here is the header file for the Angle class. This class allows the user to easily convert between radians and degrees.