コード例 #1
0
ファイル: GUIDraw.cs プロジェクト: pandey623/Unity3D
    public static IntersectStruct Intersects(Vector2 a1, Vector2 a2, Vector2 b1, Vector2 b2)
    {
        IntersectStruct response  = new IntersectStruct();
        Vector2         b         = a2 - a1;
        Vector2         d         = b2 - b1;
        float           bDotDPerp = b.x * d.y - b.y * d.x;

        // if b dot d == 0, it means the lines are parallel so have infinite intersection points
        if (bDotDPerp == 0)
        {
            return(response);
        }

        Vector2 c = b1 - a1;
        float   t = (c.x * d.y - c.y * d.x) / bDotDPerp;

        if (t < 0 || t > 1)
        {
            return(response);
        }

        float u = (c.x * b.y - c.y * b.x) / bDotDPerp;

        if (u < 0 || u > 1)
        {
            return(response);
        }

        response.Valid = true;
        response.Point = a1 + t * b;

        return(response);
    }
コード例 #2
0
ファイル: GUIDraw.cs プロジェクト: k-lock/Unity3D
    public static IntersectStruct Intersects(Vector2 a1, Vector2 a2, Vector2 b1, Vector2 b2)
    {
        IntersectStruct response = new IntersectStruct();
        Vector2 b = a2 - a1;
        Vector2 d = b2 - b1;
        float bDotDPerp = b.x * d.y - b.y * d.x;

        // if b dot d == 0, it means the lines are parallel so have infinite intersection points
        if (bDotDPerp == 0)
            return response;

        Vector2 c = b1 - a1;
        float t = (c.x * d.y - c.y * d.x) / bDotDPerp;
        if (t < 0 || t > 1)
            return response;

        float u = (c.x * b.y - c.y * b.x) / bDotDPerp;
        if (u < 0 || u > 1)
            return response;

        response.Valid = true;
        response.Point = a1 + t * b;

        return response;
    }