public Segment Insert(TrackObject insertedOject, float distanceFromBegining) { Segment insertedSegment = new Segment(); IFirstLastAccessor insertedFLAccessor = FirstLastAccessorFactory.Create(_direction, insertedSegment); IFirstLastAccessor segmentFLAccessor = FirstLastAccessorFactory.Create(_direction, _segment); insertedFLAccessor.FirstObject = segmentFLAccessor.FirstObject; insertedFLAccessor.LastObject = insertedOject; segmentFLAccessor.FirstObject = insertedOject; Segment prevSegment = segmentFLAccessor.PrevSegment; insertedFLAccessor.NextSegment = _segment; insertedFLAccessor.PrevSegment = prevSegment; segmentFLAccessor.PrevSegment = insertedSegment; if (prevSegment != null) { IFirstLastAccessor prevSegmentFLAccessor = FirstLastAccessorFactory.Create(_direction, prevSegment); prevSegmentFLAccessor.NextSegment = insertedSegment; } float distance = distanceFromBegining - _distanceFull; if (distance > 0.0) { throw new VrException(String.Format("Longer distance '{0}' can't be appended, only appended!", distance)); } float insertedSegmentDistance = _segment.Distance + distance; insertedSegment.Distance = insertedSegmentDistance; _segment.Distance = _segment.Distance - insertedSegmentDistance; return insertedSegment; }
private Segment FindSegmentEndsWith(TrackObject toObject) { Segment segment = _segmentFrom; while (segment != null && segment.To != toObject) { segment = segment.Next; } return segment; }
public Segment Insert(TrackObject fromObject, TrackObject newObject, float distanceFrom) { ISegementInfo segmentDistInfo = TryGetSegmantAtDistance(fromObject, distanceFrom); if (segmentDistInfo is ISegementInfoAtAnd) { return ((ISegementInfoAtAnd)segmentDistInfo).Append(newObject, distanceFrom); } return ((ISegementInfoMiddle)segmentDistInfo).Insert(newObject, distanceFrom); }
public Segment GetSegmantAtDistance(TrackObject fromObject, float distance) { ISegementInfo segmentInfo = TryGetSegmantAtDistance(fromObject, distance); if (segmentInfo is ISegementInfoAtAnd) { throw new VrException("Segment is small."); } return segmentInfo.Segment; }
public void Insert(RailwayStation fromObject, TrackObject newObject, float distanceFrom) { if (fromObject == From) { _segmentFrom.Insert(From, newObject, distanceFrom); return; } if (fromObject != To) { throw new VrException("Railway station isn't part of this Line."); } Segment segment = FindSegmentEndsWith(To); if (segment == null) { throw new VrException("Railway station isn't part of this Line."); } segment.Insert(To, newObject, distanceFrom); }
public Segment Append(TrackObject appendedOject, float distanceFromBegining) { Segment appendedSegment = new Segment(); IFirstLastAccessor appendedFLAccessor = FirstLastAccessorFactory.Create(_direction, appendedSegment); IFirstLastAccessor segmentFLAccessor = FirstLastAccessorFactory.Create(_direction, _segment); appendedFLAccessor.LastObject = appendedOject; appendedFLAccessor.FirstObject = segmentFLAccessor.LastObject; appendedFLAccessor.PrevSegment = _segment; segmentFLAccessor.NextSegment = appendedSegment; float appendedSegmentDistance = distanceFromBegining - _distanceFull; if (appendedSegmentDistance <= 0.0) { throw new VrException(String.Format("Shorter distance '{0}' can't be appended, only inserted!", appendedSegmentDistance)); } appendedSegment.Distance = appendedSegmentDistance; return appendedSegment; }
public void SetUp() { _st1 = new RailwayStation("St_1"); _st2 = new RailwayStation("St_2"); _st3 = new RailwayStation("St_3"); _seg1 = new Segment(_st1, _st2, 15); _seg2 = _seg1.Insert(_st1, _st3, 40); }
public Segment(TrackObject from, TrackObject to, float distance) { From = from; To = to; Distance = distance; }
protected Direction GetDirection(TrackObject trackObj) { if (trackObj == From) { return Direction.From; } if (trackObj == To) { return Direction.To; } throw new VrException(String.Format("TrackObject '{0}' isn't part of this Segment '{1}' - '{2}'", trackObj.Name, From.Name, To.Name)); }
protected ISegmentIterator CreateSegmentIterator(TrackObject from) { Direction direction = GetDirection(from); return CreateSegmentIterator(direction); }
public ISegementInfo TryGetSegmantAtDistance(TrackObject fromObject, float distance) { ISegmentIterator iterator = CreateSegmentIterator(fromObject); float distanceSum = iterator.Current.Distance; while (distanceSum < distance && iterator.MoveNext()) { distanceSum += iterator.Current.Distance; } if (distanceSum < distance) { return new SegementInfoAtEnd(iterator, iterator.Current, distanceSum); } return new SegementInfoInside(iterator, iterator.Current, distanceSum); }