jfreerails.world.train
Class TrainPositionOnMap

java.lang.Object
  extended by jfreerails.world.train.TrainPositionOnMap
All Implemented Interfaces:
java.io.Serializable, FreerailsSerializable

public class TrainPositionOnMap
extends java.lang.Object
implements FreerailsSerializable

This immutable class represents the position of a train as a String of points. There must be at least two points. The first point is the position of the front of the train; the last point is the position of the end of the train. Any intermediate points are positions of 'kinks' in the track. Coordinates are expressed in display coordinates relative to the map origin (as opposed to map squares).

Train positions can be combined and divided as illustrated below (notice what happens to the head and tail that are combined)

if a = {(10, 10), (20,20), (30,30), (40,40) }
and b = {(1,1), (4,4), (5,5), (10, 10)}
then a.addToHead(b) = {(1,1), (4,4), (5,5), (20,20), (30,30), (40,40) }
and b.addToTail(a) = {(1,1), (4,4), (5,5), (20,20), (30,30), (40,40) }
and if c = {(1,1), (4,4), (5,5), (20,20), (30,30), (40,40) }
then c.removeFromTail(a) = {(1,1), (4,4), (5,5), (10, 10)}
and c.removeFromHead(b) = {(10, 10), (20,20), (30,30), (40,40) }

Author:
Luke Lindsay 26-Oct-2002
See Also:
Serialized Form

Field Summary
private  double acceleration
           
private  SpeedTimeAndStatus.TrainActivity activity
           
static int CRASH_FRAMES_COUNT
           
private  boolean crashSite
           
private  int frame
           
private  int frameCt
           
private static long serialVersionUID
           
private  double speed
           
private  ImInts xpoints
           
private  ImInts ypoints
           
 
Constructor Summary
  TrainPositionOnMap(ImInts xs, ImInts ys)
           
private TrainPositionOnMap(int[] xs, int[] ys, double speed, double acceleration, SpeedTimeAndStatus.TrainActivity activity)
           
 
Method Summary
private  TrainPositionOnMap addBtoHeadOfA(TrainPositionOnMap b, TrainPositionOnMap a)
           
 TrainPositionOnMap addToHead(TrainPositionOnMap b)
           
 TrainPositionOnMap addToTail(TrainPositionOnMap a)
           
static boolean aHeadEqualsBTail(TrainPositionOnMap a, TrainPositionOnMap b)
           
static boolean bHeadEqualsATail(TrainPositionOnMap a, TrainPositionOnMap b)
           
 double calulateDistance()
           
 boolean canAddToHead(TrainPositionOnMap b)
           
 boolean canAddToTail(TrainPositionOnMap b)
           
 boolean canRemoveFromHead(TrainPositionOnMap b)
           
 boolean canRemoveFromTail(TrainPositionOnMap b)
           
static TrainPositionOnMap createInSameDirectionAsPath(FreerailsPathIterator path)
           
static TrainPositionOnMap createInSameDirectionAsPath(FreerailsPathIterator path, double speed, double acceleration, SpeedTimeAndStatus.TrainActivity activity)
           
static TrainPositionOnMap createInstance(int[] xpoints, int[] ypoints)
           
 boolean equals(java.lang.Object o)
           
 double getAcceleration()
           
 SpeedTimeAndStatus.TrainActivity getActivity()
           
 int getFrameCt()
           
 int getLength()
           
 double getSpeed()
           
 int getX(int position)
           
 ImInts getXPoints()
           
 int getY(int position)
           
 ImInts getYPoints()
           
 int hashCode()
           
static boolean headsAreEqual(TrainPositionOnMap a, TrainPositionOnMap b)
           
 void incrementFramCt()
           
 void incrementFrame()
           
 boolean isCrashSite()
           
 FreerailsPathIterator path()
           
 TrainPositionOnMap removeFromHead(TrainPositionOnMap b)
           
 TrainPositionOnMap removeFromTail(TrainPositionOnMap b)
           
 TrainPositionOnMap reverse()
           
 FreerailsPathIterator reversePath()
           
 void setCrashSite(boolean isCrash)
           
static boolean tailsAreEqual(TrainPositionOnMap a, TrainPositionOnMap b)
           
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

CRASH_FRAMES_COUNT

public static final int CRASH_FRAMES_COUNT
See Also:
Constant Field Values

serialVersionUID

private static final long serialVersionUID
See Also:
Constant Field Values

xpoints

private final ImInts xpoints

ypoints

private final ImInts ypoints

speed

private final double speed

acceleration

private final double acceleration

activity

private final SpeedTimeAndStatus.TrainActivity activity

crashSite

private boolean crashSite

frameCt

private int frameCt

frame

private int frame
Constructor Detail

TrainPositionOnMap

public TrainPositionOnMap(ImInts xs,
                          ImInts ys)

TrainPositionOnMap

private TrainPositionOnMap(int[] xs,
                           int[] ys,
                           double speed,
                           double acceleration,
                           SpeedTimeAndStatus.TrainActivity activity)
Method Detail

isCrashSite

public boolean isCrashSite()

setCrashSite

public void setCrashSite(boolean isCrash)

getFrameCt

public int getFrameCt()

incrementFramCt

public void incrementFramCt()

incrementFrame

public void incrementFrame()

hashCode

public int hashCode()
Overrides:
hashCode in class java.lang.Object

equals

public boolean equals(java.lang.Object o)
Overrides:
equals in class java.lang.Object

calulateDistance

public double calulateDistance()

getLength

public int getLength()

getXPoints

public ImInts getXPoints()

getYPoints

public ImInts getYPoints()

getX

public int getX(int position)

getY

public int getY(int position)

path

public FreerailsPathIterator path()

reversePath

public FreerailsPathIterator reversePath()

reverse

public TrainPositionOnMap reverse()

createInstance

public static TrainPositionOnMap createInstance(int[] xpoints,
                                                int[] ypoints)

addToHead

public TrainPositionOnMap addToHead(TrainPositionOnMap b)

addBtoHeadOfA

private TrainPositionOnMap addBtoHeadOfA(TrainPositionOnMap b,
                                         TrainPositionOnMap a)

canAddToHead

public boolean canAddToHead(TrainPositionOnMap b)

addToTail

public TrainPositionOnMap addToTail(TrainPositionOnMap a)

canAddToTail

public boolean canAddToTail(TrainPositionOnMap b)

removeFromHead

public TrainPositionOnMap removeFromHead(TrainPositionOnMap b)

canRemoveFromHead

public boolean canRemoveFromHead(TrainPositionOnMap b)

removeFromTail

public TrainPositionOnMap removeFromTail(TrainPositionOnMap b)

canRemoveFromTail

public boolean canRemoveFromTail(TrainPositionOnMap b)

createInSameDirectionAsPath

public static TrainPositionOnMap createInSameDirectionAsPath(FreerailsPathIterator path)

createInSameDirectionAsPath

public static TrainPositionOnMap createInSameDirectionAsPath(FreerailsPathIterator path,
                                                             double speed,
                                                             double acceleration,
                                                             SpeedTimeAndStatus.TrainActivity activity)

headsAreEqual

public static boolean headsAreEqual(TrainPositionOnMap a,
                                    TrainPositionOnMap b)

tailsAreEqual

public static boolean tailsAreEqual(TrainPositionOnMap a,
                                    TrainPositionOnMap b)

aHeadEqualsBTail

public static boolean aHeadEqualsBTail(TrainPositionOnMap a,
                                       TrainPositionOnMap b)

bHeadEqualsATail

public static boolean bHeadEqualsATail(TrainPositionOnMap a,
                                       TrainPositionOnMap b)

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

getAcceleration

public double getAcceleration()

getActivity

public SpeedTimeAndStatus.TrainActivity getActivity()

getSpeed

public double getSpeed()