XScript Manual · Chapter 31

MATH — math functions

MATH is the built-in object that collects the math functions most used for machine coordinate work. Every function that takes or returns an angle works in degrees. Convert with DegToRad and RadToDeg when you need radians.

Example

xscript
double x1 = 0.0;
double y1 = 0.0;
double x2 = 100.0;
double y2 = 100.0;
 
double dist = MATH.CalcDistance(x1, y1, x2, y2);
Log("[{0},{1}] <--> [{2},{3}] ==> DIST = {4}", x1, y1, x2, y2, dist);
 
double angle = MATH.CalcAngle(x1, y1, x2, y2);
Log("[{0},{1}] <--> [{2},{3}] ==> ANGLE = {4}", x1, y1, x2, y2, angle);
 
double a = 10.2;
double b = 9.3;
double min = MATH.Min(a, b);
Log("[{0}, {1}] ==> Min = {2}", a, b, min);

Methods

Trigonometry & basic operations

MethodDescription
double Sin(double deg) / double Cos(double deg) / double Tan(double deg)Sine · cosine · tangent. deg is an angle in degrees
double ASin(double value) / double ACos(double value) / double ATan(double value)Inverse trigonometric functions. The result is an angle in degrees (e.g. MATH.ACos(0) → 90)
double ATan2(double y, double x)The angle (degrees) of the direction to the point (x, y). Quadrant-aware, from -180 to 180 (e.g. MATH.ATan2(1, -1) → 135)
double Max(double a, double b) / double Min(double a, double b)The larger / smaller of two numbers
double Abs(double value)Absolute value
double RadToDeg(double angle) / double DegToRad(double angle)Radians → degrees / degrees → radians
double Floor(double value)Rounds down — the largest integer not greater than the value (-1.5 → -2)
double Round(double value) 4.0+ / double Round(double value, int fix)Rounds to an integer / to fix decimal places. A value exactly halfway goes to the even neighbour (MATH.Round(2.5) → 2, MATH.Round(3.5) → 4)
int Clamp(int value, int min, int max) / double Clamp(double value, double min, double max)Returns min if the value is below min, max if it is above max, and the value itself when it lies between them

Coordinate maths

MethodDescription
double CalcDistance(double x1, double y1, double x2, double y2)Distance between two points
double CalcAngle(double x1, double y1, double x2, double y2)Angle (degrees) of the line through the two points. The point with the smaller X is taken as the start, so the order of the points does not matter (-90 to 90). 0 when the points are the same
bool CalcRotate(double cx, double cy, double sx, double sy, double angle, ref double tx, ref double ty)Rotates the point (sx, sy) about the pivot (cx, cy) by angle degrees and puts the result in tx, ty. A positive angle turns from the X axis towards the Y axis
bool CalcOffsetPoint(double cx, double cy, double sx, double sy, double offset, ref double tx, ref double ty)Puts in tx, ty the point offset away from (cx, cy) towards (sx, sy). An offset longer than the distance lands on the extension; a negative offset goes the other way. (cx, cy) when the two points are the same
bool FindCrossPointTwoLines(double lineAX, double lineAY, double angleA, double lineBX, double lineBY, double angleB, out double crossPtX, out double crossPtY)Puts in crossPtX, crossPtY the intersection of the line through (lineAX, lineAY) at angleA degrees and the line through (lineBX, lineBY) at angleB degrees. false when they are parallel
bool FindCrossPointTwoLines(double lineA1X, double lineA1Y, double lineA2X, double lineA2Y, double lineB1X, double lineB1Y, double lineB2X, double lineB2Y, out double crossPtX, out double crossPtY)Intersection of the line through points A1·A2 and the line through points B1·B2. Returns the intersection of the extended lines even when the two segments do not touch. false when they are parallel

Note Pass a variable to a ref or out parameter; the result is in that variable when the function returns. Do not write ref at the call; marking the argument with a /*ref*/ comment is recommended, as in MATH.CalcRotate(cx, cy, sx, sy, 90, /*ref*/tx, /*ref*/ty) — see Functions.

Linear converter (LCVT)

MethodDescription
bool LCVT_SetName(int index, string name)Sets the name of the linear converter object
bool LCVT_Clear(int index)Clears the linear converter
bool LCVT_Add(int index, double x, double y, bool log=true)Adds linear converter data. log sets whether to log
bool LCVT_GetValueY(int index, double x, ref double y)Gets the Y value for x
bool LCVT_GetValueX(int index, double y, ref double x)Gets the X value for y

Caution In QMS 4.0.23, calling a MATH.LCVT_* function with object 0 to 999 fails at run time because the converter objects are never created. For the same feature (piecewise linear conversion) use SYS.LINEAR_ClearData · SYS.LINEAR_AddData · SYS.LINEAR_GetValueY · SYS.LINEAR_GetValueX — see the SYS chapter.

Properties

PropertyTypeDescription
M_PIdoublepi (3.14159…)
M_PI_2doublepi / 2.0 (π/2)
M_2_PIdoublepi × 2.0 (2π)
M_Edoublee (2.718…)

Properties are read without parentheses — double full = MATH.M_2_PI;

Subscribe