2d algorithm used to determine whether a vertex is in a certain triangle

Source: Internet
Author: User

Given A point of p (x0, y0) on the plane, determine whether the point is in the Triangle ABC. The vertex coordinates of the triangle are A (xa, xb), B (xb, yb ), C (xc, yc ). You can use the area method to determine the area. The method S (A, B, C) indicates the area of the Triangle ABC.
1. If abs (S (A, B, C) = abs (S (P, B, C) + abs (S (A, P, C )) + abs (S (A, B, P), P is inside or beside the Triangle ABC; if there is abs (S (P, B, C )) abs (S (A, P, C) and abs (S (A, B, P) are all greater than 0, it means that P is inside the Triangle ABC, otherwise P is on the edge of the Triangle ABC, specifically: S (P, B, C) is 0, it means P is on the edge of BC, S (A, P, C) is 0, P is on the AC side, S (A, B, P) is 0, and P is on the AB side;
2. If abs (S (A, B, C) <abs (S (P, B, C) + abs (S (A, P, C )) + abs (S (A, B, P), P is outside the Triangle ABC;
3. for abs (S (A, B, C)> abs (S (P, B, C) + abs (S (A, P, C )) + abs (S (A, B, P) does not exist theoretically.

How can we find the area of a triangle in a plane? This can be achieved using the cross multiplication, that is, S (A, B, C) is 1/2 of the Vector Model Obtained by the vector AB cross multiplication AC, then the absolute value is the area of the Triangle ABC.

 # Include <iostream> <br/> # include <math. h> <br/> using namespace std; </p> <p> # define ABS_FLOAT_0 0.0001 </p> <p> struct point_float <br/>{< br/> float x; <br/> float y; <br/> }; </p> <p>/** <br/> * @ brief calculate the Triangle Area <br/> */<br/> float GetTriangleSquar (const point_float pt0, const point_float pt1, const point_float pt2) <br/>{< br/> point_float AB, BC; <br/> AB. x = pt1.x-pt0.x; <br/> AB. y = pt1.y-pt0.y; <br/> BC. x = pt2.x-pt1.x; <br/> BC. y = pt2.y-pt1.y; <br/> return fabs (AB. x * BC. y-AB. y * BC. x)/2.0f; <br/>}</p> <p>/** <br/> * @ brief: determines whether a given point is within or on the triangle. <br/> */<br/> bool IsInTriangle (const point_float, const point_float B, const point_float C, const point_float D) <br/>{< br/> float SABC, SADB, SBDC, SADC; <br/> SABC = GetTriangleSquar (, b, C); <br/> SADB = GetTriangleSquar (A, D, B); <br/> SBDC = Ge TTriangleSquar (B, D, C); <br/> SADC = GetTriangleSquar (A, D, C); </p> <p> float SumSuqar = SADB + SBDC + SADC; </p> <p> if (-ABS_FLOAT_0 <(SABC-SumSuqar) & (SABC-SumSuqar) <ABS_FLOAT_0 )) <br/>{< br/> return true; <br/>}< br/> else <br/>{< br/> return false; <br/>}</p> <p> void main (void) <br/>{< br/> point_float A, B, C, P; <br/>. x =. y = 1.0; <br/> B. x = 4.0; <br/> B. y = 1.0; <br/> C. x = 2.0; <Br/> C. y = 5.0; <br/> P. x = 3.0; <br/> P. y = 2.0; </p> <p> if (IsInTriangle (A, B, C, P )) <br/>{< br/> cout <"P is in ABC! "<Endl; <br/>}< br/> else <br/> {<br/> cout <" P is not in ABC! "<Endl; <br/>}< br/>}

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.