sdr 0.3

net.cscott.sdr.calls
Class Position

java.lang.Object
  extended by net.cscott.sdr.calls.Position
All Implemented Interfaces:
Comparable<Position>

public class Position
extends Object
implements Comparable<Position>

Position objects represent the position and orientation of a dancer. The (0,0) coordinate represents the center of the square (or formation), and dancers are nominally at least two units away from each other (although breathing may change this). A zero rotation for 'facing direction' means "facing away from the caller". Positive y is "away from the caller". Positive x is "toward the caller's right". The boy in couple number one starts out at (-1, -3) facing 0. The facing field may not be null; to indicate "rotation unspecified" (for example, for phantoms or when specifying "general lines") use a Rotation with a modulus of 0.


Field Summary
 Rotation facing
          Facing direction.
 Fraction x
          Location.
 Fraction y
          Location.
 
Constructor Summary
Position(Fraction x, Fraction y, Rotation facing)
          Create a Position object from the given x and y coordinates and Rotation.
Position(int x, int y, Rotation facing)
          Create a Position object with integer-valued x and y coordinates.
 
Method Summary
 int compareTo(Position p)
          Compare two Positions.
 boolean equals(Object o)
           
 Position forwardStep(Fraction distance)
          Move the given distance in the facing direction.
static Position getGrid(int x, int y, ExactRotation r)
          Returns a position corresponding to the standard square dance grid. 0,0 is the center of the set, and odd coordinates between -3 and 3 correspond to the standard 4x4 grid.
static Position getGrid(int x, int y, String direction)
          Returns a position corresponding to the standard square dance grid. 0,0 is the center of the set, and odd coordinates between -3 and 3 correspond to the standard 4x4 grid.
 int hashCode()
           
 Position normalize()
          Normalize (restrict to 0-modulus) the rotation of the given position.
 Position rotateAroundOrigin(ExactRotation rot)
          Rotate this position around the origin by the given amount.
 Position sideStep(Fraction distance)
          Move the given distance perpendicular to the facing direction.
 String toString()
           
 Position turn(Fraction amount)
          Turn in place the given amount.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

x

public final Fraction x
Location. Always non-null.


y

public final Fraction y
Location. Always non-null.


facing

public final Rotation facing
Facing direction. Note that facing should always be an ExactRotation for real (non-phantom) dancers.

Constructor Detail

Position

public Position(Fraction x,
                Fraction y,
                Rotation facing)
Create a Position object from the given x and y coordinates and Rotation.


Position

public Position(int x,
                int y,
                Rotation facing)
Create a Position object with integer-valued x and y coordinates.

Method Detail

forwardStep

public Position forwardStep(Fraction distance)
Move the given distance in the facing direction. Requires that the facing direction be an ExactRotation.

Tests:
Move the couple #1 boy forward (in to the center) two steps:
js> importPackage(net.cscott.sdr.util)
js> Position.getGrid(-1,-3,"n").forwardStep(Fraction.TWO)
-1,-1,0

sideStep

public Position sideStep(Fraction distance)
Move the given distance perpendicular to the facing direction. Requires that the facing direction be an ExactRotation.

Tests:
Couple #1 boy truck:
js> importPackage(net.cscott.sdr.util)
js> Position.getGrid(-1,-3,"n").sideStep(Fraction.mONE)
-2,-3,0
Couple #2 girl truck:
js> importPackage(net.cscott.sdr.util)
js> Position.getGrid(3,1,"w").sideStep(Fraction.ONE)
3,2,3/4

turn

public Position turn(Fraction amount)
Turn in place the given amount.

Tests:
Exercise the turn method:
js> ONE_HALF = net.cscott.sdr.util.Fraction.ONE_HALF
1/2
js> p = Position.getGrid(0,0,"n").turn(ONE_HALF)
0,0,1/2
js> p = p.turn(ONE_HALF)
0,0,1

rotateAroundOrigin

public Position rotateAroundOrigin(ExactRotation rot)
Rotate this position around the origin by the given amount.

Tests:
Rotating the #1 boy by 1/4 gives the #4 boy position:
js> p = Position.getGrid(-1,-3,0)
-1,-3,0
js> p.rotateAroundOrigin(ExactRotation.ONE_QUARTER)
-3,1,1/4

normalize

public Position normalize()
Normalize (restrict to 0-modulus) the rotation of the given position.

Tests:
Show normalization after two 180-degree turns:
js> importPackage(net.cscott.sdr.util)
js> p = Position.getGrid(0,0,"e").turn(Fraction.ONE_HALF)
0,0,3/4
js> p = p.turn(Fraction.ONE_HALF)
0,0,1 1/4
js> p.normalize()
0,0,1/4

getGrid

public static Position getGrid(int x,
                               int y,
                               ExactRotation r)
Returns a position corresponding to the standard square dance grid. 0,0 is the center of the set, and odd coordinates between -3 and 3 correspond to the standard 4x4 grid.

Tests:
Some sample grid locations:
js> Position.getGrid(0,0,ExactRotation.ZERO)
0,0,0
js> Position.getGrid(-3,3,ExactRotation.WEST)
-3,3,3/4

getGrid

public static Position getGrid(int x,
                               int y,
                               String direction)
Returns a position corresponding to the standard square dance grid. 0,0 is the center of the set, and odd coordinates between -3 and 3 correspond to the standard 4x4 grid. For convenience, the direction is specified as a string valid for ExactRotation.valueOf(String).

Tests:
Some sample grid locations:
js> Position.getGrid(0,0,"n")
0,0,0
js> Position.getGrid(1,2,"e")
1,2,1/4

equals

public boolean equals(Object o)
Overrides:
equals in class Object

hashCode

public int hashCode()
Overrides:
hashCode in class Object

toString

public String toString()
Overrides:
toString in class Object

compareTo

public int compareTo(Position p)
Compare two Positions. We use reading order: top to bottom, then left to right. Ties are broken by facing direction: first the most specific rotation modulus, then by normalized direction.

Specified by:
compareTo in interface Comparable<Position>
Tests:
Top to bottom:
js> Position.getGrid(0,0,"n").compareTo(Position.getGrid(1,1,"n")) > 0
true
Left to right:
js> Position.getGrid(1,0,"n").compareTo(Position.getGrid(0,0,"n")) > 0
true
Most specific rotation modulus first:
js> new Position["(int,int,net.cscott.sdr.calls.Rotation)"](
  >              0,0,Rotation.fromAbsoluteString("|")
  >              ).compareTo(Position.getGrid(0,0,"n")) > 0
true
Normalized direction:
js> Position.getGrid(0,0,"e").compareTo(Position.getGrid(0,0,"n")) > 0
true
Equality:
js> Position.getGrid(1,2,"w").compareTo(Position.getGrid(1,2,"w")) == 0
true

sdr 0.3

Copyright © 2006-2009 C. Scott Ananian