final case class Velocity(direction: Direction, speed: Double) extends Product with Serializable
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._
.
- direction
the direction of movement
- speed
the speed of movement; this cannot be negative
- Alphabetic
- By Inheritance
- Velocity
- Serializable
- Product
- Equals
- AnyRef
- Any
- Hide All
- Show All
- Public
- Protected
Instance Constructors
Value Members
- final def !=(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
- final def ##(): Int
- Definition Classes
- AnyRef → Any
- 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).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.
Sums this velocity with the given one. The sum’s
dx
equals the sum of the twodx
s; the same goes fordy
. - def -(another: Velocity): Velocity
Subtracts the given velocity from this one.
Subtracts the given velocity from this one. The results’s
dx
equals the difference between the twodx
s; the same goes fordy
. - def /(divisor: Double): Velocity
Returns a velocity whose
speed
equals this velocity’s speed divided by the given number (which must be positive).Returns a velocity whose
speed
equals this velocity’s speed divided by the given number (which must be positive). The direction is the same. - final def ==(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
- final def asInstanceOf[T0]: T0
- Definition Classes
- Any
- def changeDirection(newDirection: Direction): Velocity
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.
- def clockwise(degrees: Double): Velocity
Returns a velocity whose direction is the given number of degrees clockwise from this one’s.
Returns a velocity whose direction is the given number of degrees clockwise from this one’s. The speed is the same.
- def clone(): AnyRef
- Attributes
- protected[lang]
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.CloneNotSupportedException]) @native() @HotSpotIntrinsicCandidate()
- def counterclockwise(degrees: Double): Velocity
Returns a velocity whose direction is the given number of degrees counterclockwise from this one’s.
Returns a velocity whose direction is the given number of degrees counterclockwise from this one’s. The speed is the same.
- val direction: Direction
- lazy val dx: Double
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
- final def eq(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
- def faster(addition: Double): Velocity
Returns a velocity whose
speed
equals this velocity’s speed plus the given number.Returns a velocity whose
speed
equals this velocity’s speed plus the given number. The direction is the same. - final def getClass(): Class[_ <: AnyRef]
- Definition Classes
- AnyRef → Any
- Annotations
- @native() @HotSpotIntrinsicCandidate()
- final def isInstanceOf[T0]: Boolean
- Definition Classes
- Any
- def isStill: Boolean
Determines whether the speed of this
Velocity
is precisely zero. - final def ne(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
- def nextFrom(position: Pos): Pos
Returns the
Pos
that an object moving at this velocity reaches in one unit of time.Returns the
Pos
that an object moving at this velocity reaches in one unit of time. That is, adds thedx
anddy
components of this velocity to the givenPos
and returns the result. - def noFasterThan(maxSpeed: Double): Velocity
Returns a velocity whose
speed
is as close to this one’s as possible without exceeding the given number.Returns a velocity whose
speed
is as close to this one’s as possible without exceeding the given number. The direction is the same. - 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.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. - final def notify(): Unit
- Definition Classes
- AnyRef
- Annotations
- @native() @HotSpotIntrinsicCandidate()
- final def notifyAll(): Unit
- Definition Classes
- AnyRef
- Annotations
- @native() @HotSpotIntrinsicCandidate()
- def opposite: Velocity
Returns a velocity that is equal to this one in speed but has the opposite direction.
Returns a velocity that is equal to this one in speed but has the opposite direction. The expressions
myVelocity.opposite
and-myVelocity
are equivalent. - def productElementNames: Iterator[String]
- Definition Classes
- Product
- def roughly: String
Returns a
String
description of theVelocity
.Returns a
String
description of theVelocity
. 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.Returns a velocity whose
speed
equals this velocity’s speed minus the given number. The direction is the same. - val speed: Double
- def stopped: Velocity
Returns a velocity whose direction equals this one’s but whose
speed
is zero. - def switchX: Velocity
Returns a velocity whose
dx
is the opposite of this one’s.Returns a velocity whose
dx
is the opposite of this one’s. Thedy
is the same, as is the speed. - def switchY: Velocity
Returns a velocity whose
dy
is the opposite of this one’s.Returns a velocity whose
dy
is the opposite of this one’s. Thedx
is the same, as is the speed. - final def synchronized[T0](arg0: => T0): T0
- Definition Classes
- AnyRef
- def toPos: Pos
Returns a Pos whose
x
andy
components equal this velocity’sdx
anddy
, respectively. - def toString: String
Returns a
String
description of theVelocity
. - def unary_-: Velocity
Returns a velocity that is equal to this one in speed but has the opposite direction.
Returns a velocity that is equal to this one in speed but has the opposite direction. The expressions
-myVelocity
andmyVelocity.opposite
are equivalent. - final def wait(arg0: Long, arg1: Int): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.InterruptedException])
- final def wait(arg0: Long): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.InterruptedException]) @native()
- final def wait(): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.InterruptedException])
Deprecated Value Members
- def finalize(): Unit
- Attributes
- protected[lang]
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.Throwable]) @Deprecated @deprecated
- Deprecated
(Since version ) see corresponding Javadoc for more information.