Exemplo n.º 1
0
        /// <summary>
        /// Onko piste <c>p</c> pisteiden <c>a</c>, <c>b</c> ja <c>c</c> määräämän kolmion sisällä.
        /// </summary>
        /// <param name="p"></param>
        /// <param name="a"></param>
        /// <param name="b"></param>
        /// <param name="c"></param>
        /// <returns></returns>
        protected static bool IsInsideTriangle(Vector p, Vector a, Vector b, Vector c)
        {
            Vector v0 = c - a;
            Vector v1 = b - a;
            Vector v2 = p - a;

            // Dot products between each side
            double dot00 = Vector.DotProduct(v0, v0);
            double dot01 = Vector.DotProduct(v0, v1);
            double dot02 = Vector.DotProduct(v0, v2);
            double dot11 = Vector.DotProduct(v1, v1);
            double dot12 = Vector.DotProduct(v1, v2);

            // Barycentric coordinates
            double invDenom = 1 / (dot00 * dot11 - dot01 * dot01);
            double u        = (dot11 * dot02 - dot01 * dot12) * invDenom;
            double v        = (dot00 * dot12 - dot01 * dot02) * invDenom;

            return((u >= 0) && (v >= 0) && (u + v < 1));
        }
Exemplo n.º 2
0
 public double ScalarProjection(Vector unitVector)
 {
     return(Vector.DotProduct(this, unitVector) / unitVector.MagnitudeSquared);
 }