Determine if two segments intersect

Source: Internet
Author: User

According to this property, it is possible to determine whether the point P2 is on the left or the right side of the line, which is an important property to determine if the two segments intersect.

This is a case where the two segments intersect, and the end of one segment is on the other.

This is the principle of judging whether the two segments intersect.

1#include <iostream>2#include <algorithm>3#include <cmath>4 using namespacestd;5 6 structPoint {7     Doublex, y;8 };9 Ten BOOLSegmentsintersect (Point P1, point P2, point p3,point p4) { One     DoubleD1 =direction (P1, P2, p3); A     DoubleD2 =direction (P1, P2, p4); -     DoubleD3 =direction (P3, P4, p1); -     DoubleD4 =direction (P3, P4, p2); the  -     if(D1 >0&& D2 <0|| D1 <0|| D2>0|| D3>0&& D4 <0|| D3 <0&& d4>0) -         return true; -     if(Fabs (D1) <= 1e-9&& onsegment (P1, p2, p3))return true; +     if(Fabs (D2) <= 1e-9&& onsegment (P1, P2, p4))return true; -     if(Fabs (D3) <= 1e-9&& onsegment (P3, P4, p1))return true; +     if(Fabs (D4) <= 1e-9&& onsegment (P3, P4, p2))return true; A     return false; at } -  - //This is the judgment P3 is on which side of the segment p1p2 - Doubledirection (Point P1, point P2, point p3) { -     return(p2.x-p1.x) * (P3.Y-P2.Y)-(p3.x-p2.x) * (P2.Y-p1.y); - } in  - //This is to determine whether the point P3 is within a rectangle with p1p2 diagonal to BOOLonsegment (Point P1, point P2, point p3) { +     if(p3.x >= min (p1.x, p2.x) && p3.x <= Max (p1.x, p2.x) && -P3.y >= min (p1.y, p2.y) && p3.y <=Max (P1.Y, p2.y)) the         return true; *     return false; $}

Here's a more detailed look at the geometry of the introduction to algorithms

This method is somewhat understood.

Determine if two segments intersect

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.