3 // Part of the Architektonas Project
4 // Originally part of QCad Community Edition by Andrew Mustun
5 // Extensively rewritten and refactored by James L. Hammons
6 // Portions copyright (C) 2001-2003 RibbonSoft
7 // Copyright (C) 2010 Underground Software
8 // See the README and GPLv2 files for licensing and warranty information
10 // JLH = James L. Hammons <jlhamm@acm.org>
13 // --- ---------- -----------------------------------------------------------
14 // JLH 06/01/2010 Added this text. :-)
19 #include <assert.h> // For test()
26 * Rounds the given double to the next int.
28 int RS_Math::round(double v)
30 return (v - floor(v) < 0.5 ? (int)floor(v) : (int)ceil(v));
36 double RS_Math::pow(double x, double y)
39 double ret = ::pow(x, y);
43 RS_DEBUG->print(RS_Debug::D_ERROR, "RS_Math::pow: EDOM in pow");
46 else if (errno == ERANGE)
48 RS_DEBUG->print(RS_Debug::D_WARNING, "RS_Math::pow: ERANGE in pow");
56 * Converts radians to degrees.
58 double RS_Math::rad2deg(double a)
60 return (a / (2.0 * M_PI) * 360.0);
64 * Converts degrees to radians.
66 double RS_Math::deg2rad(double a)
68 return ((a / 360.0) * (2.0 * M_PI));
72 * Converts radians to gradians.
74 double RS_Math::rad2gra(double a)
76 return (a / (2.0 * M_PI) * 400.0);
80 * Finds greatest common divider using Euclid's algorithm.
82 int RS_Math::findGCD(int a, int b)
95 * Tests if angle a is between a1 and a2. a, a1 and a2 must be in the
96 * range between 0 and 2*PI.
99 * @param reversed true for clockwise testing. false for ccw testing.
100 * @return true if the angle a is between a1 and a2.
102 bool RS_Math::isAngleBetween(double a, double a1, double a2, bool reversed)
113 if (a1 >= a2 - 1.0e-12)
115 if (a >= a1 - 1.0e-12 || a <= a2 + 1.0e-12)
122 if (a >= a1 - 1.0e-12 && a <= a2 + 1.0e-12)
128 //RS_DEBUG->print("angle %f is %sbetween %f and %f",
129 // a, ret ? "" : "not ", a1, a2);
134 * Corrects the given angle to the range of 0-2*Pi.
136 double RS_Math::correctAngle(double a)
148 * @return The angle that needs to be added to a1 to reach a2.
149 * Always positive and less than 2*pi.
151 double RS_Math::getAngleDifference(double a1, double a2)
167 * Makes a text constructed with the given angle readable. Used
168 * for dimension texts and for mirroring texts.
170 * @param readable true: make angle readable, false: unreadable
171 * @param corrected Will point to true if the given angle was
172 * corrected, false otherwise.
174 * @return The given angle or the given angle+PI, depending which on
175 * is readable from the bottom or right.
177 double RS_Math::makeAngleReadable(double angle, bool readable, bool * corrected)
180 bool cor = isAngleReadable(angle) ^ readable;
189 if (corrected != NULL)
196 * @return true: if the given angle is in a range that is readable
197 * for texts created with that angle.
199 bool RS_Math::isAngleReadable(double angle)
201 if (angle > M_PI /2.0 * 3.0 + 0.001 || angle < M_PI / 2.0 + 0.001)
208 * @param tol Tolerance in rad.
209 * @retval true The two angles point in the same direction.
211 bool RS_Math::isSameDirection(double dir1, double dir2, double tol)
213 double diff = fabs(dir1 - dir2);
215 if (diff < tol || diff > 2 * M_PI - tol)
217 //std::cout << "RS_Math::isSameDirection: " << dir1 << " and " << dir2
218 // << " point in the same direction" << "\n";
222 //std::cout << "RS_Math::isSameDirection: " << dir1 << " and " << dir2
223 // << " don't point in the same direction" << "\n";
228 * Compares two double values with a tolerance.
230 bool RS_Math::cmpDouble(double v1, double v2, double tol)
232 return (fabs(v2 - v1) < tol);
236 * Evaluates a mathematical expression and returns the result.
237 * If an error occured, the given default value 'def' will be returned.
239 double RS_Math::eval(const QString & expr, double def)
242 double res = RS_Math::eval(expr, &ok);
246 //std::cerr << "RS_Math::evaluate: Parse error at col "
247 //<< ret << ": " << fp.ErrorMsg() << "\n";
255 * Evaluates a mathematical expression and returns the result.
256 * If an error occured, ok will be set to false (if ok isn't NULL).
258 double RS_Math::eval(const QString & expr, bool * ok)
269 fp.AddConstant("pi", M_PI);
271 // replace '14 3/4' with '14+3/4'
278 // int i = s.find(QRegExp("[0-9]* [0-9]*/[0-9]*"));
279 int i = s.indexOf(QRegExp("[0-9]* [0-9]*/[0-9]*"));
283 // int i2 = s.find(' ', i);
284 int i2 = s.indexOf(' ', i);
288 s.replace(i2, 1, "+");
295 int ret = fp.Parse(s.toLatin1().data(), "", true);
308 return fp.Eval(NULL);
312 * Converts a double into a string which is as short as possible
314 * @param value The double value
315 * @param prec Precision e.g. a precision of 1 would mean that a
316 * value of 2.12030 will be converted to "2.1". 2.000 is always just "2").
318 QString RS_Math::doubleToString(double value, double prec)
322 std::cerr << "RS_Math::doubleToString: invalid precision\n";
329 int num = RS_Math::round(value / prec);
331 exaStr = RS_Math::doubleToString(prec, 10);
332 // dotPos = exaStr.find('.');
333 dotPos = exaStr.indexOf('.');
336 ret.sprintf("%d", RS_Math::round(num * prec));
339 int digits = exaStr.length() - dotPos - 1;
340 ret = RS_Math::doubleToString(num * prec, digits);
347 * Converts a double into a string which is as short as possible.
349 * @param value The double value
350 * @param prec Precision
352 QString RS_Math::doubleToString(double value, int prec)
356 valStr.setNum(value, 'f', prec);
358 if (valStr.contains('.'))
360 // Remove zeros at the end:
361 while (valStr.at(valStr.length() - 1) == '0')
362 valStr.truncate(valStr.length() - 1);
364 if (valStr.at(valStr.length() - 1) == '.')
365 valStr.truncate(valStr.length() - 1);
372 * Performs some testing for the math class.
376 std::cout << "RS_Math::test: doubleToString:\n";
379 QString s = RS_Math::doubleToString(v, 0.1);
381 s = RS_Math::doubleToString(v, 0.01);
383 s = RS_Math::doubleToString(v, 0.0);
387 s = RS_Math::doubleToString(v, 0.1);
389 s = RS_Math::doubleToString(v, 0.01);
391 s = RS_Math::doubleToString(v, 0.0);
395 s = RS_Math::doubleToString(v, 0.1);
397 s = RS_Math::doubleToString(v, 0.01);
399 s = RS_Math::doubleToString(v, 0.001);
400 assert(s == "0.001");
401 s = RS_Math::doubleToString(v, 0.0);
404 std::cout << "RS_Math::test: complete\n";