Line Segment intersection line intersection Template

Source: Internet
Author: User
View Code

Const int maxn = 100000;
Const double eps = 1e-8;
Const double pi = acos (-1.0 );
# Define sgn (x) (fabs (x) <eps? 0 :( x> 0? 1:-1 ))
Struct Point {
Double x, y;
Point (double a = 0, double B = 0) {x = a; y = B ;}
Point operator-(const Point & t) const {
Point tmp;
Tmp. x = x-t. x;
Tmp. y = y-t. y;
Return tmp;
}
Bool operator <(const Point & p) const {
Return sgn (x-p.x) <0 | sgn (x-p.x) = 0 & sgn (y-p.y) <0;
}
Bool operator = (const Point & t) const {
Return sgn (x-t.x) = 0 & sgn (y-t.y) = 0;
}
} GP;
Struct Line {
Double a, B, c;
};
Struct Seg {
Point s, e;
Seg (Point a, Point B) {s = a; e = B ;}
};
Inline double Cross (Point a, Point B, Point c ){
Return (B. x-a.x) * (c. y-a.y)-(c. x-a.x) * (B. y-a.y );
};
Line Turn (Point s, Point e ){
Line ln;
Ln. a = s. y-e. y;
Ln. B = e. x-s. x;
Ln. c = s. x * e. y-e. x * s. y;
Return ln;
}
Bool dotOnSeg (Point p, Point s, Point e ){
If (p = s | p = e)
Return true;
Return sgn (Cross (s, e, p) = 0 &&
Sgn (p. x-s.x) * (p. x-e.x) <= 0 & sgn (p. y-s.y) * (p. y-e.y) <= 0; // sgn may be equal to 0, such as line vertical axis
}
Bool Intersect (Point p1, Point p2, Point p3, Point p4, Point & p) {// line intersection
Double a1, b1, c1, a2, b2, c2, d;
A1 = p1.y-p2.y; b1 = p2.x-p1.x; c1 = p1.x * p2.y-p2.x * p1.y;
A2 = p3.y-p4.y; b2 = p4.x-p3.x; c2 = p3.x * p4.y-p4.x * p3.y;
D = a1 * b2-a2 * b1;
If (! Sgn (d) return false;
P. x = (-c1 * b2 + c2 * b1)/d;
P. y = (-a1 * c2 + a2 * c1)/d;
Return true;
}
Bool Line_Inst (Line l1, Line l2, Point & p) {// Line intersection
Double d = l1.a * l2. B-l2.a * l1. B;
If (! Sgn (d) return false;
P. x = (-l1.c * l2. B + l2.c * l1. B)/d;
P. y = (-l1.a * l2.c + l2.a * l1.c)/d;
Return true;
}
Bool Seg_Inst (Seg s1, Seg s2, Point & p) {// determines the intersection of a line segment. Special Judgment is required when the line is collocated. First, the intersection of a line is determined, and then the judgment Point is on a straight line.
Line l1 = Turn (s1.s, s1.e), l2 = Turn (s2.s, s2.e );
Double d = l1.a * l2. B-l2.a * l1. B;
If (sgn (d) = 0) return false;
P. x = (-l1.c * l2. B + l2.c * l1. B)/d;
P. y = (-l1.a * l2.c + l2.a * l1.c)/d;
Return dotOnSeg (p, s1.s, s1.e) & dotOnSeg (p, s2.s, s2.e );
}
Bool check (Seg s1, Seg s2, Point & pp) {// collinearity, Special Judgment
If (s1.s = s2.s | s1.s = s2.e) {pp = s1.s; return true ;}
If (s1.e = s2.s | s1.e = s2.e) {pp = s1.e; return true ;}
Return false;
}

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.