package world
This package contains tools for locations and movement in two-dimensional space.
The tools in this package have aliases in the top-level package o1, so they are accessible
to students simply via import o1._
.
The subpackage o1.world.objects contains additional tools for representing entities that reside within two-dimensional spaces.
- Alphabetic
- By Inheritance
- world
- AnyRef
- Any
- Hide All
- Show All
- Public
- Protected
Package Members
- package objects
This package contains tools for representing objects that exist in two-dimensional space.
This package contains tools for representing objects that exist in two-dimensional space. In particular, it defines:
- the Anchor type: an anchor is the point where an object connects to its environment; and
- a number of traits (HasPos, HasVelocity, etc.) the classes of an application can mix in to gain access to various convenience methods.
NOTE TO STUDENTS: In this course, you don't need to understand how this package works or can be used. That being said, some students may wish to experiment with some of its contents. Many of the traits in this package have aliases in the top-level package o1, so they are accessible to students simply via
import o1._
.
Type Members
- final case class Bounds(left: Double, top: Double, width: Double, height: Double) extends Product with Serializable
A
Bounds
represents the dimensions of a rectangular area in two-dimensional space.A
Bounds
represents the dimensions of a rectangular area in two-dimensional space.Bounds
objects are immutable. They assume a space in which x coordinates increase rightwards and y coordinates downwards.This class has an alias in the top-level package o1, so it’s accessible to students simply via
import o1._
.- left
the x coordinate of the left edge of the
Bounds
- top
the y coordinate of the top edge of the
Bounds
- width
the width of the
Bounds
(the distance from its left edge to its right edge)- height
the height of the
Bounds
(the distance from its top edge to its bottom edge)
- sealed case class Direction extends Product with Serializable
Each instance of this class represents a direction on a two-dimensional plane.
Each instance of this class represents a direction on a two-dimensional plane. A direction consists of two components:
dx
anddy
, 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 adx
of +1.0 and ady
of 0.0; by comparison, the upward direction would have adx
of 0.0 and ady
of -1.0. . Diagonals have non-zero values for bothdx
anddy
.The
dx
anddy
of anyDirection
always sum up to (roughly) 1.0, with the exception of the special valueNoDirection
which represents the lack of a direction and as zero for both components. (In this, aDirection
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 createDirection
s 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 newDirection
s, as does the directionOf method onPos
objects. Moreover, the companion object of this class has a few predefinedDirection
constants:Up
,Down
,Left
,Right
, andNoDirection
.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._
. - construct a direction from two differences between
coordinates, as in
- final case class Pos(x: Double, y: Double) extends Product with Serializable
Each instance of this class represents a location on a two-dimensional plane.
Each instance of this class represents a location on a two-dimensional plane. A
Pos
object is essentially a pair of two coordinates,x
andy
.Pos
objects are immutable.This class has an alias in the top-level package o1, so it’s accessible to students simply via
import o1._
.- x
the x coordinate of the
Pos
- y
the y coordinate of the
Pos
- 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.
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
anddy
.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 adx
of 10 and ady
of zero indicates rightward movement, and a velocity with adx
of -100 and ady
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
- construct a velocity from a direction and a (positive
or zero) speed, as in
Value Members
- object Bounds extends Serializable
This companion object of class
Bounds
exists to make creatingBounds
objects a bit more convenient.This companion object of class
Bounds
exists to make creatingBounds
objects a bit more convenient.The object has an alias in the top-level package o1, so it’s accessible to students simply via
import o1._
. - object Direction extends Serializable
This companion object of class
Direction
provides some constants of typeDirection
and methods for creating newDirection
instances.This companion object of class
Direction
provides some constants of typeDirection
and methods for creating newDirection
instances.The object has an alias in the top-level package o1, so it’s accessible to students simply via
import o1._
. - object Pos extends Serializable
This companion object of class
Pos
provides alternative methods for creating newPos
objects. - object Velocity extends Serializable
This companion object of class
Velocity
exists primarily to provide convenience methods for creating new instances ofVelocity
.This companion object of class
Velocity
exists primarily to provide convenience methods for creating new instances ofVelocity
.The object has an alias in the top-level package o1, so it’s accessible to students simply via
import o1._
.