Velocity

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.*.

Value parameters:
direction

the direction of movement

speed

the speed of movement; this cannot be negative

Companion:
object
trait Product
trait Equals
class Object
trait Matchable
class Any

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.

def +(another: Velocity): Velocity

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

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

def -(another: Velocity): Velocity

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

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

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.

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.

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.

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.

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.

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.

Determines whether the speed of this Velocity is precisely zero.

Determines whether the speed of this Velocity is precisely zero.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

See also:
Definition Classes

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.

Inherited methods

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

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