> ## Documentation Index
> Fetch the complete documentation index at: https://docs.dairy.foundation/llms.txt
> Use this file to discover all available pages before exploring further.

# Vectors

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.

<CodeGroup>
  ```java Java theme={null}
  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();
  ```

  ```kt Kotlin theme={null}
  val vec = DistanceVector2D(10.mm, 10.mm)
  // OR
  millimeterVector(10.0, 10.0)

  vec.x
  vec.y

  val (x: Distance, y: Distance) = vec

  val mag: Distance = vec.magnitude
  // theta is always an `Angle`
  val theta: Angle = vec.theta
  ```
</CodeGroup>

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 `Distance`s in the `Unit`
section to review this. The only operation not defined here is `mod` or `rem`.

Vectors also get these vector specific methods:

<CodeGroup>
  ```java Java theme={null}
  // 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));
  ```

  ```kt Kotlin theme={null}
  // normalise gives us a new vector with the specified length
  vec.normalise() // defaults to 1 mm / 1.0
  vec.normalise(20.mm)

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

  // rotate
  vec.rotate(90.wrappedDeg)
  ```
</CodeGroup>

`DistanceVector2D`s also have utilties for working with the `DistanceUnit`s of
the `Distance`s it stores.

<CodeGroup>
  ```java Java theme={null}
  // 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();
  ```

  ```kt Kotlin theme={null}
  // 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()
  ```
</CodeGroup>
