1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
|
using System;
using System.Collections.Generic;
using System.Text;
namespace QuickRoute.BusinessEntities
{
/// <summary>
/// Class for representing a location on a route relative to the indexes of the segments and waypoints.
/// </summary>
[Serializable]
public class ParameterizedLocation : IComparable<ParameterizedLocation>
{
private int segmentIndex;
private double value;
public ParameterizedLocation(int segmentIndex, double value)
{
this.segmentIndex = segmentIndex;
this.value = value;
}
public ParameterizedLocation(ParameterizedLocation pl)
{
segmentIndex = pl.segmentIndex;
value = pl.value;
}
public int SegmentIndex
{
get { return segmentIndex; }
set { segmentIndex = value; }
}
public double Value
{
get { return value; }
set { this.value = value; }
}
public int CompareTo(ParameterizedLocation other)
{
int indexSign = Math.Sign(segmentIndex - other.SegmentIndex);
if (indexSign != 0) return indexSign;
return Math.Sign(value - other.Value);
}
public override bool Equals(object obj)
{
if (!(obj is ParameterizedLocation)) return false;
return this == (ParameterizedLocation)obj;
}
public override int GetHashCode()
{
unchecked
{
return (SegmentIndex.GetHashCode() * 397) ^ Value.GetHashCode();
}
}
public static bool operator <(ParameterizedLocation pl0, ParameterizedLocation pl1)
{
return pl0.CompareTo(pl1) < 0;
}
public static bool operator <=(ParameterizedLocation pl0, ParameterizedLocation pl1)
{
return pl0.CompareTo(pl1) <= 0;
}
public static bool operator >(ParameterizedLocation pl0, ParameterizedLocation pl1)
{
return pl0.CompareTo(pl1) > 0;
}
public static bool operator >=(ParameterizedLocation pl0, ParameterizedLocation pl1)
{
return pl0.CompareTo(pl1) >= 0;
}
public static bool operator ==(ParameterizedLocation pl0, ParameterizedLocation pl1)
{
if ((object)pl0 == null && (object)pl1 == null) return true;
if ((object)pl0 == null || (object)pl1 == null) return false;
return pl0.CompareTo(pl1) == 0;
}
public static bool operator !=(ParameterizedLocation pl0, ParameterizedLocation pl1)
{
return !(pl0 == pl1);
}
public static ParameterizedLocation operator -(ParameterizedLocation pl0, ParameterizedLocation pl1)
{
if (pl0 <= pl1)
{
return new ParameterizedLocation(0, 0);
}
else if (pl0.SegmentIndex == pl1.SegmentIndex)
{
return new ParameterizedLocation(0, pl0.Value - pl1.Value);
}
else
{
return new ParameterizedLocation(pl0.SegmentIndex - pl1.SegmentIndex, pl0.Value);
}
}
public static ParameterizedLocation operator ++(ParameterizedLocation pl)
{
return new ParameterizedLocation(pl.segmentIndex, Math.Floor(pl.value + 1));
}
public ParameterizedLocation Floor()
{
return new ParameterizedLocation(segmentIndex, Math.Floor(value));
}
public ParameterizedLocation Ceiling()
{
return new ParameterizedLocation(segmentIndex, Math.Ceiling(value));
}
/// <summary>
/// Decides whether this parameterized location has a waypoint, i e the value part is an integer
/// </summary>
public bool IsNode
{
get { return (value == Math.Floor(value)); }
}
public static ParameterizedLocation Start
{
get { return new ParameterizedLocation(0,0); }
}
public override string ToString()
{
return segmentIndex + ", " + value;
}
public enum Direction
{
Backward,
Forward
}
}
}
|