2015-08-01 14:36:28 -04:00
|
|
|
//
|
|
|
|
// FILE: Angle.cpp
|
|
|
|
// AUTHOR: Rob Tillaart
|
2020-11-27 05:10:47 -05:00
|
|
|
// VERSION: 0.1.8
|
2015-08-01 14:36:28 -04:00
|
|
|
// PURPOSE: library for Angle math for Arduino
|
2020-11-27 05:10:47 -05:00
|
|
|
// URL: https://github.com/RobTillaart/Angle
|
|
|
|
// http://forum.arduino.cc/index.php?topic=339402
|
2015-08-01 14:36:28 -04:00
|
|
|
//
|
|
|
|
// Released to the public domain
|
2020-11-27 05:10:47 -05:00
|
|
|
// 0.1.8 2020-05-27 update library.json
|
|
|
|
// 0.1.7 2020-03-26 refactor #pragma once
|
2015-10-19 14:15:06 -04:00
|
|
|
// 0.1.06 - fixed bug negative values.
|
2015-08-23 12:20:08 -04:00
|
|
|
// 0.1.05 - added AngleFormat proxy added 03/03/15 by Christoper Andrews.
|
|
|
|
// 0.1.04 - changed thousands in tenthousands, string constructor
|
|
|
|
// 0.1.03 - added URL, fromRadians [TEST]
|
2015-08-02 07:38:56 -04:00
|
|
|
// 0.1.02 - added toRadians() + fix compare()
|
2015-08-01 14:44:45 -04:00
|
|
|
// 0.1.01 - cleanup a bit
|
2015-08-01 14:36:28 -04:00
|
|
|
// 0.1.00 - initial version
|
|
|
|
|
|
|
|
#include "Angle.h"
|
|
|
|
|
2015-08-03 07:42:59 -04:00
|
|
|
AngleFormat::AngleFormat( const Angle &ref, AngleFormatMode format )
|
2015-08-23 12:20:08 -04:00
|
|
|
: angle(ref), mode(format) {}
|
2015-08-03 07:42:59 -04:00
|
|
|
|
|
|
|
size_t AngleFormat::printTo(Print& p) const
|
|
|
|
{
|
|
|
|
return angle.printTo( p, mode );
|
|
|
|
}
|
|
|
|
|
2015-10-19 14:15:06 -04:00
|
|
|
Angle::Angle(int dd, int mm, int ss, int tt) // todo optimize
|
|
|
|
{
|
|
|
|
neg = false;
|
|
|
|
d = dd;
|
|
|
|
m = mm;
|
|
|
|
s = ss;
|
|
|
|
t = tt;
|
|
|
|
// TODO
|
|
|
|
// normalize();
|
2020-11-27 05:10:47 -05:00
|
|
|
// assume only one (largest) parameter is negative at most...
|
2015-10-19 14:15:06 -04:00
|
|
|
if (d < 0) { d = -d; neg = true; }
|
|
|
|
if (m < 0) { m = -m; neg = true; }
|
|
|
|
if (s < 0) { s = -s; neg = true; }
|
|
|
|
if (t < 0) { t = -t; neg = true; }
|
|
|
|
while (t >= 10000) { s++; t -= 10000; }
|
|
|
|
while (s >= 60) { m++; s -= 60; }
|
|
|
|
while (m >= 60) { d++; m -= 60; }
|
|
|
|
if (d == 0 && m == 0 && s == 0 && t == 0) neg = false;
|
|
|
|
}
|
|
|
|
|
2015-08-01 14:36:28 -04:00
|
|
|
Angle::Angle(const double alpha)
|
|
|
|
{
|
|
|
|
double a = alpha;
|
2015-10-19 14:15:06 -04:00
|
|
|
neg = (alpha < 0);
|
2015-08-01 14:36:28 -04:00
|
|
|
if (neg) a = -a;
|
2015-08-01 14:44:45 -04:00
|
|
|
|
2015-08-01 14:36:28 -04:00
|
|
|
d = int(a);
|
|
|
|
a = a - d;
|
2015-08-01 14:44:45 -04:00
|
|
|
// unsigned long p = a * 3600000L;
|
|
|
|
// 3600 000 = 2^7 • 3^2 • 5^5 = 128 * 28125
|
|
|
|
// 2^7 = 128 will only affect exponent - no loss precision
|
|
|
|
// 28125 is less digits so less loss of significant digits.
|
2015-08-23 12:20:08 -04:00
|
|
|
// upgraded to 4 decimal seconds
|
|
|
|
// 36 000 000L = 256 * 140625
|
|
|
|
a = a * 256;
|
|
|
|
unsigned long p = round(a * 140625.0);
|
|
|
|
t = p % 10000UL;
|
|
|
|
p = p / 10000UL;
|
2015-08-01 14:36:28 -04:00
|
|
|
s = p % 60UL;
|
2015-08-23 12:20:08 -04:00
|
|
|
m = p / 60UL;
|
2015-08-01 14:36:28 -04:00
|
|
|
}
|
|
|
|
|
2015-08-23 12:20:08 -04:00
|
|
|
Angle::Angle(char * str)
|
|
|
|
{
|
|
|
|
uint32_t yy = 0;
|
|
|
|
uint8_t d_cnt = 0;
|
2015-10-19 14:15:06 -04:00
|
|
|
neg = false;
|
2015-08-23 12:20:08 -04:00
|
|
|
// parse whole degrees
|
|
|
|
char *p = str;
|
|
|
|
d = 0;
|
2015-10-19 14:15:06 -04:00
|
|
|
// skip crap
|
2015-08-23 12:20:08 -04:00
|
|
|
while (!isdigit(*p) && (*p != '-')) p++;
|
2015-10-19 14:15:06 -04:00
|
|
|
// process sign
|
2015-08-23 12:20:08 -04:00
|
|
|
if (*p == '-')
|
|
|
|
{
|
|
|
|
neg = true;
|
|
|
|
p++;
|
|
|
|
}
|
2015-10-19 14:15:06 -04:00
|
|
|
if (*p == '+') p++;
|
|
|
|
// parse whole part into degrees;
|
2015-08-23 12:20:08 -04:00
|
|
|
while (isdigit(*p))
|
|
|
|
{
|
|
|
|
d *= 10;
|
|
|
|
d += (*p - '0');
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
// parse decimal part into an uint32_t
|
|
|
|
if (*p != '\0')
|
|
|
|
{
|
|
|
|
p++; // skip .
|
|
|
|
while (isdigit(*p) && d_cnt < 9)
|
|
|
|
{
|
|
|
|
d_cnt++;
|
|
|
|
yy *= 10;
|
|
|
|
yy += (*p - '0');
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// make sure we have 9 decimal places.
|
|
|
|
while (d_cnt < 9)
|
|
|
|
{
|
|
|
|
d_cnt++;
|
|
|
|
yy *= 10;
|
|
|
|
}
|
|
|
|
// convert float to degrees. 1000000000 ~> 36000000 -> /250 * 9
|
|
|
|
// yy = yy * 4 / 125 + yy / 250; // just keeps the math within 32 bits
|
|
|
|
yy = yy * 4 / 125;
|
|
|
|
yy = yy + (yy + 4)/ 8; // just keeps the math within 32 bits
|
|
|
|
// split yy in m, s, tt
|
|
|
|
t = yy % 10000UL;
|
|
|
|
yy = yy / 10000UL;
|
|
|
|
s = yy % 60;
|
|
|
|
m = yy / 60;
|
|
|
|
}
|
|
|
|
|
2015-08-01 14:36:28 -04:00
|
|
|
// PRINTING
|
2015-08-03 07:42:59 -04:00
|
|
|
size_t Angle::printTo(Print& p, AngleFormatMode mode) const
|
2015-08-01 14:36:28 -04:00
|
|
|
{
|
2015-08-03 07:42:59 -04:00
|
|
|
unsigned char c = mode;
|
2015-08-23 12:20:08 -04:00
|
|
|
char separator[4] = ".\'\""; // "..."; // ALT-0176 = ° 179.59.59.9999
|
|
|
|
|
2015-08-01 14:36:28 -04:00
|
|
|
size_t n = 0;
|
2015-10-19 14:15:06 -04:00
|
|
|
if (neg) n += p.print('-');
|
2015-08-01 14:36:28 -04:00
|
|
|
n += p.print(d);
|
2015-08-23 12:20:08 -04:00
|
|
|
n += p.print(separator[0]);
|
2015-08-03 07:42:59 -04:00
|
|
|
if( --c )
|
|
|
|
{
|
|
|
|
if (m < 10) n += p.print('0');
|
|
|
|
n += p.print(m);
|
2015-08-23 12:20:08 -04:00
|
|
|
n += p.print(separator[1]);
|
2015-08-03 07:42:59 -04:00
|
|
|
if( --c )
|
|
|
|
{
|
|
|
|
if (s < 10) n += p.print('0');
|
|
|
|
n += p.print(s);
|
2015-08-23 12:20:08 -04:00
|
|
|
n += p.print(separator[2]);
|
|
|
|
if( --c )
|
2015-08-03 07:42:59 -04:00
|
|
|
{
|
2015-08-23 12:20:08 -04:00
|
|
|
if (t < 1000) n += p.print('0');
|
2015-08-03 07:42:59 -04:00
|
|
|
if (t < 100) n += p.print('0');
|
|
|
|
if (t < 10) n += p.print('0');
|
|
|
|
n += p.print(t);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-08-01 14:36:28 -04:00
|
|
|
return n;
|
|
|
|
};
|
|
|
|
|
|
|
|
double Angle::toDouble(void)
|
|
|
|
{
|
2015-08-23 12:20:08 -04:00
|
|
|
long v = t + s * 10000UL + m * 600000UL;
|
|
|
|
double val = ((1.0 / 140625.0) / 256) * v + d;
|
|
|
|
if (neg) val = -val;
|
2015-08-01 14:36:28 -04:00
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
2015-10-19 14:15:06 -04:00
|
|
|
// NEGATE
|
|
|
|
Angle Angle::operator - ()
|
|
|
|
{
|
|
|
|
Angle temp = *this;
|
|
|
|
if (temp.d == 0 && temp.m == 0 && temp.s == 0 && temp.t == 0)
|
|
|
|
{
|
|
|
|
temp.neg = false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
temp.neg = !neg;
|
|
|
|
}
|
|
|
|
return temp;
|
|
|
|
};
|
|
|
|
|
2015-08-01 14:36:28 -04:00
|
|
|
// BASIC MATH
|
2015-10-19 14:15:06 -04:00
|
|
|
Angle Angle::operator + (const Angle &a) // TOCHECK
|
2015-08-01 14:36:28 -04:00
|
|
|
{
|
2015-08-01 14:44:45 -04:00
|
|
|
return addHelper(a);
|
2015-08-01 14:36:28 -04:00
|
|
|
}
|
|
|
|
|
2015-10-19 14:15:06 -04:00
|
|
|
Angle& Angle::operator += (const Angle &a) // TOCHECK
|
2015-08-01 14:36:28 -04:00
|
|
|
{
|
2015-08-01 14:44:45 -04:00
|
|
|
*this = addHelper(a);
|
|
|
|
return *this;
|
2015-08-01 14:36:28 -04:00
|
|
|
}
|
|
|
|
|
2015-10-19 14:15:06 -04:00
|
|
|
Angle Angle::addHelper(const Angle &a) // TOCHECK
|
2015-08-01 14:36:28 -04:00
|
|
|
{
|
|
|
|
Angle temp = *this;
|
2015-10-19 14:15:06 -04:00
|
|
|
if (temp.neg == a.neg)
|
2015-08-01 14:36:28 -04:00
|
|
|
{
|
|
|
|
temp.d += a.d;
|
|
|
|
temp.m += a.m;
|
|
|
|
temp.s += a.s;
|
|
|
|
temp.t += a.t;
|
|
|
|
}
|
2015-08-01 14:44:45 -04:00
|
|
|
else
|
2015-08-01 14:36:28 -04:00
|
|
|
{
|
2015-10-19 14:15:06 -04:00
|
|
|
temp.d -= a.d;
|
2015-08-01 14:36:28 -04:00
|
|
|
temp.m -= a.m;
|
|
|
|
temp.s -= a.s;
|
|
|
|
temp.t -= a.t;
|
|
|
|
}
|
|
|
|
temp.normalize();
|
2015-10-19 14:15:06 -04:00
|
|
|
|
2015-08-01 14:44:45 -04:00
|
|
|
return temp;
|
2015-08-01 14:36:28 -04:00
|
|
|
}
|
|
|
|
|
2015-10-19 14:15:06 -04:00
|
|
|
Angle Angle::operator - (const Angle &a) // TOCHECK
|
2015-08-01 14:36:28 -04:00
|
|
|
{
|
2015-08-02 07:38:56 -04:00
|
|
|
return subHelper(a);
|
2015-08-01 14:36:28 -04:00
|
|
|
}
|
|
|
|
|
2015-10-19 14:15:06 -04:00
|
|
|
Angle& Angle::operator -= (const Angle &a) // TOCHECK
|
2015-08-02 07:38:56 -04:00
|
|
|
{
|
|
|
|
*this = subHelper(a);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2015-10-19 14:15:06 -04:00
|
|
|
Angle Angle::subHelper(const Angle &a) // TOCHECK
|
2015-08-01 14:36:28 -04:00
|
|
|
{
|
|
|
|
Angle temp = *this;
|
2015-10-19 14:15:06 -04:00
|
|
|
if (temp.neg == a.neg)
|
2015-08-01 14:36:28 -04:00
|
|
|
{
|
|
|
|
temp.d -= a.d;
|
|
|
|
temp.m -= a.m;
|
|
|
|
temp.s -= a.s;
|
|
|
|
temp.t -= a.t;
|
|
|
|
}
|
2015-08-01 14:44:45 -04:00
|
|
|
else
|
2015-08-01 14:36:28 -04:00
|
|
|
{
|
2015-10-19 14:15:06 -04:00
|
|
|
temp.d += a.d;
|
2015-08-01 14:36:28 -04:00
|
|
|
temp.m += a.m;
|
|
|
|
temp.s += a.s;
|
|
|
|
temp.t += a.t;
|
|
|
|
}
|
|
|
|
temp.normalize();
|
|
|
|
return temp;
|
|
|
|
}
|
|
|
|
|
|
|
|
Angle Angle::operator * (const double dd)
|
|
|
|
{
|
2015-08-23 12:20:08 -04:00
|
|
|
return Angle(this->toDouble() * dd);
|
2015-08-01 14:36:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
Angle Angle::operator / (const double dd)
|
|
|
|
{
|
|
|
|
return Angle(this->toDouble() / dd);
|
|
|
|
}
|
|
|
|
|
|
|
|
Angle& Angle::operator *= (const double dd)
|
|
|
|
{
|
2015-08-02 07:38:56 -04:00
|
|
|
*this = this->toDouble() * dd;
|
2015-08-01 14:36:28 -04:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
Angle& Angle::operator /= (const double dd)
|
|
|
|
{
|
2015-08-02 07:38:56 -04:00
|
|
|
*this = this->toDouble() / dd;
|
2015-08-01 14:36:28 -04:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2015-08-23 12:20:08 -04:00
|
|
|
// !! can divide by zero
|
2015-08-01 14:36:28 -04:00
|
|
|
double Angle::operator / (Angle& a)
|
|
|
|
{
|
|
|
|
double f = this->toDouble();
|
|
|
|
double g = a.toDouble();
|
|
|
|
return f/g;
|
|
|
|
}
|
2015-08-02 07:38:56 -04:00
|
|
|
|
2015-08-01 14:36:28 -04:00
|
|
|
///////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// PRIVATE
|
|
|
|
//
|
|
|
|
int Angle::compare(const Angle &a, const Angle &b)
|
|
|
|
{
|
2015-10-19 14:15:06 -04:00
|
|
|
// check sign first
|
|
|
|
if (!a.neg && b.neg) return 1;
|
|
|
|
if (a.neg && !b.neg) return -1;
|
|
|
|
// check abs value
|
|
|
|
int rv = 0;
|
|
|
|
if (a.d > b.d) rv = 1;
|
|
|
|
else if (a.d < b.d) rv = -1;
|
|
|
|
else if (a.m > b.m) rv = 1;
|
|
|
|
else if (a.m < b.m) rv = -1;
|
|
|
|
else if (a.s > b.s) rv = 1;
|
|
|
|
else if (a.s < b.s) rv = -1;
|
|
|
|
else if (a.t > b.t) rv = 1;
|
|
|
|
else if (a.t < b.t) rv = -1;
|
|
|
|
|
|
|
|
if (rv != 0 && a.neg) rv = -rv;
|
|
|
|
return rv;
|
2015-08-01 14:36:28 -04:00
|
|
|
}
|
|
|
|
|
2015-10-19 14:15:06 -04:00
|
|
|
void Angle::normalize() // TOCHECK
|
2015-08-01 14:36:28 -04:00
|
|
|
{
|
2015-08-23 12:20:08 -04:00
|
|
|
while (t < 0) { s--; t += 10000; }
|
|
|
|
while (t >= 10000) { s++; t -= 10000; }
|
|
|
|
while (s < 0) { m--; s += 60; }
|
|
|
|
while (s >= 60) { m++; s -= 60; }
|
|
|
|
while (m < 0) { d--; m += 60; }
|
|
|
|
while (m >= 60) { d++; m -= 60; }
|
2015-08-01 14:36:28 -04:00
|
|
|
|
2015-10-19 14:15:06 -04:00
|
|
|
if (d < 0)
|
|
|
|
{
|
|
|
|
if (t != 0)
|
|
|
|
{
|
|
|
|
t = 10000 - t;
|
|
|
|
s++;
|
|
|
|
}
|
|
|
|
if (s != 0)
|
|
|
|
{
|
|
|
|
s = (60 - s) % 60;
|
|
|
|
m++;
|
|
|
|
}
|
|
|
|
if (m != 0)
|
|
|
|
{
|
|
|
|
m = (60 - m) % 60;
|
|
|
|
d++;
|
|
|
|
}
|
|
|
|
d = -d;
|
|
|
|
neg = !neg;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (d == 0 && m == 0 && s == 0 && t == 0) neg = false;
|
2015-08-01 14:36:28 -04:00
|
|
|
}
|
|
|
|
|
2020-11-27 05:10:47 -05:00
|
|
|
// --- END OF FILE ---
|