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
anddy
, as inVelocity(-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
Value members
Concrete methods
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.
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
.
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
.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.