Exemplo n.º 1
0
        bool IsSegmentsReallyNear(LineSegment2d source, LineSegment2d target)
        {
            // Duplicate should return.
            if (source.StartPoint == target.StartPoint && source.EndPoint == target.EndPoint ||
                source.EndPoint == target.StartPoint && source.StartPoint == target.EndPoint)
            {
                return(false);
            }

            if (source.IsColinearTo(target))
            {
                return(false);
            }

            // source segment and target segment's angle should less than 20
            var sourceVector = source.Direction;
            var targetVector = target.Direction;
            var angle        = sourceVector.GetAngleTo(targetVector);

            if (angle.Larger(Math.PI / 36.0) && angle.Smaller(Math.PI * 35.0 / 36.0))
            {
                return(false);
            }

            // Check two segments' distance
            var sourceLine2d = new Line2d(source.StartPoint, source.EndPoint);
            var targetLine2d = new Line2d(target.StartPoint, target.EndPoint);
            var dist1        = sourceLine2d.GetDistanceTo(target.StartPoint);
            var dist2        = sourceLine2d.GetDistanceTo(target.EndPoint);

            // If they are both equal to 0.0, means duplicate, just return.
            if (dist1.EqualsWithTolerance(0.0, Tolerance.Global.EqualPoint) && dist2.EqualsWithTolerance(0.0, Tolerance.Global.EqualPoint))
            {
                return(false);
            }

            var dist3 = targetLine2d.GetDistanceTo(source.StartPoint);
            var dist4 = targetLine2d.GetDistanceTo(source.EndPoint);

            if ((dist1.Larger(_tolerance) || dist2.Larger(_tolerance)) &&
                (dist3.Larger(_tolerance) || dist4.Larger(_tolerance)))
            {
                return(false);
            }


            // Filter out the situation if a short segment (length < _tolerance) is connect to another.
            var pt1OnSource = source.GetClosestPointTo(target.StartPoint);
            var pt2OnSource = source.GetClosestPointTo(target.EndPoint);

            if (pt1OnSource.Point == pt2OnSource.Point)
            {
                return(false);
            }

            return(true);
        }