Vectors are a useful multidimensional data structure.

Dairy supports DoubleVector2D and DistanceVector2D.

We’ll look at DistanceVector2D, but DoubleVector2D is super similar, outside of a couple of Distance specific utility methods.

DistanceVector2D vec = new DistanceVector2D(Distances.mm(10), Distances.mm(10));
// OR
DistanceVector2Ds.millimeterVector(10.0, 10.0);

Distance x = vec.getX();
Distance y = vec.getY();

Distance mag = vec.getMagnitude();
// theta is always an `Angle`
Angle theta = vec.getTheta();

Thats the basics! In Kotlin, we also get that cool destructuring to x and y.

Vectors also have all expected mathematical operations defined on them: plus, minus, unaryPlus, unaryMinus, times by other vector, times by scalar, div by other vector, div by scalar. Take a look back at Distances in the Unit section to review this. The only operation not defined here is mod or rem.

Vectors also get these vector specific methods:

// normalise gives us a new vector with the specified length
vec.normalise(); // defaults to 1 mm / 1.0
vec.normalise(Distances.mm(20));

// the scalar dot product
vec.dot(new DistanceVector2D());

// rotate
vec.rotate(Angles.wrappedDeg(90));

DistanceVector2Ds also have utilties for working with the DistanceUnits of the Distances it stores.

// converts x to FOOT, y to CENTIMETER
vec.into(DistanceUnits.FOOT, DistanceUnits.CENTIMETER);
// converts both x and y to FOOT
vec.into(DistanceUnits.FOOT);

vec.intoMillimeters();
vec.intoCentimeters();
vec.intoMeters();
vec.intoInches();
vec.intoFeet();