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. :-)
20 * Rounds the given double to the next int.
22 int RS_Math::round(double v)
24 return (v - floor(v) < 0.5 ? (int)floor(v) : (int)ceil(v));
30 double RS_Math::pow(double x, double y)
33 double ret = ::pow(x, y);
37 RS_DEBUG->print(RS_Debug::D_ERROR, "RS_Math::pow: EDOM in pow");
40 else if (errno == ERANGE)
42 RS_DEBUG->print(RS_Debug::D_WARNING, "RS_Math::pow: ERANGE in pow");
50 * Converts radians to degrees.
52 double RS_Math::rad2deg(double a)
54 return (a / (2.0 * M_PI) * 360.0);
58 * Converts degrees to radians.
60 double RS_Math::deg2rad(double a)
62 return ((a / 360.0) * (2.0 * M_PI));
66 * Converts radians to gradians.
68 double RS_Math::rad2gra(double a)
70 return (a / (2.0 * M_PI) * 400.0);
74 * Finds greatest common divider using Euclid's algorithm.
76 int RS_Math::findGCD(int a, int b)
89 * Tests if angle a is between a1 and a2. a, a1 and a2 must be in the
90 * range between 0 and 2*PI.
93 * @param reversed true for clockwise testing. false for ccw testing.
94 * @return true if the angle a is between a1 and a2.
96 bool RS_Math::isAngleBetween(double a, double a1, double a2, bool reversed)
107 if (a1 >= a2 - 1.0e-12)
109 if (a >= a1 - 1.0e-12 || a <= a2 + 1.0e-12)
116 if (a >= a1 - 1.0e-12 && a <= a2 + 1.0e-12)
122 //RS_DEBUG->print("angle %f is %sbetween %f and %f",
123 // a, ret ? "" : "not ", a1, a2);
128 * Corrects the given angle to the range of 0-2*Pi.
130 double RS_Math::correctAngle(double a)
142 * @return The angle that needs to be added to a1 to reach a2.
143 * Always positive and less than 2*pi.
145 double RS_Math::getAngleDifference(double a1, double a2)
161 * Makes a text constructed with the given angle readable. Used
162 * for dimension texts and for mirroring texts.
164 * @param readable true: make angle readable, false: unreadable
165 * @param corrected Will point to true if the given angle was
166 * corrected, false otherwise.
168 * @return The given angle or the given angle+PI, depending which on
169 * is readable from the bottom or right.
171 double RS_Math::makeAngleReadable(double angle, bool readable, bool * corrected)
174 bool cor = isAngleReadable(angle) ^ readable;
183 if (corrected != NULL)
190 * @return true: if the given angle is in a range that is readable
191 * for texts created with that angle.
193 bool RS_Math::isAngleReadable(double angle)
195 if (angle > M_PI /2.0 * 3.0 + 0.001 || angle < M_PI / 2.0 + 0.001)
202 * @param tol Tolerance in rad.
203 * @retval true The two angles point in the same direction.
205 bool RS_Math::isSameDirection(double dir1, double dir2, double tol)
207 double diff = fabs(dir1 - dir2);
209 if (diff < tol || diff > 2 * M_PI - tol)
211 //std::cout << "RS_Math::isSameDirection: " << dir1 << " and " << dir2
212 // << " point in the same direction" << "\n";
216 //std::cout << "RS_Math::isSameDirection: " << dir1 << " and " << dir2
217 // << " don't point in the same direction" << "\n";
222 * Compares two double values with a tolerance.
224 bool RS_Math::cmpDouble(double v1, double v2, double tol)
226 return (fabs(v2 - v1) < tol);
230 * Evaluates a mathematical expression and returns the result.
231 * If an error occured, the given default value 'def' will be returned.
233 double RS_Math::eval(const QString & expr, double def)
236 double res = RS_Math::eval(expr, &ok);
240 //std::cerr << "RS_Math::evaluate: Parse error at col "
241 //<< ret << ": " << fp.ErrorMsg() << "\n";
249 * Evaluates a mathematical expression and returns the result.
250 * If an error occured, ok will be set to false (if ok isn't NULL).
252 //double RS_Math::eval(const QString& expr, bool* ok);
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)
260 #ifndef RS_NO_FPARSER
270 fp.AddConstant("pi", M_PI);
272 // replace '14 3/4' with '14+3/4'
279 // int i = s.find(QRegExp("[0-9]* [0-9]*/[0-9]*"));
280 int i = s.indexOf(QRegExp("[0-9]* [0-9]*/[0-9]*"));
284 // int i2 = s.find(' ', i);
285 int i2 = s.indexOf(' ', i);
289 s.replace(i2, 1, "+");
296 int ret = fp.Parse(s.toLatin1().data(), "", true);
309 return fp.Eval(NULL);
311 //std::cerr << "RS_Math::eval: No FParser support compiled in.\n";
312 return expr.toDouble();
317 * Converts a double into a string which is as short as possible
319 * @param value The double value
320 * @param prec Precision e.g. a precision of 1 would mean that a
321 * value of 2.12030 will be converted to "2.1". 2.000 is always just "2").
323 QString RS_Math::doubleToString(double value, double prec)
327 std::cerr << "RS_Math::doubleToString: invalid precision\n";
334 int num = RS_Math::round(value / prec);
336 exaStr = RS_Math::doubleToString(prec, 10);
337 // dotPos = exaStr.find('.');
338 dotPos = exaStr.indexOf('.');
341 ret.sprintf("%d", RS_Math::round(num * prec));
344 int digits = exaStr.length() - dotPos - 1;
345 ret = RS_Math::doubleToString(num * prec, digits);
352 * Converts a double into a string which is as short as possible.
354 * @param value The double value
355 * @param prec Precision
357 QString RS_Math::doubleToString(double value, int prec)
361 valStr.setNum(value, 'f', prec);
363 if (valStr.contains('.'))
365 // Remove zeros at the end:
366 while (valStr.at(valStr.length() - 1) == '0')
368 valStr.truncate(valStr.length() - 1);
371 if (valStr.at(valStr.length() - 1) == '.')
373 valStr.truncate(valStr.length() - 1);
381 * Performs some testing for the math class.
385 std::cout << "RS_Math::test: doubleToString:\n";
388 QString s = RS_Math::doubleToString(v, 0.1);
390 s = RS_Math::doubleToString(v, 0.01);
392 s = RS_Math::doubleToString(v, 0.0);
396 s = RS_Math::doubleToString(v, 0.1);
398 s = RS_Math::doubleToString(v, 0.01);
400 s = RS_Math::doubleToString(v, 0.0);
404 s = RS_Math::doubleToString(v, 0.1);
406 s = RS_Math::doubleToString(v, 0.01);
408 s = RS_Math::doubleToString(v, 0.001);
409 assert(s == "0.001");
410 s = RS_Math::doubleToString(v, 0.0);
413 std::cout << "RS_Math::test: complete\n";