sdr 0.7

net.cscott.sdr.calls
Class ExactRotation

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

public class ExactRotation
extends Rotation
implements Comparable<ExactRotation>

Rotations are represented as fractions, where '0' is facing north (away from the caller), and '1/4' is facing east. Positive denotes clockwise rotation.


Field Summary
static ExactRotation EAST
          Common absolute rotations.
static ExactRotation FIVE_EIGHTHS
          Common rotations.
static ExactRotation mONE_QUARTER
          Common rotations.
static ExactRotation NORTH
          Common absolute rotations.
static ExactRotation ONE
          Common rotations.
static ExactRotation ONE_EIGHTH
          Common rotations.
static ExactRotation ONE_HALF
          Common rotations.
static ExactRotation ONE_QUARTER
          Common rotations.
static ExactRotation SEVEN_EIGHTHS
          Common rotations.
static ExactRotation SOUTH
          Common absolute rotations.
static ExactRotation THREE_EIGHTHS
          Common rotations.
static ExactRotation THREE_QUARTERS
          Common rotations.
static ExactRotation WEST
          Common absolute rotations.
static ExactRotation ZERO
          Common rotations.
 
Fields inherited from class net.cscott.sdr.calls.Rotation
amount, modulus
 
Constructor Summary
ExactRotation(Fraction amount)
          Constructor from a Fraction object.
 
Method Summary
 ExactRotation add(Fraction f)
          Add the given amount to this rotation direction.
 int compareTo(ExactRotation r)
          Compare unnormalized rotation amounts.
static ExactRotation fromAbsoluteString(String s)
          Converts a string (one of n/s/e/w, ne/nw/se/sw) to the appropriate rotation object.
static ExactRotation fromRelativeString(String s)
          Returns a ExactRotation corresponding to one of the strings "right", "left", or "none".
static ExactRotation fromXY(Fraction x, Fraction y)
          Convert an x/y displacement to a rotation, using our 'squared off' circle.
 boolean isExact()
          Return true iff this rotation is exact (that is, if the modulus is one).
 Fraction minSweep(ExactRotation er)
          Compute the minimum angle (in absolute value) between this heading and the given heading er.
 ExactRotation negate()
          Negate this rotation (mirror image).
 ExactRotation normalize()
          Normalize rotation to the range [0, 1).
 String repr()
          Return an executable representation of this Rotation.
 ExactRotation subtract(Fraction f)
          Subtract the given amount from this rotation direction.
 String toAbsoluteString()
          Returns a human-readable description of the rotation.
 char toDiagramChar()
          Convert rotation to an appropriate ascii-art representation.
 String toRelativeString()
          Returns a human-readable description of the rotation.
 String toString()
          Returns a human-readable description of the rotation.
 Fraction toX()
          Return the X offset of a one-unit step in the rotation direction.
 Fraction toY()
          Return the Y offset of a one-unit step in the rotation direction.
 
Methods inherited from class net.cscott.sdr.calls.Rotation
create, equals, hashCode, included, includes, union, union
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

mONE_QUARTER

public static final ExactRotation mONE_QUARTER
Common rotations.


ZERO

public static final ExactRotation ZERO
Common rotations.


ONE_EIGHTH

public static final ExactRotation ONE_EIGHTH
Common rotations.


ONE_QUARTER

public static final ExactRotation ONE_QUARTER
Common rotations.


THREE_EIGHTHS

public static final ExactRotation THREE_EIGHTHS
Common rotations.


ONE_HALF

public static final ExactRotation ONE_HALF
Common rotations.


FIVE_EIGHTHS

public static final ExactRotation FIVE_EIGHTHS
Common rotations.


THREE_QUARTERS

public static final ExactRotation THREE_QUARTERS
Common rotations.


SEVEN_EIGHTHS

public static final ExactRotation SEVEN_EIGHTHS
Common rotations.


ONE

public static final ExactRotation ONE
Common rotations.


NORTH

public static final ExactRotation NORTH
Common absolute rotations.


EAST

public static final ExactRotation EAST
Common absolute rotations.


SOUTH

public static final ExactRotation SOUTH
Common absolute rotations.


WEST

public static final ExactRotation WEST
Common absolute rotations.

Constructor Detail

ExactRotation

public ExactRotation(Fraction amount)
Constructor from a Fraction object.

Method Detail

isExact

public boolean isExact()
Description copied from class: Rotation
Return true iff this rotation is exact (that is, if the modulus is one).

Overrides:
isExact in class Rotation

add

public ExactRotation add(Fraction f)
Add the given amount to this rotation direction.

Overrides:
add in class Rotation

subtract

public ExactRotation subtract(Fraction f)
Subtract the given amount from this rotation direction.

Overrides:
subtract in class Rotation

negate

public ExactRotation negate()
Negate this rotation (mirror image).

Overrides:
negate in class Rotation

normalize

public ExactRotation normalize()
Normalize rotation to the range [0, 1).

Overrides:
normalize in class Rotation

compareTo

public int compareTo(ExactRotation r)
Compare unnormalized rotation amounts.

Specified by:
compareTo in interface Comparable<ExactRotation>

minSweep

public Fraction minSweep(ExactRotation er)
Compute the minimum angle (in absolute value) between this heading and the given heading er. The result is positive or negative, and can be added to this heading to yield a rotation equivalent to the given heading er.

Tests:
js> ExactRotation.ONE_EIGHTH.minSweep(ExactRotation.SEVEN_EIGHTHS);
-1/4
js> ExactRotation.SEVEN_EIGHTHS.minSweep(ExactRotation.ONE_EIGHTH);
1/4

toAbsoluteString

public String toAbsoluteString()
Returns a human-readable description of the rotation. The output is a valid input to ExactRotation.fromAbsoluteString(String).

Overrides:
toAbsoluteString in class Rotation

toRelativeString

public String toRelativeString()
Returns a human-readable description of the rotation. The output is a valid input to ExactRotation.fromAbsoluteString(String).


fromAbsoluteString

public static ExactRotation fromAbsoluteString(String s)
Converts a string (one of n/s/e/w, ne/nw/se/sw) to the appropriate rotation object. 'n' is facing away from the caller.


repr

public String repr()
Description copied from class: Rotation
Return an executable representation of this Rotation.

Overrides:
repr in class Rotation

fromRelativeString

public static ExactRotation fromRelativeString(String s)
Returns a ExactRotation corresponding to one of the strings "right", "left", or "none".

Parameters:
s -
Returns:
ExactRotation.ZERO if s is "none", ExactRotation.ONE_QUARTER if s is "right", or ExactRotation.mONE_QUARTER if s is "left".
Throws:
IllegalArgumentException - if s is not one of "right", "left", "none", or a number.

toX

public Fraction toX()
Return the X offset of a one-unit step in the rotation direction. Zero indicates north (towards positive y). Use a 'squared off' circle to avoid irrational numbers. This is roughly equivalent to sin(amount).

See Also:
toY()
Tests:
js> importPackage(net.cscott.sdr.util);
js> for (n=-8; n<20; n++) {
  >  er = new ExactRotation(Fraction.valueOf(n, 16));
  >  print("toX("+er+")="+er.toX().toProperString());
  > }; undefined
toX(-1/2)=0
toX(-7/16)=-1/2
toX(-3/8)=-1
toX(-5/16)=-1
toX(-1/4)=-1
toX(-3/16)=-1
toX(-1/8)=-1
toX(-1/16)=-1/2
toX(0)=0
toX(1/16)=1/2
toX(1/8)=1
toX(3/16)=1
toX(1/4)=1
toX(5/16)=1
toX(3/8)=1
toX(7/16)=1/2
toX(1/2)=0
toX(9/16)=-1/2
toX(5/8)=-1
toX(11/16)=-1
toX(3/4)=-1
toX(13/16)=-1
toX(7/8)=-1
toX(15/16)=-1/2
toX(1)=0
toX(1 1/16)=1/2
toX(1 1/8)=1
toX(1 3/16)=1

toY

public Fraction toY()
Return the Y offset of a one-unit step in the rotation direction. Zero indicates north (towards positive y). Use a 'squared off' circle to avoid irrational numbers. This is roughly equivalent to cos(amt).

See Also:
toX()
Tests:
js> importPackage(net.cscott.sdr.util);
js> for (n=-8; n<20; n++) {
  >  er = new ExactRotation(Fraction.valueOf(n, 16));
  >  print("toY("+er+")="+er.toY().toProperString());
  > }; undefined
toY(-1/2)=-1
toY(-7/16)=-1
toY(-3/8)=-1
toY(-5/16)=-1/2
toY(-1/4)=0
toY(-3/16)=1/2
toY(-1/8)=1
toY(-1/16)=1
toY(0)=1
toY(1/16)=1
toY(1/8)=1
toY(3/16)=1/2
toY(1/4)=0
toY(5/16)=-1/2
toY(3/8)=-1
toY(7/16)=-1
toY(1/2)=-1
toY(9/16)=-1
toY(5/8)=-1
toY(11/16)=-1/2
toY(3/4)=0
toY(13/16)=1/2
toY(7/8)=1
toY(15/16)=1
toY(1)=1
toY(1 1/16)=1
toY(1 1/8)=1
toY(1 3/16)=1/2

fromXY

public static ExactRotation fromXY(Fraction x,
                                   Fraction y)
Convert an x/y displacement to a rotation, using our 'squared off' circle. Roughly equivalent to atan2().


toString

public String toString()
Description copied from class: Rotation
Returns a human-readable description of the rotation. The output is a valid input to ExactRotation.valueOf(String).

Overrides:
toString in class Rotation

toDiagramChar

public char toDiagramChar()
Convert rotation to an appropriate ascii-art representation. The character '.' is used for "unrepresentable rotations". The southeast and northwest characters are a little funny.

Overrides:
toDiagramChar in class Rotation
Tests:
Show rotations going CW from north:
js> function c2s(c) { return String.fromCharCode(c) }
js> [c2s(r.toDiagramChar()) for each (r in 
  >  [ExactRotation.NORTH, ExactRotation.EAST,
  >   ExactRotation.SOUTH, ExactRotation.WEST])]
^,>,v,<
js> [c2s(r.toDiagramChar()) for each (r in 
  >  [ExactRotation.ONE_EIGHTH, ExactRotation.THREE_EIGHTHS,
  >   ExactRotation.FIVE_EIGHTHS, ExactRotation.SEVEN_EIGHTHS])]
7,Q,L,`
js> c2s(new ExactRotation(net.cscott.sdr.util.Fraction.valueOf(1,3))
  >     .toDiagramChar())
.

sdr 0.7

Copyright © 2006-2009 C. Scott Ananian