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 // (C) 2010 Underground Software
8 // JLH = James L. Hammons <jlhamm@acm.org>
11 // --- ---------- -----------------------------------------------------------
12 // JLH 06/01/2010 Added this text. :-)
17 #include <assert.h> // For test()
24 * Rounds the given double to the next int.
26 int RS_Math::round(double v)
28 return (v - floor(v) < 0.5 ? (int)floor(v) : (int)ceil(v));
34 double RS_Math::pow(double x, double y)
37 double ret = ::pow(x, y);
41 RS_DEBUG->print(RS_Debug::D_ERROR, "RS_Math::pow: EDOM in pow");
44 else if (errno == ERANGE)
46 RS_DEBUG->print(RS_Debug::D_WARNING, "RS_Math::pow: ERANGE in pow");
54 * Converts radians to degrees.
56 double RS_Math::rad2deg(double a)
58 return (a / (2.0 * M_PI) * 360.0);
62 * Converts degrees to radians.
64 double RS_Math::deg2rad(double a)
66 return ((a / 360.0) * (2.0 * M_PI));
70 * Converts radians to gradians.
72 double RS_Math::rad2gra(double a)
74 return (a / (2.0 * M_PI) * 400.0);
78 * Finds greatest common divider using Euclid's algorithm.
80 int RS_Math::findGCD(int a, int b)
93 * Tests if angle a is between a1 and a2. a, a1 and a2 must be in the
94 * range between 0 and 2*PI.
97 * @param reversed true for clockwise testing. false for ccw testing.
98 * @return true if the angle a is between a1 and a2.
100 bool RS_Math::isAngleBetween(double a, double a1, double a2, bool reversed)
111 if (a1 >= a2 - 1.0e-12)
113 if (a >= a1 - 1.0e-12 || a <= a2 + 1.0e-12)
120 if (a >= a1 - 1.0e-12 && a <= a2 + 1.0e-12)
126 //RS_DEBUG->print("angle %f is %sbetween %f and %f",
127 // a, ret ? "" : "not ", a1, a2);
132 * Corrects the given angle to the range of 0-2*Pi.
134 double RS_Math::correctAngle(double a)
146 * @return The angle that needs to be added to a1 to reach a2.
147 * Always positive and less than 2*pi.
149 double RS_Math::getAngleDifference(double a1, double a2)
165 * Makes a text constructed with the given angle readable. Used
166 * for dimension texts and for mirroring texts.
168 * @param readable true: make angle readable, false: unreadable
169 * @param corrected Will point to true if the given angle was
170 * corrected, false otherwise.
172 * @return The given angle or the given angle+PI, depending which on
173 * is readable from the bottom or right.
175 double RS_Math::makeAngleReadable(double angle, bool readable, bool * corrected)
178 bool cor = isAngleReadable(angle) ^ readable;
187 if (corrected != NULL)
194 * @return true: if the given angle is in a range that is readable
195 * for texts created with that angle.
197 bool RS_Math::isAngleReadable(double angle)
199 if (angle > M_PI /2.0 * 3.0 + 0.001 || angle < M_PI / 2.0 + 0.001)
206 * @param tol Tolerance in rad.
207 * @retval true The two angles point in the same direction.
209 bool RS_Math::isSameDirection(double dir1, double dir2, double tol)
211 double diff = fabs(dir1 - dir2);
213 if (diff < tol || diff > 2 * M_PI - tol)
215 //std::cout << "RS_Math::isSameDirection: " << dir1 << " and " << dir2
216 // << " point in the same direction" << "\n";
220 //std::cout << "RS_Math::isSameDirection: " << dir1 << " and " << dir2
221 // << " don't point in the same direction" << "\n";
226 * Compares two double values with a tolerance.
228 bool RS_Math::cmpDouble(double v1, double v2, double tol)
230 return (fabs(v2 - v1) < tol);
234 * Evaluates a mathematical expression and returns the result.
235 * If an error occured, the given default value 'def' will be returned.
237 double RS_Math::eval(const QString & expr, double def)
240 double res = RS_Math::eval(expr, &ok);
244 //std::cerr << "RS_Math::evaluate: Parse error at col "
245 //<< ret << ": " << fp.ErrorMsg() << "\n";
253 * Evaluates a mathematical expression and returns the result.
254 * If an error occured, ok will be set to false (if ok isn't NULL).
256 double RS_Math::eval(const QString & expr, bool * ok)
267 fp.AddConstant("pi", M_PI);
269 // replace '14 3/4' with '14+3/4'
276 // int i = s.find(QRegExp("[0-9]* [0-9]*/[0-9]*"));
277 int i = s.indexOf(QRegExp("[0-9]* [0-9]*/[0-9]*"));
281 // int i2 = s.find(' ', i);
282 int i2 = s.indexOf(' ', i);
286 s.replace(i2, 1, "+");
293 int ret = fp.Parse(s.toLatin1().data(), "", true);
306 return fp.Eval(NULL);
310 * Converts a double into a string which is as short as possible
312 * @param value The double value
313 * @param prec Precision e.g. a precision of 1 would mean that a
314 * value of 2.12030 will be converted to "2.1". 2.000 is always just "2").
316 QString RS_Math::doubleToString(double value, double prec)
320 std::cerr << "RS_Math::doubleToString: invalid precision\n";
327 int num = RS_Math::round(value / prec);
329 exaStr = RS_Math::doubleToString(prec, 10);
330 // dotPos = exaStr.find('.');
331 dotPos = exaStr.indexOf('.');
334 ret.sprintf("%d", RS_Math::round(num * prec));
337 int digits = exaStr.length() - dotPos - 1;
338 ret = RS_Math::doubleToString(num * prec, digits);
345 * Converts a double into a string which is as short as possible.
347 * @param value The double value
348 * @param prec Precision
350 QString RS_Math::doubleToString(double value, int prec)
354 valStr.setNum(value, 'f', prec);
356 if (valStr.contains('.'))
358 // Remove zeros at the end:
359 while (valStr.at(valStr.length() - 1) == '0')
360 valStr.truncate(valStr.length() - 1);
362 if (valStr.at(valStr.length() - 1) == '.')
363 valStr.truncate(valStr.length() - 1);
370 * Performs some testing for the math class.
374 std::cout << "RS_Math::test: doubleToString:\n";
377 QString s = RS_Math::doubleToString(v, 0.1);
379 s = RS_Math::doubleToString(v, 0.01);
381 s = RS_Math::doubleToString(v, 0.0);
385 s = RS_Math::doubleToString(v, 0.1);
387 s = RS_Math::doubleToString(v, 0.01);
389 s = RS_Math::doubleToString(v, 0.0);
393 s = RS_Math::doubleToString(v, 0.1);
395 s = RS_Math::doubleToString(v, 0.01);
397 s = RS_Math::doubleToString(v, 0.001);
398 assert(s == "0.001");
399 s = RS_Math::doubleToString(v, 0.0);
402 std::cout << "RS_Math::test: complete\n";