Example #1
0
        public override double GetIntersection(Vector from, Vector to, ref Vector normalAtPoint)
        {
            Vector u   = new Vector(v1 - v0);
            Vector v   = new Vector(v2 - v0);
            Vector n   = Vector.CrossProd(u, v);
            Vector dir = to - from;
            Vector w0  = from - v0;
            double a   = -1 * Vector.dot(n, w0);
            double b   = Vector.dot(n, dir);

            if (Math.Abs(b) < 0.000000001)
            {
                if (a == 0)
                {
                    return(2);
                }
                else
                {
                    return(0);
                }
            }
            double r = a / b;

            if (r < 0)
            {
                return(0);
            }
            Vector I = from + r * dir;
            double uu, uv, vv, wu, wv, D;

            uu = Vector.dot(u, u);
            uv = Vector.dot(u, v);
            vv = Vector.dot(v, v);
            Vector w = I - v0;

            wu = Vector.dot(w, u);
            wv = Vector.dot(w, v);
            D  = uv * uv - uu * vv;
            double s, t;

            s = (uv * wv - vv * wu) / D;
            if (s < 0.0 || s > 1.0)         // neni v trojuhelniku
            {
                return(0);
            }
            t = (uv * wu - uu * wv) / D;
            if (t < 0.0 || (s + t) > 1.0)  // neni v trojuhelniku
            {
                return(0);
            }
            normalAtPoint = I;
            normalAtPoint.normalize();
            return(t);                       // je v trojuhelniku

            //returning point
            //V0 + s * u + t * v
            //u = V1 - V0
            //v = V2 - V0
        }
Example #2
0
        public override double GetIntersection(Vector from, Vector to)
        {
            Line   line = new Line(to - from, from);
            Vector p    = line.point;
            Vector v    = line.direction;
            Vector n    = normal;
            double dot1 = Vector.dot(n, v);
            double dot2 = Vector.dot(n, p);

            if (dot1 == 0)
            {
                return(0);
            }
            double t = -(dot2 + d) / dot1;

            return(t);
        }