Several methods for determining whether a point is in a triangle (2D)

Source: Internet
Author: User

 

Several methods for determining whether a point is in a triangle (2D)

I. calculation based on area
This is easy to understand. If a vertex P is inside the triangle or on the edge, the area of the three triangles composed of two vertices of the triangle is equal to that of the original triangle, at this time, the problem is how to calculate the Triangle Area.
Triangle Area = bottom * Height/2
You can also use this formula:
S = | (t0.x * t1.y + t1.x * t2.y + t2.x * t0.y-t1.x * t0.y-t2.x * t1.y-t0.x * t2.y) * 0.5 |

// T0, T1, and T2 are the three vertices of a triangle.
Function getarea (T0: vector2d, T1: vetor2d, T2: vector2d): Number
{
VaR area: Number = (t0.x * t1.y + t1.x * t2.y + t2.x * t0.y-t1.x * t0.y-t2.x * t1.y-t0.x * t2.y) * 0.5;
Area = Area> 0? Area:-area;
Return area;
}

Function ispointinside (P: vector2d): Boolean
{
VaR trianglearea: Number = getarea (T0, T1, T2 );
VaR area0: Number = getarea (p, T0, T1 );
VaR area1: Number = getarea (p, T0, T2 );
VaR area2: Number = getarea (p, T1, T2 );
VaR D: Number = triangleArea-area0-area1-area2;
D = D> 0? D:-D;
If (d <0.0001) return true;
Return false;
}

 

Another method is introduced here:

Let's talk about the content of the vector cross product:

Vector cross product:

Calculation of vector cross product is the core part of algorithms related to straight lines and line segments. If the vector p = (x1, Y1), q = (X2, Y2) is set, the vector cross product is defined as (0, 0) the signed area of the parallelogram consisting of P1, P2, and P1 + P2, that is, p × q = x1 * Y2-X2 * Y1. The result is a scalar. Obviously, the properties include P x q =-(q x P) and P x (-q) =-(P x q ). Generally, all vertices in the following algorithms are considered as vectors without any explanation. The addition and subtraction of two vertices is the addition and subtraction of vectors, and the multiplication of vertices is considered as the cross product of vectors.

A very important property of the cross product is that it can be used to determine the clockwise relationship between two vectors:

If p × q> 0, P is clockwise in Q.
If p × q <0, P is in the counterclockwise direction of Q.
If p × q = 0, p and q are both in the same direction, but they may be reversed.

The area formula we used in the previous article comes from the vector cross product.

A (x1, Y1) B (X2, Y2) C (X3, Y3) P (x0, y0)
: To determine whether a point P is within the Triangle ABC, we only need to determine whether the AP, BP, CP and the corresponding line segments BC, AC, and AB are intersecting, if any one of them is not in the triangle.

The rest is how to determine whether a straight line and a line segment are intersecting.

Take the intersection of AP and BC as an example:
Determine the clockwise relationship between the two vectors based on the vector cross product. If the AP and BC intersect, We can get:
(BA × Pa) * (Ca × Pa) <= 0

// Ba, Pa, CA represents vector, such as Ba = (x2-x1, y2-y1 );

// X0 and Y0 are the points to be judged, and the other points are the three vertices of the triangle.
// The optimization is performed here, and several identical numbers are merged.
Function isonsameside (x0: Number, y0: Number, X1: Number, Y1: Number,
X2: Number, Y2: Number, X3: Number, Y3: Number): Boolean
{
VaR A: number;
VaR B: number;
VaR C: number;
A = y0-Y1;
B = x1-x0;
C = x0 * Y1-X1 * y0;

If (A * X2 + B * y2 + C) * (A * X3 + B * Y3 + C)> 0) return true;
Return false;
}
// P is the vertex to be detected. v0, V1, and v2 are the three vertices of the triangle.
Function ispointinside2 (P: vector2d): Boolean
{
If (isonsameside (P. X, p. Y, v0.x, v0.y, v1.x, v1.y, v2.x, v2.y) |
Isonsameside (P. X, p. Y, v1.x, v1.y, v2.x, v2.y, v0.x, v0.y) |
Isonsameside (P. X, p. Y, v2.x, v2.y, v0.x, v0.y, v1.x, v1.y) return false;
Return true;
}

This method is better than the method I mentioned yesterday.

In an example, a triangle is randomly generated by clicking the mouse and moving the mouse to test whether a triangle is touched.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.