Пример #1
0
    /// <summary> Perform an intersection test between two lines and return result </summary>
    public bool Intersect(Line2D other, out Line2DIntersection intersection)
    {
        float denominator = (ab.y * other.ab.x - ab.x * other.ab.y);

        float t1 =
            ((a.x - other.a.x) * other.ab.y + (other.a.y - a.y) * other.ab.x)
            / denominator;

        if (float.IsInfinity(t1))
        {
            // The lines are parallel (or close enough to it).
            intersection = new Line2DIntersection(false, Vector2.zero, Vector2.zero, Vector2.zero, 0, 0);
            return(false);
        }

        float t2 =
            ((other.a.x - a.x) * ab.y + (a.y - other.a.y) * ab.x)
            / -denominator;

        // Find the point of intersection.
        Vector2 i_point = new Vector2(a.x + ab.x * t1, a.y + ab.y * t1);

        // The segments intersect if t1 and t2 are between 0 and 1.
        bool segments_intersect =
            ((t1 >= 0) && (t1 <= 1) &&
             (t2 >= 0) && (t2 <= 1));

        float t1cached = t1;
        float t2cached = t2;

        // Find the closest points on the segments.
        if (t1 < 0)
        {
            t1 = 0;
        }
        else if (t1 > 1)
        {
            t1 = 1;
        }
        if (t2 < 0)
        {
            t2 = 0;
        }
        else if (t2 > 1)
        {
            t2 = 1;
        }

        Vector2 close_p1 = a + (ab * t1);
        Vector2 close_p2 = other.a + (other.ab * t2);

        intersection = new Line2DIntersection(segments_intersect, i_point, close_p1, close_p2, t1cached, t2cached);
        return(true);
    }
Пример #2
0
    /// <summary> Perform an intersection test between a line and a point. The intersection point lies in the point nearest to the line </summary>
    public bool Intersect(Vector2 point, out Line2DIntersection intersection)
    {
        Line2D other = new Line2D(point, point + Normal);

        return(Intersect(other, out intersection));
    }