sealed case class Direction extends Product with Serializable
Each instance of this class represents a direction on a two-dimensional plane. A direction
consists of two components: dx and dy, each of which is a number between -1.0 and +1.0
(inclusive). dx indicates the degree to which the x coordinate changes when moving in that
direction; dy is the same for the y coordinate. The x coordinate increases rightwards and
y downwards.
For example, moving straight rightwards can be thought of a moving in a Direction with a
dx of +1.0 and a dy of 0.0; by comparison, the upward direction would have a dx of 0.0
and a dy of -1.0. . Diagonals have non-zero values for both dx and dy.
The dx and dy of any Direction always sum up to (roughly) 1.0, with the exception of the
special value NoDirection which represents the lack of a direction
and as zero for both components. (In this, a Direction is different than a Velocity,
which is a combination of a direction of movement and a speed.)
You don’t instantiate Direction directly; instead, you create Directions with the methods
on the companion object. Among other things, you can:
- construct a direction from two differences between
coordinates, as in
Direction.fromDeltas(-100, 50); or - create a direction that corresponds to a given angle, as
in
Direction.fromDegrees(60)andDirection.fromRadians(-scala.math.Pi / 4).
Some of the methods of a Direction object also create and return new Directions, as does
the directionOf method on Pos objects. Moreover, the companion object of this class has a few predefined Direction constants: Up, Down, Left, Right,
and NoDirection.
Direction objects are immutable.
This class has an alias in the top-level package o1, so it’s accessible to students
simply via import o1._.
- Alphabetic
- By Inheritance
- Direction
- Serializable
- Product
- Equals
- AnyRef
- Any
- Hide All
- Show All
- Public
- Protected
Value Members
- final def !=(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
- final def ##(): Int
- Definition Classes
- AnyRef → Any
- def +(another: Direction): Direction
Returns a direction that is, clockwise, removed from this direction by a number of degrees specified by the given direction.
Returns a direction that is, clockwise, removed from this direction by a number of degrees specified by the given direction. Calling
dir1 + dir2is equivalent to callingdir1.counterclockwise(dir2.toDegrees) - final def ==(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
- final def asInstanceOf[T0]: T0
- Definition Classes
- Any
- def clockwise(degrees: Double): Direction
Returns a direction that is removed from this direction by the given number of degrees clockwise.
Returns a direction that is removed from this direction by the given number of degrees clockwise. For instance,
clockwise(90)produces a direction at a right angle to the original. - def clone(): AnyRef
- Attributes
- protected[lang]
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.CloneNotSupportedException]) @native() @HotSpotIntrinsicCandidate()
- def counterclockwise(degrees: Double): Direction
Returns a direction that is removed from this direction by the given number of degrees counterclockwise.
Returns a direction that is removed from this direction by the given number of degrees counterclockwise. For instance,
counterclockwise(90)produces a direction at a right angle to the original. - val dx: Double
- val dy: Double
- final def eq(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
- final def getClass(): Class[_ <: AnyRef]
- Definition Classes
- AnyRef → Any
- Annotations
- @native() @HotSpotIntrinsicCandidate()
- def isDownward: Boolean
Returns
trueif this direction is a clearly downward one (with adyover zero) or at least non-upward (with adyof exactly zero).Returns
trueif this direction is a clearly downward one (with adyover zero) or at least non-upward (with adyof exactly zero). Only returnsfalseifdyis less than zero. - final def isInstanceOf[T0]: Boolean
- Definition Classes
- Any
- def isLeftward: Boolean
Returns
trueif this direction is a clearly leftward one (with adxunder zero) or at least non-rightward (with adxof exactly zero).Returns
trueif this direction is a clearly leftward one (with adxunder zero) or at least non-rightward (with adxof exactly zero). Only returnsfalseifdxis over zero. - def isRightward: Boolean
Returns
trueif this direction is a clearly rightward one (with adxover zero) or at least non-leftward (with adxof exactly zero).Returns
trueif this direction is a clearly rightward one (with adxover zero) or at least non-leftward (with adxof exactly zero). Only returnsfalseifdxis less than zero. - def isUpward: Boolean
Returns
trueif this direction is a clearly upward one (with adyunder zero) or at least non-downward (with adyof exactly zero).Returns
trueif this direction is a clearly upward one (with adyunder zero) or at least non-downward (with adyof exactly zero). Only returnsfalseifdyis over zero. - final def ne(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
- final def notify(): Unit
- Definition Classes
- AnyRef
- Annotations
- @native() @HotSpotIntrinsicCandidate()
- final def notifyAll(): Unit
- Definition Classes
- AnyRef
- Annotations
- @native() @HotSpotIntrinsicCandidate()
- def opposite: Direction
Returns a direction that has the opposite
dxand the oppositedyas this one. - def productElementNames: Iterator[String]
- Definition Classes
- Product
- def roughly: String
Returns a
Stringdescription of theDirection; e.g.,"dx=1.00,dy=0.00".Returns a
Stringdescription of theDirection; e.g.,"dx=1.00,dy=0.00". Uses two decimals for each coordinate. - def sharesQuadrant(another: Direction): Boolean
Determines whether this direction and the given one point towards the same quarter of the unit circle.
Determines whether this direction and the given one point towards the same quarter of the unit circle. That is, determines whether the two directions’
dxhave same sign as the other and whether theirdys also have the same signs. Zero values count as both positive and negative for this purpose. - def switchX: Direction
Returns a direction that has the opposite
dxas this one and the samedy. - def switchY: Direction
Returns a direction that has the opposite
dyas this one and the samedx. - final def synchronized[T0](arg0: => T0): T0
- Definition Classes
- AnyRef
- def toDegrees: Double
Returns this direction as degrees.
Returns this direction as degrees. (Zero degrees points rightwards, positive angles are counterclockwise from there, negative ones clockwise.)
- def toRadians: Double
Returns this direction as radians.
Returns this direction as radians. (Zero radians points rightwards, positive angles are counterclockwise from there, negative ones clockwise.)
- def toString: String
Returns a
Stringdescription of theDirection. - 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.