Flyinghearts book notes serialized in the beauty of programming (18)

Source: Internet
Author: User

4.4 Is in the Triangle

Geometric questions are often used as vectors. If the P point is inside the triangle, vector PA rotates Three times in a certain direction of the clock after petabytes, PCs, and then returns to Pa. The rotation angle does not exceed 180 degrees each time. That is to say, the Three Vector products of PA × Pb, Pb × PC and PC × PA should be both positive or negative. If P is on the edge of the triangle, one of the three Vector products should be 0, and the other two are both positive and negative. If a vertex is in a triangle, there must be two of the three Vector products, which are 0 and one is not 0. If ABC three-point is collocated and P is in this line, all the Vector products are 0. If P is not in this line, it is two positive, one negative, or two negative and one positive.

 

View plaincopy to clipboardprint?
Struct point {
Double X;
Double Y;
};

Int direction (const point & A, const point & B, const point & P)
{
Const double zero = 1e-6;
Double TMP = (P. x-a.x) * (p. y-b.y)-(P. x-b.x) * (p. y-a.y );
If (TMP> zero) return 1;
If (TMP <-1 * zero) return 4;
Return 0;
}

Bool is_in_triangle (const point & A, const point & B, const point & C, const point & P)
{
Int T = direction (A, B, P) + direction (B, c, p) + direction (C, A, P );
/*
If (t = 3 | T = 12) return true; // within the Triangle ABC
If (t = 1 | T = 4) return true; // coincides with one of the points A B C
If (t = 2 | T = 8) return true; // It is on the edge of the Triangle ABC.
*/
If (T> = 1 & T <= 4) return true;
If (t = 8 | T = 12) return true;
Return false;

// Within the triangle a [3] = A [12] = 1 on the edge a [2] = A [8] = 1 on the vertex A [1] = A [4] = 1;
// Int A [] = {0, 0, 1, 0, 0, 1 };
// Return a [direction (A, B, P) + direction (B, c, p) + direction (C, A, P)];
}

 

From: http://blog.csdn.net/flyinghearts/archive/2010/05/19/5605993.aspx

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.