O1Library Docs
Fall 2023
Docs API

# Velocity

o1.world.Velocity
See the Velocity companion object
final case class Velocity(direction: Direction, speed: Double)

Each instance of this class represents movement in the context of a two-dimensional plane. A velocity is a combination of a Direction of movement with a speed (a non-negative `Double`).

Another way to think about a `Velocity` is to divide it in two components: `dx` and `dy`. `dx` indicates the amount (and direction) of movement along the x axis; `dy` is the same for the y axis. The x coordinate increases rightwards and y downwards. For instance, a velocity with a `dx` of 10 and a `dy` of zero indicates rightward movement, and a velocity with a `dx` of -100 and a `dy` of -100 indicates faster movement leftwards and upwards.

There are many ways to create a `Velocity` using the methods on the companion object. Among other things, you can:

- construct a velocity from a direction and a (positive or zero) speed, as in `Velocity(Direction.Left, 50)`;
- construct a direction from `dx` and `dy`, as in `Velocity(-100, 50)`; or
- determine the velocity needed to go from one Pos to another in a single unit of time, as in `Velocity.between(pos1, pos2)`.

Many of the methods on `Velocity` objects also create and return new velocities.

`Velocity` objects are immutable.

This class has an alias in the top-level package o1, so it’s accessible to students simply via `import o1.*`.

## Parameters

direction

  the direction of movement

speed

  the speed of movement; this cannot be negative

## Attributes

Companion
  object
Graph
Supertypes
  trait Serializable
  trait Product
  trait Equals
  class Object
  trait Matchable
  class Any
  Show all

## Members list

### Value members

#### Concrete methods

def *(multiplier: Double): Velocity

Returns a velocity whose `speed` equals this velocity’s speed multiplied by the given number (which must be positive or zero). The direction is the same.

Returns a velocity whose `speed` equals this velocity’s speed multiplied by the given number (which must be positive or zero). The direction is the same.

## Attributes

def +(another: Velocity): Velocity

Sums this velocity with the given one. The sum’s `dx` equals the sum of the two `dx`s; the same goes for `dy`.

Sums this velocity with the given one. The sum’s `dx` equals the sum of the two `dx`s; the same goes for `dy`.

## Attributes

def -(another: Velocity): Velocity

Subtracts the given velocity from this one. The results’s `dx` equals the difference between the two `dx`s; the same goes for `dy`.

Subtracts the given velocity from this one. The results’s `dx` equals the difference between the two `dx`s; the same goes for `dy`.

## Attributes

def /(divisor: Double): Velocity

Returns a velocity whose `speed` equals this velocity’s speed divided by the given number (which must be positive). The direction is the same.

Returns a velocity whose `speed` equals this velocity’s speed divided by the given number (which must be positive). The direction is the same.

## Attributes

def changeDirection(newDirection: Direction): Velocity

Returns a velocity that has the given direction and the same speed as this one.

Returns a velocity that has the given direction and the same speed as this one.

## Attributes

def changeSpeed(newSpeed: Double): Velocity

Returns a velocity that has the given speed and the same direction as this one.

Returns a velocity that has the given speed and the same direction as this one.

## Attributes

def clockwise(degrees: Double): Velocity

Returns a velocity whose direction is the given number of degrees clockwise from this one’s. The speed is the same.

Returns a velocity whose direction is the given number of degrees clockwise from this one’s. The speed is the same.

## Attributes

def counterclockwise(degrees: Double): Velocity

Returns a velocity whose direction is the given number of degrees counterclockwise from this one’s. The speed is the same.

Returns a velocity whose direction is the given number of degrees counterclockwise from this one’s. The speed is the same.

## Attributes

def faster(addition: Double): Velocity

Returns a velocity whose `speed` equals this velocity’s speed plus the given number. The direction is the same.

Returns a velocity whose `speed` equals this velocity’s speed plus the given number. The direction is the same.

## Attributes

def isStill: Boolean

Determines whether the speed of this `Velocity` is precisely zero.

Determines whether the speed of this `Velocity` is precisely zero.

## Attributes

def nextFrom(position: Pos): Pos

Returns the `Pos` that an object moving at this velocity reaches in one unit of time. That is, adds the `dx` and `dy` components of this velocity to the given `Pos` and returns the result.

Returns the `Pos` that an object moving at this velocity reaches in one unit of time. That is, adds the `dx` and `dy` components of this velocity to the given `Pos` and returns the result.

## Attributes

infix def noFasterThan(maxSpeed: Double): Velocity

Returns a velocity whose `speed` is as close to this one’s as possible without exceeding the given number. The direction is the same.

Returns a velocity whose `speed` is as close to this one’s as possible without exceeding the given number. The direction is the same.

## Attributes

infix def noSlowerThan(minSpeed: Double): Velocity

Returns a velocity whose `speed` is as close to this one’s as possible without being lower than the given number. The direction is the same.

Returns a velocity whose `speed` is as close to this one’s as possible without being lower than the given number. The direction is the same.

## Attributes

def opposite: Velocity

Returns a velocity that is equal to this one in speed but has the opposite direction. The expressions `myVelocity.opposite` and `-myVelocity` are equivalent.

Returns a velocity that is equal to this one in speed but has the opposite direction. The expressions `myVelocity.opposite` and `-myVelocity` are equivalent.

## Attributes

def roughly: String

Returns a `String` description of the `Velocity`. E.g., `"111.80 with direction dx=0.89,dy=-0.45"`. Uses two decimals for each number.

Returns a `String` description of the `Velocity`. E.g., `"111.80 with direction dx=0.89,dy=-0.45"`. Uses two decimals for each number.

## Attributes

def slower(reduction: Double): Velocity

Returns a velocity whose `speed` equals this velocity’s speed minus the given number. The direction is the same.

Returns a velocity whose `speed` equals this velocity’s speed minus the given number. The direction is the same.

## Attributes

def stopped: Velocity

Returns a velocity whose direction equals this one’s but whose `speed` is zero.

Returns a velocity whose direction equals this one’s but whose `speed` is zero.

## Attributes

def switchX: Velocity

Returns a velocity whose `dx` is the opposite of this one’s. The `dy` is the same, as is the speed.

Returns a velocity whose `dx` is the opposite of this one’s. The `dy` is the same, as is the speed.

## Attributes

def switchY: Velocity

Returns a velocity whose `dy` is the opposite of this one’s. The `dx` is the same, as is the speed.

Returns a velocity whose `dy` is the opposite of this one’s. The `dx` is the same, as is the speed.

## Attributes

def toPos: Pos

Returns a Pos whose `x` and `y` components equal this velocity’s `dx` and `dy`, respectively.

Returns a Pos whose `x` and `y` components equal this velocity’s `dx` and `dy`, respectively.

## Attributes

override def toString: String

Returns a `String` description of the `Velocity`. The format varies depending on the velocity’s direction.

Returns a `String` description of the `Velocity`. The format varies depending on the velocity’s direction.

## Attributes

See also

  roughly

Definition Classes
  Any

def unary_-: Velocity

Returns a velocity that is equal to this one in speed but has the opposite direction. The expressions `-myVelocity` and `myVelocity.opposite` are equivalent.

Returns a velocity that is equal to this one in speed but has the opposite direction. The expressions `-myVelocity` and `myVelocity.opposite` are equivalent.

## Attributes

#### Inherited methods

def productElementNames: Iterator[String]

## Attributes

Inherited from:
  Product

def productIterator: Iterator[Any]

## Attributes

Inherited from:
  Product

#### Concrete fields

lazy val dx: Double

the amount of change in the x coordinate when moving at this velocity for one unit of time

the amount of change in the x coordinate when moving at this velocity for one unit of time

## Attributes

lazy val dy: Double

the amount of change in the y coordinate when moving at this velocity for one unit of time

the amount of change in the y coordinate when moving at this velocity for one unit of time

## Attributes