Math3D.LineLineIntersection C# (CSharp) Method

LineLineIntersection() public static method

public static LineLineIntersection ( Vector3 &intersection, Vector3 linePoint1, Vector3 lineVec1, Vector3 linePoint2, Vector3 lineVec2 ) : bool
intersection Vector3
linePoint1 Vector3
lineVec1 Vector3
linePoint2 Vector3
lineVec2 Vector3
return bool
    public static bool LineLineIntersection(out Vector3 intersection, Vector3 linePoint1, Vector3 lineVec1, Vector3 linePoint2, Vector3 lineVec2)
    {
        intersection = Vector3.zero;

        Vector3 lineVec3 = linePoint2 - linePoint1;
        Vector3 crossVec1and2 = Vector3.Cross(lineVec1, lineVec2);
        Vector3 crossVec3and2 = Vector3.Cross(lineVec3, lineVec2);

        float planarFactor = Vector3.Dot(lineVec3, crossVec1and2);

        //Lines are not coplanar. Take into account rounding errors.
        if((planarFactor >= 0.00001f) || (planarFactor <= -0.00001f)){

            return false;
        }

        //Note: sqrMagnitude does x*x+y*y+z*z on the input vector.
        float s = Vector3.Dot(crossVec3and2, crossVec1and2) / crossVec1and2.sqrMagnitude;

        if((s >= 0.0f) && (s <= 1.0f)){

            intersection = linePoint1 + (lineVec1 * s);
            return true;
        }

        else{
            return false;
        }
    }

Usage Example

Esempio n. 1
0
        private Vector3 SegmentIntersection(Vector3 subpoint1Ab, Vector3 subpoint2Cd, Vector3 subpoint1Bc,
                                            Vector3 subpoint2Da)
        {
            Vector3 intesection;

            Math3D.LineLineIntersection(out intesection, subpoint1Ab, subpoint1Ab - subpoint2Cd, subpoint1Bc,
                                        subpoint1Bc - subpoint2Da);
            return(intesection);
        }
All Usage Examples Of Math3D::LineLineIntersection