public static lineIntersection getLineIntersection(lineSegment L1, lineSegment L2)
        {
            lineIntersection LI;
            LI.isIntersection = 0;
            LI.x = 0;
            LI.y = 0;

            double p0_x = L1.x0;
            double p0_y = L1.y0;
            double p1_x = L1.x1;
            double p1_y = L1.y1;

            double p2_x = L2.x0;
            double p2_y = L2.y0;
            double p3_x = L2.x1;
            double p3_y = L2.y1;

            double s02_x, s02_y, s10_x, s10_y, s32_x, s32_y, s_numer, t_numer, denom, t;
            s10_x = p1_x - p0_x;
            s10_y = p1_y - p0_y;
            s32_x = p3_x - p2_x;
            s32_y = p3_y - p2_y;

            denom = s10_x * s32_y - s32_x * s10_y;
            if (denom == 0)
                return LI; // Collinear
            bool denomPositive = denom > 0;

            s02_x = p0_x - p2_x;
            s02_y = p0_y - p2_y;
            s_numer = s10_x * s02_y - s10_y * s02_x;
            if ((s_numer < 0) == denomPositive)
                return LI; // No collision

            t_numer = s32_x * s02_y - s32_y * s02_x;
            if ((t_numer < 0) == denomPositive)
                return LI; // No collision

            if (((s_numer > denom) == denomPositive) || ((t_numer > denom) == denomPositive))
                return LI; // No collision
            // Collision detected
            t = t_numer / denom;

                LI.x = p0_x + (t * s10_x);

                LI.y = p0_y + (t * s10_y);

                LI.isIntersection = 1; // there is intersection
            return LI;
        }
Example #2
0
    private bool doSegmentsIntersect(lineSegment a, lineSegment b)
    {
        if (a.StartPoint == b.StartPoint ||
            a.StartPoint == b.EndPoint ||
            a.EndPoint == b.StartPoint ||
            a.EndPoint == b.EndPoint)
        {
            return(false);
        }

        return((Mathf.Max(a.StartPoint.x, a.EndPoint.x) >= Mathf.Min(b.StartPoint.x, b.EndPoint.x)) &&
               (Mathf.Max(b.StartPoint.x, b.EndPoint.x) >= Mathf.Min(a.StartPoint.x, a.EndPoint.x)) &&
               (Mathf.Max(a.StartPoint.y, a.EndPoint.y) >= Mathf.Min(b.StartPoint.y, b.EndPoint.y)) &&
               (Mathf.Max(b.StartPoint.y, b.EndPoint.y) >= Mathf.Min(a.StartPoint.y, a.EndPoint.y)));
    }
Example #3
0
    private bool trailCollided()
    {
        if (renderer.positionCount < 3)
        {
            if (renderer.positionCount > 0)
            {
                lastDeltaDistance = renderer.GetPosition(renderer.positionCount - 1);
            }
            return(false);
        }

        if (Vector2.Distance(lastDeltaDistance, renderer.GetPosition(renderer.positionCount - 1)) < deltaDistance)
        {
            return(false);
        }
        lastDeltaDistance = renderer.GetPosition(renderer.positionCount - 1);
        lineSegment[] segs = new lineSegment[renderer.positionCount - 1];
        if (renderer.positionCount > 1)
        {
            for (int i = 0; i < segs.Length; i++)
            {
                segs[i].StartPoint = renderer.GetPosition(i);
                segs[i].EndPoint   = renderer.GetPosition(i + 1);
            }
        }

        for (int i = 0; i < segs.Length; i++)
        {
            lineSegment currentSegment = new lineSegment();
            currentSegment.StartPoint = renderer.GetPosition(renderer.positionCount - 2);
            currentSegment.StartPoint = renderer.GetPosition(renderer.positionCount - 1);

            if (doSegmentsIntersect(segs[i], currentSegment))
            {
                return(true);
            }
        }
        return(false);
    }