background

How do you know if there is a graph where the mouse is? The transformation problem is you can think of it as a ray from where the mouse is, does that ray intersect the triangle, in other words is there a point in the triangle where the mouse is projected?

Thinking analytical

According to the above description, our real requirement is to determine whether the mouse is located in the triangle. Here we introduce a fastest method. We can take the point where the mouse is as origin and the three points of triangles A, B and C as conditions

Step 1 (set the lead variable and calculate the point origin to the triangle vertex vector and the three edge vector) :

Step 2 (Calculate the cross product from the point to the edge) :

Step 3 (Judge whether the three cross product values are in the same direction) :

If P1,P2,P3P_1,P_2,P_3P1,P2,P3 have the same sign, then the ray intersects the triangle

explain

The cross product of a vector can determine the direction of the vector. Take the result of P1P_1P1 as an example. If it is 0, it means collinear; if it is less than 0, it is perpendicular to the inside; if it is greater than 0, it is perpendicular to the outside; if a point is inside the triangle, three cross products can be calculated respectively. A different sign means that the point is on the other side of an edge

extension
  1. Judgment at some point in the triangle and center of gravity coordinate representation, and this kind of method is constructed by the rays of the mouse as a starting point in the direction of parameter equation, if ray intersecting triangles, shows a triangle inside there will be a bit of a triangle with ray intersection, need simultaneous ray equation parameter equation and the triangle barycenter coordinate parameters equation solving, here no additional instructions
  2. Usually in solving the location of the mouse when the presence of graphics, will not use direct solution of ray and whether the triangle intersection method, because of a complex geometric said there may be thousands of tens of thousands of triangles, impact performance calculation is too big, often USES bounding box method, although there are some error, but to a large number of less calculation, performance is very considerable