File: Angle.cc

package info (click to toggle)
xtide 2.6.4-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 2,996 kB
  • ctags: 2,617
  • sloc: cpp: 26,266; ansic: 8,105; makefile: 152; yacc: 113; sh: 54; lex: 54
file content (217 lines) | stat: -rw-r--r-- 4,472 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
// $Id: Angle.cc,v 1.2 2002/10/04 13:44:00 flaterco Exp $
// Angle:  abstraction to partially alleviate degrees versus radians debate.
// All angles are "normalized" to the 0-2pi rad (0-360 degree) range.

/*
    Copyright (C) 1997  David Flater.

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include "common.hh"

void
Angle::normalize() {
  radians = fmod (radians, 2.0 * M_PI);
  if (radians < 0.0)
    radians += 2.0 * M_PI;
}

void Angle::setup (units u, qualifier q, double v) {
  myqual = q;
  switch (u) {
  case DEGREES:
    // Convert to radians and fall through.
    v = v * M_PI / 180.0;
  case RADIANS:
    radians = v;
    normalize();
    break;
  default:
    assert (0);
  }
}

Angle::Angle (units u, qualifier q, double v) {
  setup (u, q, v);
}

Angle::Angle (units u, double v) {
  setup (u, Angle::NONE, v);
}

Angle::Angle () {
  radians = 0.0;
}

Angle::qualifier Angle::qual() {
  return myqual;
}

double Angle::deg () {
  return radians * 180.0 / M_PI;
}

double Angle::rad () {
  return radians;
}

Dstr &
Angle::ppdeg (unsigned precision) {
  static Dstr retbuf;
  char tempbuf[80];
  int width = precision + 4;
  assert (width < 80);
  if (precision == 0)
    width = 3;
  sprintf (tempbuf, "%*.*f", width, (int)(precision), deg());

  // The rounding can result in us seeing 360.00 -- but we don't want that.
  if (tempbuf[0] == '3' && tempbuf[1] == '6')
    tempbuf[0] = tempbuf[1] = ' ';

  retbuf = tempbuf;
  return retbuf;
}

Dstr &Angle::ppqdeg (unsigned precision) {
  static Dstr retbuf;
  retbuf = ppdeg (precision);
  retbuf += '\260';
  switch (myqual) {
  case Angle::TRU:
    retbuf += " True";
    break;
  default:
    ;
  }
  retbuf.trim();
  return retbuf;
}

Angle &
Angle::operator+= (Angle a) {
  radians += a.rad();
  normalize();
  return (*this);
}

Angle &
Angle::operator-= (Angle a) {
  radians -= a.rad();
  normalize();
  return (*this);
}

Angle &
Angle::operator*= (double a) {
  radians *= a;
  normalize();
  return (*this);
}

Degrees::Degrees (double v): Angle (Angle::DEGREES, v) {};
Radians::Radians (double v): Angle (Angle::RADIANS, v) {};

// Save for a rainy day
// This gives the whole nine yards:  degrees, minutes, and seconds.
// ostream &operator<< (ostream &out, Angle &v) {
//   double degrees, minutes, seconds;
//   degrees = v.deg();
//   minutes = fmod (degrees, 1.0);
//   degrees -= minutes;
//   minutes = fabs (minutes) * 60.0;
//   seconds = fmod (minutes, 1.0);
//   minutes -= seconds;
//   seconds *= 60.0;
//   out << (int)degrees << '\260' << (int)minutes << "'" << seconds << "''";
//   return out;
// }

#if 0
// This just gives degrees with decimals, not degrees/minutes/seconds.
ostream &operator<< (ostream &out, Angle v) {
  out << v.ppdeg (2);
  return out;
}
#endif

Angle operator+ (Angle a, Angle b) {
  return Radians (a.rad() + b.rad());
}

Angle operator- (Angle a, Angle b) {
  return Radians (a.rad() - b.rad());
}

Angle operator* (double a, Angle b) {
  return Radians (a * b.rad());
}

Angle operator* (Angle a, double b) {
  return Radians (b * a.rad());
}

int operator> (Angle a, Angle b) {
  if (a.rad() > b.rad())
    return 1;
  return 0;
}

int operator< (Angle a, Angle b) {
  if (a.rad() < b.rad())
    return 1;
  return 0;
}

int operator>= (Angle a, Angle b) {
  if (a.rad() >= b.rad())
    return 1;
  return 0;
}

int operator<= (Angle a, Angle b) {
  if (a.rad() <= b.rad())
    return 1;
  return 0;
}

int operator== (Angle a, Angle b) {
  if (a.rad() == b.rad())
    return 1;
  return 0;
}

int operator!= (Angle a, Angle b) {
  if (a.rad() != b.rad())
    return 1;
  return 0;
}

double sin (Angle a) {
  return sin (a.rad());
}

double cos (Angle a) {
  return cos (a.rad());
}

double tan (Angle a) {
  return tan (a.rad());
}

double cot (Angle a) {
  return 1.0 / tan (a);
}