Defining custom units
The Unit
system makes it easy to define your own custom Unit
s in a
pre-existing Unit
family.
Lets look at defining our own DistanceUnit
s for use with Distance
s
DistanceUnit
is the unit family that Distance
uses. Its an interface, so we
can implement it on an enum.
All we need to do is define a ratio of this unit, to millimeters.
Essentially: let x
be some value represented in unit u
, r
be unit u
’s
toCommonRatio
, c
be the common unit for the unit family u
belongs to.
Then, x / r
is equal to x
in c
.
For example: DistanceUnits.METER
has a toCommonRatio
of 1000.0
as to
convert Meters to Millimeters, you divide by 1000.
Dairy does this for the enums DistanceUnits
, CurrentUnits
and AngleUnits
.
now:
While FATHOM
and LEAGUE
might be totally useless units, TILE
is genuinely
useful! This season, try doing all your pathplanning using Distance
s using
measurements relative to the size of your practice field’s tiles, then, when you
go to competition, remeasure the size of the tiles, and update a single number
in your code, to instantly remap your auto to the changed tile size. Just
remember to reset it after the event.