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 Direction
s with the methods on this class’s 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 Direction
s, as does the directionOf method on Pos
objects. Moreover, this class’s companion object 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.*
.
Parameters
- dx
-
the amount of change in the x coordinate when moving in this direction, relative to
dy
- dy
-
the amount of change in the y coordinate when moving in this direction, relative to
dx
Attributes
- Companion
- object
- Graph
-
- Supertypes