Exemplo n.º 1
0
        public LineMath PerpendicularAtAPointOnLine(Vector3 point)
        {
            LineMath lm = new LineMath();

            // TODO Check if it on the line
            lm.p1 = point;
            if (IsNaNorIsInfinity(m))
            {
                var x = point.x + 1;
                var z = point.z;
                lm.p2 = new Vector3(x, point.y, z);
                return(lm);
            }
            else if (m != 0)
            {
                var perpM = -1 / m;
                var perpB = point.z - point.x * perpM;
                var x     = point.x + 5;
                var z     = perpM * x + perpB;
                lm.p2 = new Vector3(x, point.y, z);
                return(lm);
            }
            else
            {
                var x = point.x;
                var z = point.z + 1;
                lm.p2 = new Vector3(x, point.y, z);
                return(lm);
            }
        }
Exemplo n.º 2
0
        public Vector3 Intersect(LineMath secondLine)
        {
            if (IsNaNorIsInfinity(m) && IsNaNorIsInfinity(secondLine.m)) // x=a1 and x=a2
            {
                // check if they are the same line, or parallel and have no intersection.
                if (p1.x == secondLine.p1.x)
                {
                    // Same line, return either end of first line, or start of the second line
                    return(p2);
                }
                else
                {
                    return(new Vector3(float.NaN, float.NaN, float.NaN));
                }
            }
            else if (IsNaNorIsInfinity(m))
            {
                return(new Vector3(p1.x, p1.y, p1.x * secondLine.m + secondLine.b));
            }
            else if (IsNaNorIsInfinity(secondLine.m))
            {
                return(new Vector3(secondLine.p1.x, secondLine.p1.y, secondLine.p1.x * m + b));
            }
            else if (m == secondLine.m)
            {
                if (b == secondLine.b)
                {
                    // Same line, return either end of first line, or start of the second line
                    return(p2);
                }
                else
                {
                    return(new Vector3(float.NaN, float.NaN, float.NaN));
                }
            }

            var     intersectionX = (secondLine.b - b) / (m - secondLine.m);
            var     intersectionZ = m * intersectionX + b;
            Vector3 intersection  = new Vector3(intersectionX, p1.y, intersectionZ);

            return(intersection);
        }