Relationship between point and line, convex and concave quadrilateral.

Source: Internet
Author: User

It is known that the two endpoints of a line segment are a (x1, Y1), B (X2, Y2), and the two endpoints of the other line segment are C (X3, Y3), D (X4, y4), to determine whether there is a point between AB and Cd, if any.
First Judge d = (y2-y1) (x4-x3)-(y4-y3) (x2-x1 ),
If D = 0, the line AB and Cd are parallel or overlapped,
If D! = 0, the line AB and CD have the intersection, and the intersection (x0, y0) is:
X0 = [(x2-x1) * (x4-x3) * (y3-y1) + (y2-y1) * (x4-x3) * x1-(y4-y3) * (x2-x1) * X3]/d
X0 = [(y2-y1) * (y4-y3) * (x3-x1) + (x2-x1) * (y4-y3) * Y1-(x4-x3) * (y2-y1) * Y3]/(-d)
After finding the intersection, determine whether the intersection is on the online segment, that is, determine the following formula:
(X0-x1) * (x0-x2) <= 0
(X0-x3) * (x0-x4) <= 0
(Y0-y1) * (y0-y2) <= 0
(Y0-y3) * (y0-y4) <= 0
Only when the four formulas above are vertical can we determine that (x0, y0) is the intersection of line AB and Cd, otherwise there is no intersection between the two lines

========================================================== ======================================

// Function: Find the point on the left or right of a straight line.
// Return value: 0, 1, left,-1, and right
Int left_right (point a, point B, double X, Double Y)
{
Double T;
A. X-= x; B. X-= X;
A. Y-= y; B. Y-= y;
T = A. x * B. y-a.y * B. X;
Return T = 0? 0: T> 0? 1:-1;
}

// Function: whether the line segments C, D, and a and B intersect
Bool intersect1 (point a, point B, point C, point D)
{
Return left_right (A, B, C. X, C. Y) ^ left_right (A, B, D. x, D. y) =-2;
}

// Function: determines whether the segments C, D, and A and B are intersecting.
Bool intersect (point a, point B, point C, point D)
{
Return intersect1 (A, B, C, D) & intersect (c, d, A, B );
}

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.