QCAD
Open Source 2D CAD
RMath Class Reference

Math functions. More...

#include <RMath.h>

Static Public Member Functions

static int absmod (int a, int b)
 
static QString angleToString (double a)
 Converts the given angle in rad to the given angle format. More...
 
static bool containsFuzzy (const QList< double > &values, double v, double tol=RS::PointTolerance)
 
static double deg2rad (double a)
 Converts degrees to radians. More...
 
static double eval (const QString &expression, bool *ok=NULL)
 Evaluates the given mathematical expression and returns the result. More...
 
static bool fuzzyAngleCompare (double v1, double v2, double tolerance=RS::AngleTolerance)
 
static bool fuzzyCompare (double v1, double v2, double tolerance=RS::PointTolerance)
 
static double getAngleDifference (double a1, double a2)
 
static double getAngleDifference180 (double a1, double a2)
 
static void getBiQuadRoots (double p[], double r[][5])
 
static void getCubicRoots (double p[], double r[][5])
 
static QString getError ()
 
static int getGcd (int a, int b)
 Finds greatest common divider using Euclid's algorithm. More...
 
static double getNormalizedAngle (double a)
 Gets the normalized angle from a. More...
 
static void getQuadRoots (double p[], double r[][5])
 
static double getRelativeAngle (double a, double baseAngle)
 
static double gra2deg (double a)
 Converts grads to degrees. More...
 
static bool hasError ()
 
static void init ()
 
static bool isAngleBetween (double a, double a1, double a2, bool reversed)
 Tests if angle a is between a1 and a2. More...
 
static bool isAngleReadable (double angle, double tolerance=0.01)
 
static bool isBetween (double value, double limit1, double limit2, bool inclusive, double tolerance=RS::PointTolerance)
 
static bool isInf (double v)
 
static bool isNaN (double v)
 
static bool isNormal (double v)
 
static bool isSameDirection (double dir1, double dir2, double tol=RS::AngleTolerance)
 Tests if two angels point approximately in the same direction. More...
 
static bool isSane (double v)
 
static double makeAngleReadable (double angle, bool readable=true, bool *corrected=NULL)
 Adds 180° to the given angle if a text constructed with that angle otherwise wouldn't be readable. More...
 
static int mround (double v)
 Rounds the given double to the next int. More...
 
static RVector parseCoordinate (const QString &coordinateString, const RVector &relativeZero=RVector::nullVector)
 Parses the given string as coordinate. More...
 
static double parseScale (const QString &scaleString)
 
static double pow (double x, double y)
 Safe pow function. More...
 
static double rad2deg (double a)
 Converts radians to degrees. More...
 
static double rad2gra (double a)
 Converts radians to gradians. More...
 
static void simplify (int numerator, int denominator, int &numeratorRes, int &denominatorRes)
 
template<class T >
static void swap (T &a, T &b)
 Swaps two given values. More...
 
static void toFraction (double v, int maxDenominator, int &number, int &numerator, int &denominator)
 
static QString toFractionString (double v, int maxDenominator)
 
static QString trimTrailingZeroes (const QString &s)
 
static double trunc (double v)
 
static void uninit ()
 

Static Private Attributes

static QString lastError = ""
 Copyright (c) 2011-2018 by Andrew Mustun. More...
 
static QString mathExt = QString()
 

Detailed Description

Math functions.

Scriptable:\nThis class is available in script environments.\n

Member Function Documentation

◆ absmod()

int RMath::absmod ( int  a,
int  b 
)
static
Returns
Absolute modulus.

◆ angleToString()

QString RMath::angleToString ( double  a)
static

Converts the given angle in rad to the given angle format.

Todo:
Support different formats.

◆ containsFuzzy()

bool RMath::containsFuzzy ( const QList< double > &  values,
double  v,
double  tol = RS::PointTolerance 
)
static

◆ deg2rad()

double RMath::deg2rad ( double  a)
static

Converts degrees to radians.

Parameters
aangle in degrees

◆ eval()

double RMath::eval ( const QString &  expression,
bool *  ok = NULL 
)
static

Evaluates the given mathematical expression and returns the result.

Angles may be expressed in degrees (default), rad (#r), gon (#g) or as surveyors angles (N::d#'#"E).

◆ fuzzyAngleCompare()

bool RMath::fuzzyAngleCompare ( double  v1,
double  v2,
double  tolerance = RS::AngleTolerance 
)
static

◆ fuzzyCompare()

bool RMath::fuzzyCompare ( double  v1,
double  v2,
double  tolerance = RS::PointTolerance 
)
static
Returns
True if the difference between v1 and v2 is less then the given tolerance.

◆ getAngleDifference()

double RMath::getAngleDifference ( double  a1,
double  a2 
)
static
Parameters
a1first angle in rad
a2second angle in rad
Returns
The angle that needs to be added to a1 to reach a2. Always positive and less than 2*pi.

◆ getAngleDifference180()

double RMath::getAngleDifference180 ( double  a1,
double  a2 
)
static
Parameters
a1first angle in rad
a2s second angle in rad
Returns
The angle that needs to be added to a1 to reach a2. Always between -pi and pi.

◆ getBiQuadRoots()

void RMath::getBiQuadRoots ( double  p[],
double  r[][5] 
)
static
Non-Scriptable:\nThis function is not available in script environments.\n

◆ getCubicRoots()

void RMath::getCubicRoots ( double  p[],
double  r[][5] 
)
static
Non-Scriptable:\nThis function is not available in script environments.\n

◆ getError()

QString RMath::getError ( )
static
Returns
Last error encountered by a math function.

◆ getGcd()

int RMath::getGcd ( int  a,
int  b 
)
static

Finds greatest common divider using Euclid's algorithm.

See also
http://en.wikipedia.org/wiki/Greatest_common_divisor
Parameters
athe first number
bthe second number
Returns
The greatest common divisor of a and b.

◆ getNormalizedAngle()

double RMath::getNormalizedAngle ( double  a)
static

Gets the normalized angle from a.

Used to make sure that an angle is in the range between 0 and 2 pi.

Parameters
athe unnormalized angle, e.g. 8
Returns
The angle a normalized to the range of $ 0 \ldots 2\pi $, e.g. normalized angle from 8 is 1.716.

◆ getQuadRoots()

void RMath::getQuadRoots ( double  p[],
double  r[][5] 
)
static
Non-Scriptable:\nThis function is not available in script environments.\n

◆ getRelativeAngle()

double RMath::getRelativeAngle ( double  a,
double  baseAngle 
)
static
Returns
Angle a as angle relative to baseAngle. Result is in range -PI < result < PI.

◆ gra2deg()

double RMath::gra2deg ( double  a)
static

Converts grads to degrees.

Parameters
aangle in grad (gon)

◆ hasError()

bool RMath::hasError ( )
static
Returns
True if an error was encountered since the last check.

◆ init()

static void RMath::init ( )
inlinestatic

◆ isAngleBetween()

bool RMath::isAngleBetween ( double  a,
double  a1,
double  a2,
bool  reversed 
)
static

Tests if angle a is between a1 and a2.

a, a1 and a2 must be in the range between 0 and 2*PI. All angles in rad.

Parameters
athe test angle
a1the lower limiting angle
a2the upper limiting angle
reversedTrue for clockwise testing. False for ccw testing.
Returns
true if the angle a is between a1 and a2.

◆ isAngleReadable()

bool RMath::isAngleReadable ( double  angle,
double  tolerance = 0.01 
)
static
Parameters
angleThe text angle in rad
toleranceThe tolerance by which the angle still maybe in the unreadable range.
Returns
true: If the given angle is in a range that is readable for texts created with that angle.

◆ isBetween()

bool RMath::isBetween ( double  value,
double  limit1,
double  limit2,
bool  inclusive,
double  tolerance = RS::PointTolerance 
)
static
Returns
True if the given value is between the given limits.
Parameters
inclusiveTrue to accept values close to the limits within the given tolerance.

◆ isInf()

bool RMath::isInf ( double  v)
static
Returns
true if v is Inf.

◆ isNaN()

bool RMath::isNaN ( double  v)
static
Returns
true if v is NaN.

◆ isNormal()

bool RMath::isNormal ( double  v)
static
Returns
true if v is non NaN and not Inf.

◆ isSameDirection()

bool RMath::isSameDirection ( double  dir1,
double  dir2,
double  tolerance = RS::AngleTolerance 
)
static

Tests if two angels point approximately in the same direction.

Parameters
dir1first direction
dir2second direction
toleranceTolerance in rad.
Return values
trueThe two angles point in the same direction.
falseThe difference between the two angles is at least tolerance radians.

◆ isSane()

bool RMath::isSane ( double  v)
static

◆ makeAngleReadable()

double RMath::makeAngleReadable ( double  angle,
bool  readable = true,
bool *  corrected = NULL 
)
static

Adds 180° to the given angle if a text constructed with that angle otherwise wouldn't be readable.

Used for dimension texts and for mirroring texts.

Parameters
anglethe original angle
readabletrue: make angle readable, false: unreadable
correctedPointer to boolean that will point to true if the given angle was corrected, false otherwise, or null.
Returns
The given angle or the given angle + pi, depending which one is readable from the bottom or right.

◆ mround()

static int RMath::mround ( double  v)
inlinestatic

Rounds the given double to the next int.

◆ parseCoordinate()

RVector RMath::parseCoordinate ( const QString &  coordinateString,
const RVector relativeZero = RVector::nullVector 
)
static

Parses the given string as coordinate.

Parameters
relativeZeroRVector position of relative zero point.
strString to parse (e.g. "34,15.6" or "@10,30" or "50<60", ...)
Returns
undefined if the string is not a coordinate, an invalid RVector if str is an invalid coordinate or the RVector result.

◆ parseScale()

double RMath::parseScale ( const QString &  scaleString)
static
Returns
Value given as drawing scale (#:#).

◆ pow()

double RMath::pow ( double  x,
double  y 
)
static

Safe pow function.

Parameters
xthe base
ythe exponent
Returns
The value of x raised to the power of y.

◆ rad2deg()

double RMath::rad2deg ( double  a)
static

Converts radians to degrees.

Parameters
aangle in radians

◆ rad2gra()

double RMath::rad2gra ( double  a)
static

Converts radians to gradians.

Parameters
aangle in radians

◆ simplify()

void RMath::simplify ( int  numerator,
int  denominator,
int &  numeratorRes,
int &  denominatorRes 
)
static
Returns
Simplified fraction for the given fraction (numerator/denomiator).

◆ swap()

template<class T >
static void RMath::swap ( T &  a,
T &  b 
)
inlinestatic

Swaps two given values.

Non-Scriptable:\nThis function is not available in script environments.\n

◆ toFraction()

void RMath::toFraction ( double  v,
int  maxDenominator,
int &  number,
int &  numerator,
int &  denominator 
)
static
Returns
Representation of the given number as fraction (number numerator/denominator). Rounding occurs to satisfy the use of maxDenominator as maximum value for denominator.

◆ toFractionString()

QString RMath::toFractionString ( double  v,
int  maxDenominator 
)
static
Returns
String representing the given number, expressed as fraction (# #/#).

◆ trimTrailingZeroes()

QString RMath::trimTrailingZeroes ( const QString &  s)
static

◆ trunc()

double RMath::trunc ( double  v)
static
Returns
v truncated (cut off) at decimal point.

◆ uninit()

static void RMath::uninit ( )
inlinestatic

Member Data Documentation

◆ lastError

QString RMath::lastError = ""
staticprivate

Copyright (c) 2011-2018 by Andrew Mustun.

All rights reserved.

This file is part of the QCAD project.

QCAD 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 3 of the License, or (at your option) any later version.

QCAD 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 QCAD. Last error encountered.

◆ mathExt

QString RMath::mathExt = QString()
staticprivate

The documentation for this class was generated from the following files: