TriangleNet.Tools.QuadTree.IsPointInTriangle C# (CSharp) Method

IsPointInTriangle() static private method

Test, if a given point lies inside a triangle.
static private IsPointInTriangle ( Point p, Point t0, Point t1, Point t2 ) : bool
p Point Point to locate.
t0 Point Corner point of triangle.
t1 Point Corner point of triangle.
t2 Point Corner point of triangle.
return bool
        internal static bool IsPointInTriangle(Point p, Point t0, Point t1, Point t2)
        {
            // TODO: no need to create new Point instances here
            Point d0 = new Point(t1.X - t0.X, t1.Y - t0.Y);
            Point d1 = new Point(t2.X - t0.X, t2.Y - t0.Y);
            Point d2 = new Point(p.X - t0.X, p.Y - t0.Y);

            // crossproduct of (0, 0, 1) and d0
            Point c0 = new Point(-d0.Y, d0.X);

            // crossproduct of (0, 0, 1) and d1
            Point c1 = new Point(-d1.Y, d1.X);

            // Linear combination d2 = s * d0 + v * d1.
            //
            // Multiply both sides of the equation with c0 and c1
            // and solve for s and v respectively
            //
            // s = d2 * c1 / d0 * c1
            // v = d2 * c0 / d1 * c0

            double s = DotProduct(d2, c1) / DotProduct(d0, c1);
            double v = DotProduct(d2, c0) / DotProduct(d1, c0);

            if (s >= 0 && v >= 0 && ((s + v) <= 1))
            {
                // Point is inside or on the edge of this triangle.
                return true;
            }
            return false;
        }

Usage Example

Ejemplo n.º 1
0
        public ITriangle Query(double x, double y)
        {
            Point            point     = new Point(x, y);
            List <int>       nums      = this.root.FindTriangles(point);
            List <ITriangle> triangles = new List <ITriangle>();

            foreach (int num in nums)
            {
                ITriangle triangle = this.triangles[num];
                if (!QuadTree.IsPointInTriangle(point, triangle.GetVertex(0), triangle.GetVertex(1), triangle.GetVertex(2)))
                {
                    continue;
                }
                triangles.Add(triangle);
            }
            return(triangles.FirstOrDefault <ITriangle>());
        }
All Usage Examples Of TriangleNet.Tools.QuadTree::IsPointInTriangle