/// <summary> /// Returns the position of the intersection that is closest to a specific position. /// </summary> /// <param name="posn">The position to search for.</param> /// <param name="xsect">The closest intersection.</param> /// <param name="tolsq">The smallest allowable distance (squared) between the /// search position and the closest intersection (default=0.0).</param> /// <returns>TRUE if the position was found.</returns> internal bool GetClosest(IPosition posn, out IPosition xsect, double tolsq) { xsect = null; // Go through each intersection, looking for the closest // intersection that is not TOO close. double mindsq = Double.MaxValue; double dsq; foreach (IntersectionData d in m_Data) { dsq = BasicGeom.DistanceSquared(posn, d.P1); if (dsq < mindsq && dsq > tolsq) { mindsq = dsq; xsect = d.P1; } // If the intersection is a graze, check the 2nd // intersection too. if (d.IsGraze) { dsq = BasicGeom.DistanceSquared(posn, d.P2); if (dsq < mindsq && dsq > tolsq) { mindsq = dsq; xsect = d.P2; } } } return(xsect != null); }
/// <summary> /// Checks if a position is coincident with a line segment /// </summary> /// <param name="p">The position to test</param> /// <param name="start">The start of the segment.</param> /// <param name="end">The end of the segment.</param> /// <param name="tolsq">The tolerance (squared) to use. Default is XYTOLSQ.</param> /// <returns>True if the test position lies somewhere along the segment.</returns> public static bool IsCoincidentWith(IPointGeometry p, IPointGeometry start, IPointGeometry end, double tolsq) { // Check whether there is exact coincidence at either end. if (p.IsCoincident(start) || p.IsCoincident(end)) { return(true); } // Get the distance squared of a perpendicular dropped from // the test position to the segment (or the closest end if the // perpendicular does not fall ON the segment). return(BasicGeom.DistanceSquared(p.X, p.Y, start.X, start.Y, end.X, end.Y) < tolsq); }
private PointGeometry[] CheckMultiSegmentEnds(PointGeometry[] pts) { if (pts.Length <= 2) { return(pts); } //double tol = (Constants.XYRES * Constants.XYRES); double tol = (0.001 * 0.001); PointGeometry[] res = pts; bool doCheck = true; while (doCheck && res.Length > 2) { doCheck = false; // If the start position coincides with the second segment, strip out // the second position. if (BasicGeom.DistanceSquared(res[0].X, res[0].Y, res[1].X, res[1].Y, res[2].X, res[2].Y) < tol) { PointGeometry[] tmp = new PointGeometry[res.Length - 1]; tmp[0] = res[0]; Array.Copy(res, 2, tmp, 1, res.Length - 2); res = tmp; doCheck = true; } } // If the end position coincides with the second last segment, strip out // the second last position. doCheck = true; while (doCheck && res.Length > 2) { doCheck = false; int last = res.Length - 1; if (BasicGeom.DistanceSquared(res[last].X, res[last].Y, res[last - 1].X, res[last - 1].Y, res[last - 2].X, res[last - 2].Y) < tol) { PointGeometry[] tmp = new PointGeometry[res.Length - 1]; Array.Copy(res, 0, tmp, 0, res.Length - 2); tmp[tmp.Length - 1] = res[last]; res = tmp; doCheck = true; } } return(res); }
static double MinDistanceSquared(ICircularArcGeometry g, IPosition p) { // If the position lies in the arc sector, the minimum distance // is given by the distance to the circle. Otherwise the minimum // distance is the distance to the closest end of the arc. if (IsInSector(g, p, 0.0)) { double dist = BasicGeom.Distance(p, g.Circle.Center); double radius = g.Circle.Radius; return((dist - radius) * (dist - radius)); } else { double d1 = BasicGeom.DistanceSquared(p, g.BC); double d2 = BasicGeom.DistanceSquared(p, g.EC); return(Math.Min(d1, d2)); } }
/// <summary> /// Checks whether a position is coincident with a line segment, using a tolerance that's /// consistent with the resolution of data. /// </summary> /// <param name="testX">The position to test</param> /// <param name="testY"></param> /// <param name="xs">Start of line segment.</param> /// <param name="ys"></param> /// <param name="xe">End of line segment.</param> /// <param name="ye"></param> /// <param name="tsq">The tolerance (squared) to use for checking whether the test point is /// coincident with the segment.</param> /// <returns>True if the distance from the test position to the line segment is less /// than the specified tolerance.</returns> private static bool IsCoincident(double testX, double testY, double xs, double ys, double xe, double ye, double tsq) { return(BasicGeom.DistanceSquared(testX, testY, xs, ys, xe, ye) < tsq); }