Polygon.LineLineIntersection C# (CSharp) Méthode

LineLineIntersection() public static méthode

public static LineLineIntersection ( Vector2 l1From, Vector2 l1To, Vector2 l2From, Vector2 l2To, Vector2 &intersection ) : bool
l1From Vector2
l1To Vector2
l2From Vector2
l2To Vector2
intersection Vector2
Résultat bool
    public static bool LineLineIntersection(Vector2 l1From, Vector2 l1To, Vector2 l2From,Vector2 l2To, out Vector2 intersection)
    {
        float Ax = l1From.x, Ay = l1From.y;
        float Bx = l1To.x, By = l1To.y;
        float Cx = l2From.x, Cy = l2From.y;
        float Dx = l2To.x, Dy = l2To.y;
        intersection = new Vector2();

        float  distAB, theCos, theSin, newX, ABpos ;

        //  Fail if either line is undefined.
        if (Ax==Bx && Ay==By || Cx==Dx && Cy==Dy) return false;

        //  (1) Translate the system so that point A is on the origin.
        Bx-=Ax; By-=Ay;
        Cx-=Ax; Cy-=Ay;
        Dx-=Ax; Dy-=Ay;

        //  Discover the length of segment A-B.
        distAB=Mathf.Sqrt(Bx*Bx+By*By);

        //  (2) Rotate the system so that point B is on the positive X axis.
        theCos=Bx/distAB;
        theSin=By/distAB;
        newX=Cx*theCos+Cy*theSin;
        Cy  =Cy*theCos-Cx*theSin; Cx=newX;
        newX=Dx*theCos+Dy*theSin;
        Dy  =Dy*theCos-Dx*theSin; Dx=newX;

        //  Fail if the lines are parallel.
        if (Cy==Dy) return false;

        //  (3) Discover the position of the intersection point along line A-B.
        ABpos=Dx+(Cx-Dx)*Dy/(Dy-Cy);

        if (ABpos<0 || ABpos > 1) return false;

        //  (4) Apply the discovered position to line A-B in the original coordinate system.
        intersection = new Vector2 (Ax+ABpos*theCos,
                                    Ay+ABpos*theSin);

        //  Success.
        return true;
    }