/// <summary> /// Checks whether this link object intersects another one. /// </summary> /// <param name="other">The other link to check.</param> /// <returns> /// True if there is an intersection. False if: /// /// 1. The other link is THIS link. /// 2. This link is the end of a link. /// 3. This link has no link. /// 4. The other link is the end of any link. /// 5. The other link is not linked. /// 6. There is no intersection. /// </returns> internal bool IsIntersected(PolygonLink other) { // The other object can't be THIS object. if (Object.ReferenceEquals(other, this)) { return(false); } // The other can't be linked to this. if (Object.ReferenceEquals(other, m_Link)) { return(false); } // Both objects must be linked. if (m_Link == null || other.Link == null) { return(false); } // It must be the START of both links. if (m_IsEnd || other.IsEnd) { return(false); } // Get the position of this link. double xk = m_Point.X; double yk = m_Point.Y; double xl = m_Link.Point.X; double yl = m_Link.Point.Y; // Get the position of the other link. double xm = other.Point.X; double ym = other.Point.Y; double xn = other.Link.Point.X; double yn = other.Link.Point.Y; // Return if there is any intersection along the length of the test segments. double xi, yi; return(Geom.CalcIntersect(xk, yk, xl, yl, xm, ym, xn, yn, out xi, out yi, true) != 0); }
/// <summary> /// Intersects this multi-segment with itself. Only SIMPLE intersections will be /// found. There are no special checks for multi-segments that graze themselves. /// </summary> /// <param name="xsect">The intersection results.</param> /// <returns>The number of self-intersections found.</returns> uint SelfIntersect(IntersectionResult xsect) { uint nx = 0; // Get an array of cumulative distances for each segment. IPointGeometry[] data = this.Data; double[] cumdist = GetCumDist(data); // Note start of initial segment (treat as the end of some imaginary line prior to the start). double xs; double ys; double xe = data[0].X; double ye = data[0].Y; // How many line segments have we got? int nseg = data.Length - 1; // Loop through each segment, intersecting it with all subsequent // segments, except for the one that immediately follows. for (int iseg = 1; iseg <= (nseg - 2); iseg++) { // The start of this segment is the end of the previous one. xs = xe; ys = ye; // Get the position of the end of the test segment xe = data[iseg].X; ye = data[iseg].Y; // Compare against subsequent segments (except the next one) for (int jseg = iseg + 2; jseg <= nseg; jseg++) { IPointGeometry start = data[jseg - 1]; IPointGeometry end = data[jseg]; double xi, yi; if (Geom.CalcIntersect(start.X, start.Y, end.X, end.Y, xs, ys, xe, ye, out xi, out yi, true) != 0) { // Define distance to the intersection on the i-segment double dx = xi - xs; double dy = yi - ys; double ilen = cumdist[iseg - 1] + Math.Sqrt(dx * dx + dy * dy); // Likewise for the j-segment dx = xi - start.X; dy = yi - start.Y; double jlen = cumdist[jseg - 1] + Math.Sqrt(dx * dx + dy * dy); // Append TWO intersections. xsect.Append(xi, yi, ilen); xsect.Append(xi, yi, jlen); nx += 2; } } } // Sort the intersections (DON'T set new sort values). xsect.Sort(false); // Return the number of intersections return(nx); }