Skip to content

Units

NextFTC has an immutable, type-safe units system that allows you to pass quantities in any unit. It also prevents you from mixing up units accidentally. NextFTC has two types of units: Distance and Angle. All units extend the Quantity abstract class.

Distance

The first unit we'll look at is Distance. There are six units Distance accepts: millimeters, centimeters, meters, inches, feet, and yards. Internally it is stored in millimeters.

Creating a Distance is simple:

kotlin
val millimeters: Distance = 1000.mm
val centimeters: Distance = 100.cm
val meters: Distance = 1.m

val inches: Distance = 36.inches
val feet: Distance = 3.ft
val yards: Distance = 1.yd

You can convert a distance back to a double in any unit:

kotlin
val inch: Distance = 1.inch

val millimeters: Double = inch.inMm // 25.4
val centimeters: Double = inch.inCm // 2.54
val meters: Double = inch.inMeters // 0.0254

val meter: Distance = 1.m

val inches: Double = meter.inIn // 39.37
val feet: Double = meter.inFt // 3.2808
val yards: Double = meter.inYd // 1.0936

All operations are easy to use:

kotlin
val foot = 1.ft
val inch = 1.inch

val sum = foot + inch // 13 in
val difference = foot - inch // 11 in
val quotient = foot / inch // 12

val bigger = foot * 2 // 24 in
val smaller = foot / 2 // 6 in

val positive = +foot // 12 in
val negative = -foot // -12 in
val abs = foot.abs // 12 in
val otherAbs = abs(foot) // also 12 in!
val sign = foot.sign // 1 (1 for positive numbers, 0 for zero, -1 for negative numbers)

val remainder = foot % 5.in // 2 in
val pureRemainder = foot % 5 // also 2 in

foot > inches // true
foot >= inches // true
foot < inches // false
foot <= inches // false
foot == inches // false

val isNaN = foot.isNaN() // false

Angle

Angle is very similar to Distance, but it also has functionality for wrapping and normalizing. Angles can be in radians, degrees, or full revolutions, and are stored internally as radians.

kotlin
val fullCircle = 1.rev
val halfCircle = Math.PI.rad
val quarterCircle = 90.deg

val fullDegrees = fullCircle.inDeg // 360
val halfRevolution = halfCircle.inRev // 0.5
val quarterRadians = quarterCircle.inRad // pi/2

You can also wrap and normalize angles. Below is a table of what wrapping and normalizing does for angles in different units.

UnitWrappingNormalizing
Revolutions0 to 1-0.5 to 0.5
Radians0 to 2pi-pi to pi
Degrees0 to 360-180 to 180

Here's an example:

kotlin
val bigAngle = 550.deg;
val wrapped = bigAngle.wrapped.inDeg; // 190
val normalized = bigAngle.normalized.inDeg; // -170