public HeightmapMarginWithInfo UpdateWherePossible(HeightmapMarginWithInfo newMargin)
        {
            //Preconditions.Assert( _heightmapMargin.Length == newMargin.HeightmapLength, todo change not to length but to lods
            //    string.Format("Current margin length is {0} != new margin length == {1} ", _heightmapMargin.Length, newMargin.HeightmapLength));
            Preconditions.Assert((newMargin.Position.IsHorizontal && Position.IsHorizontal) || (newMargin.Position.IsVertical && Position.IsVertical),
                                 string.Format("Current and new margins are one vertical one horizontal: Old {0} new {1}", _heightmapMargin, newMargin));

            bool haveCommonElements = Position.HaveCommonElementWith(newMargin.Position);

            Preconditions.Assert(haveCommonElements, string.Format("Current {0} and new {1} margin dont have common elements", HeightmapMargin, newMargin));

            MarginPosition commonSegment = Position.GetCommonSegment(newMargin.Position);

            var ourStartPercent = Position.InvLerp(commonSegment.StartPoint);
            var ourStartOffset  = (int)Math.Round((double)HeightmapWorkingLength * ourStartPercent);

            var ourEndPercent = Position.InvLerp(commonSegment.EndPoint);
            var ourEndOffset  = (int)Math.Round((double)HeightmapWorkingLength * ourEndPercent);

            var theirStartPercent = newMargin.Position.InvLerp(commonSegment.StartPoint);
            var theirStartOffset  = (int)Math.Round((double)newMargin.HeightmapWorkingLength * theirStartPercent);

            var theirEndPercent = newMargin.Position.InvLerp(commonSegment.EndPoint);
            var theirEndOffset  = (int)Math.Round((double)newMargin.HeightmapWorkingLength * theirEndPercent);

            return(new HeightmapMarginWithInfo(SetMarginSubElement(ourStartOffset, ourEndOffset, theirStartOffset, theirEndOffset, newMargin), Position, LodFactor));
        }
 public bool HaveCommonElementWith(MarginPosition position)
 {
     if (!((IsVertical && position.IsVertical) || (IsHorizontal && position.IsHorizontal)))
     {
         return(false);
     }
     if (IsVertical)
     {
         if (StartPoint.X != position.StartPoint.X)
         {
             return(false);
         }
         else
         {
             return(MathHelp.SegmentsHaveCommonElement(StartPoint.Y, EndPoint.Y, position.StartPoint.Y,
                                                       position.EndPoint.Y));
         }
     }
     else
     {
         if (StartPoint.Y != position.StartPoint.Y)
         {
             return(false);
         }
         else
         {
             return(MathHelp.SegmentsHaveCommonElement(StartPoint.X, EndPoint.X, position.StartPoint.X,
                                                       position.EndPoint.X));
         }
     }
 }
 public MarginPosition GetCommonSegment(MarginPosition other)
 {
     Preconditions.Assert(HaveCommonElementWith(other),
                          string.Format("Cant get common element of margins {0} and {1} - they have no common element ", this, other));
     if (IsHorizontal)
     {
         return(new MarginPosition(
                    new Point2D(Math.Max(StartPoint.X, other.StartPoint.X), StartPoint.Y),
                    new Point2D(Math.Min(EndPoint.X, other.EndPoint.X), EndPoint.Y)));
     }
     else
     {
         return(new MarginPosition(
                    new Point2D(StartPoint.X, Math.Max(StartPoint.Y, other.StartPoint.Y)),
                    new Point2D(EndPoint.X, Math.Min(EndPoint.Y, other.EndPoint.Y))));
     }
 }
 public HeightmapMarginWithInfo(HeightmapMargin heightmapMargin, MarginPosition position, int lodFactor)
 {
     _heightmapMargin = heightmapMargin;
     _position        = position;
     _lodFactor       = lodFactor;
 }