XScript Manual · Chapter 31

MATH — math utilities

MATH gathers common math helpers — trig, distance, angle, comparison. Angle-based APIs use degrees by default.

Basic example

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("DIST = {0}", dist);
 
double angle = MATH.CalcAngle(x1, y1, x2, y2);
Log("ANGLE = {0}", angle);
 
double a = 10.2;
double b = 9.3;
double min = MATH.Min(a, b);

Key methods

Trig (degrees)

SignatureDescription
double Sin(double deg)Sine
double Cos(double deg)Cosine
double Tan(double deg)Tangent
double Asin(double value) / Acos(double value) / Atan(double value)Inverse
double Atan2(double y, double x)Quadrant-aware atan

Basic math

SignatureDescription
double Abs(double value)Absolute value
double Sqrt(double value)Square root
double Pow(double base, double exp)Power
double Log10(double value) / Log(double value)Logarithm
double Exp(double value)Exponential
double Round(double value, int digits = 0)Round
double Ceiling(double value) / Floor(double value)Ceil / floor

Comparison / set

SignatureDescription
double Min(double a, double b) / Max(double a, double b)Min / max
double Clamp(double value, double min, double max)Clamp

Geometry

SignatureDescription
double CalcDistance(double x1, double y1, double x2, double y2)Euclidean distance
double CalcAngle(double x1, double y1, double x2, double y2)Angle (°)
double DegToRad(double deg) / RadToDeg(double rad)Convert units

Tips

  • Trig defaults to degrees — convert with DegToRad when needed.
  • Use Clamp for soft limits and speed caps to keep conditions tidy.
  • For float comparisons, prefer MATH.Abs(a - b) < eps over direct equality.

MATH full function reference (XUtilMath)

Sourced from C# — every function callable from script.

Basic / comparison

FunctionMeaning
MATH.Abs(value)absolute value
MATH.Min(a, b) / MATH.Max(a, b)min / max
MATH.Clamp(int, min, max) / (double, min, max)clamp to range
MATH.Floor(value)floor
MATH.Round(value, fix)round to fix decimals

Trigonometry (degrees)

FunctionMeaning
MATH.Sin(deg) / MATH.Cos(deg) / MATH.Tan(deg)sin · cos · tan
MATH.ASin(value) / MATH.ACos(value) / MATH.ATan(value)inverse trig (radians; convert with RadToDeg)
MATH.ATan2(y, x)quadrant-preserving atan
MATH.DegToRad(angle) / MATH.RadToDeg(angle)unit conversion

Geometry

FunctionMeaning
MATH.CalcDistance(x1, y1, x2, y2)distance between two points
MATH.CalcAngle(x1, y1, x2, y2)angle between two points (degrees)
MATH.CalcRotate(cx, cy, sx, sy, angle, ref tx, ref ty)rotate (sx,sy) around (cx,cy) by angle°
MATH.CalcOffsetPoint(cx, cy, sx, sy, offset, ref tx, ref ty)point at distance offset along the line
MATH.TransformTrapezoidToSquare(x1..y4, x, y, ref newX, ref newY)distortion correction (quad → unit square)
MATH.FindCrossPointTwoLines(...)line-line intersection (4 overloads)
MATH.FindIntersection(p1, p2, p3, p4, ...)segment-segment intersection
MATH.FindCircle(a, b, c, out center, out radius)circle through 3 points
MATH.CalcPointDistance(pt1, pt2)distance between two Points
MATH.CalcPointAngle(pt1, pt2)angle between two Points

Linear converter (LCVT)

Linear-interpolated lookup table for unit conversion (encoder pulse ↔ physical unit, voltage ↔ raw, …).

FunctionMeaning
MATH.LCVT_SetName(index, name)set slot name
MATH.LCVT_Clear(index)clear slot
MATH.LCVT_Add(index, x, y, log=true)add a calibration point
MATH.LCVT_GetValueY(index, x, ref y)x → interpolated y
MATH.LCVT_GetValueX(index, y, ref x)y → inverse x