//public static implicit operator string(Path path) //{ // return path.ToString(); //} #endregion /// <summary> /// Yields a new path instance from the current data object /// and the object passed as the parameter 'path'. /// </summary> /// <param name = "toBasePath">The path to make the invokee relative to.</param> /// <returns>A new path that is relative to the passed path.</returns> public Path MakeRelative(Path toBasePath) { if (!IsRooted) { return(this); } var leftOverSegments = new List <string>(); var relativeSegmentCount = 0; var thisEnum = Segments.GetEnumerator(); var rootEnum = toBasePath.Segments.GetEnumerator(); bool thisHasValue; bool rootHasValue; do { thisHasValue = thisEnum.MoveNext(); rootHasValue = rootEnum.MoveNext(); if (thisHasValue && rootHasValue) { if (thisEnum.Current.Equals(rootEnum.Current, StringComparison.OrdinalIgnoreCase)) { continue; } } if (thisHasValue) { leftOverSegments.Add(thisEnum.Current); } if (rootHasValue) { relativeSegmentCount++; } } while (thisHasValue || rootHasValue); var relativeSegment = Enumerable.Repeat("..", relativeSegmentCount).Aggregate("", System.IO.Path.Combine); var finalSegment = System.IO.Path.Combine(relativeSegment, leftOverSegments.Aggregate("", System.IO.Path.Combine)); return(new Path(finalSegment)); }
/// <summary>Gets the selected text.</summary> public virtual CellString GetText() { var doc = TextArea.Document; if (doc == null) { throw new Exception("No associated TextDocument"); } var iter = Segments.GetEnumerator(); // No segments if (!iter.MoveNext()) { return(CellString.Empty); } // If there is only one segment, return the text directly var text = doc.GetText(iter.Current); if (!iter.MoveNext()) { return(text); } // Otherwise, combine text from the segments var cstr = new CellString(text) { Capacity = Length }; for (var more = true; more; more = iter.MoveNext()) { cstr.Append(doc.GetText(iter.Current)); } return(cstr); }
/// <summary> /// Get the segments enumerator /// </summary> /// <returns>The segments enumerator</returns> public IEnumerator <ODataSegment> GetEnumerator() => Segments.GetEnumerator();
private double SignedDistanceOfPoint(CartesianPoint globalPoint) { if (Segments.Count == 1) { return(Segments.First.Value.TransformGlobalToLocalPoint(globalPoint).Y); } var distances = new List <double>(); bool afterPreviousSegment = false; LinkedList <DirectedSegment2D> .Enumerator segmentEnumerator = Segments.GetEnumerator(); LinkedList <CartesianPoint> .Enumerator vertexEnumerator = Vertices.GetEnumerator(); LinkedList <double> .Enumerator angleEnumerator = Angles.GetEnumerator(); // First segment segmentEnumerator.MoveNext(); // Do not advance angleEnumerator yet, since there is 1 less angle than segments. vertexEnumerator.MoveNext(); CartesianPoint localPoint = segmentEnumerator.Current.TransformGlobalToLocalPoint(globalPoint); if (localPoint.X < segmentEnumerator.Current.Length) { distances.Add(localPoint.Y); } else { afterPreviousSegment = true; } // Subsequent segments for (int i = 1; i < Segments.Count - 1; ++i) { segmentEnumerator.MoveNext(); vertexEnumerator.MoveNext(); angleEnumerator.MoveNext(); localPoint = segmentEnumerator.Current.TransformGlobalToLocalPoint(globalPoint); if (localPoint.X < 0.0) { if (afterPreviousSegment) { // Compute the distance from the vertex between this segment and the previous double dx = globalPoint.X - vertexEnumerator.Current.X; double dy = globalPoint.Y - vertexEnumerator.Current.Y; double distance = Math.Sqrt(dx * dx + dy * dy); int sign = -Math.Sign(angleEnumerator.Current); // If growth angle > 0, the convex angle faces the positive area. distances.Add(sign * distance); } afterPreviousSegment = false; } else if (localPoint.X <= segmentEnumerator.Current.Length) { distances.Add(localPoint.Y); afterPreviousSegment = false; } else { afterPreviousSegment = true; } } // Last segment segmentEnumerator.MoveNext(); vertexEnumerator.MoveNext(); angleEnumerator.MoveNext(); localPoint = segmentEnumerator.Current.TransformGlobalToLocalPoint(globalPoint); if (localPoint.X < 0.0) { if (afterPreviousSegment) { // Compute the distance from the vertex between this segment and the previous double dx = globalPoint.X - vertexEnumerator.Current.X; double dy = globalPoint.Y - vertexEnumerator.Current.Y; double distance = Math.Sqrt(dx * dx + dy * dy); int sign = -Math.Sign(angleEnumerator.Current); // If growth angle > 0, the convex angle faces the positive area. distances.Add(sign * distance); } afterPreviousSegment = false; } else { distances.Add(localPoint.Y); } return(distances[MathUtilities.IndexOfMinAbs(distances)]); }
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return(Segments.GetEnumerator()); }
/// <summary> /// IEnumerator /// </summary> /// <returns></returns> public IEnumerator <TSegment> GetEnumerator() { return(Segments.GetEnumerator()); }
/// <summary> /// Checks if this <see cref="CommandRoute" /> matches the /// provided <paramref name="command" />. /// </summary> /// <param name="command"></param> /// <returns> /// <see cref="CommandResult" /> if it matches otherwise it returns null. /// </returns> public CommandResult Match(IEnumerable <string> command) { var results = new List <IMatchResult>(); var optionalResults = new List <IMatchResult>(); var commandEnumerator = ListToPairList(command).GetEnumerator(); var segmentEnumerator = Segments.GetEnumerator(); using (commandEnumerator) { using (segmentEnumerator) { while (segmentEnumerator.MoveNext()) { if (!commandEnumerator.MoveNext()) { //not match, the number of commands is less than the number of segments return(null); } var match = segmentEnumerator.Current?.Match(commandEnumerator.Current.Item1); if (match == null) { return(null); } results.Add(match); } } while (commandEnumerator.MoveNext()) { var current = commandEnumerator.Current; IMatchResult result = null; foreach (var optionalSegment in OptionalSegments) { //verify this isn't the last piece if (current.Item2 != null) { result = optionalSegment.Match($"{current.Item1} {current.Item2}"); if (result != null) { //skip ahead by one because we matched both the current and next command piece commandEnumerator.MoveNext(); break; } } result = optionalSegment.Match(current.Item1); if (result != null) { break; } } if (result == null) { return(null); } optionalResults.Add(result); } } return(new CommandResult { MatchResults = results, OptionalMatchResults = optionalResults }); }