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
Boundsrepresents the dimensions of a rectangular area in two-dimensional space.A
Boundsrepresents the dimensions of a rectangular area in two-dimensional space.Boundsobjects 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:
dxanddy, each of which is a number between -1.0 and +1.0 (inclusive).dxindicates the degree to which the x coordinate changes when moving in that direction;dyis 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
Directionwith adxof +1.0 and adyof 0.0; by comparison, the upward direction would have adxof 0.0 and adyof -1.0. . Diagonals have non-zero values for bothdxanddy.The
dxanddyof anyDirectionalways sum up to (roughly) 1.0, with the exception of the special valueNoDirectionwhich represents the lack of a direction and as zero for both components. (In this, aDirectionis different than a Velocity, which is a combination of a direction of movement and a speed.)You don’t instantiate
Directiondirectly; instead, you createDirections 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
Directionobject also create and return newDirections, as does the directionOf method onPosobjects. Moreover, the companion object of this class has a few predefinedDirectionconstants:Up,Down,Left,Right, andNoDirection.Directionobjects 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
Posobject is essentially a pair of two coordinates,xandy.Posobjects 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
Velocityis to divide it in two components:dxanddy.dxindicates the amount (and direction) of movement along the x axis;dyis the same for the y axis. The x coordinate increases rightwards and y downwards. For instance, a velocity with adxof 10 and adyof zero indicates rightward movement, and a velocity with adxof -100 and adyof -100 indicates faster movement leftwards and upwards.There are many ways to create a
Velocityusing 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
dxanddy, 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
Velocityobjects also create and return new velocities.Velocityobjects 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
Boundsexists to make creatingBoundsobjects a bit more convenient.This companion object of class
Boundsexists to make creatingBoundsobjects 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
Directionprovides some constants of typeDirectionand methods for creating newDirectioninstances.This companion object of class
Directionprovides some constants of typeDirectionand methods for creating newDirectioninstances.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
Posprovides alternative methods for creating newPosobjects. - object Velocity extends Serializable
This companion object of class
Velocityexists primarily to provide convenience methods for creating new instances ofVelocity.This companion object of class
Velocityexists 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._.