POJ1269 determines the positional relationship between two straight lines

Source: Internet
Author: User
The question is to determine a straight line for two points. The question is to give two straight lines (determined by four points) and determine the relationship between the two lines: parallel, same line, and intersection. If the intersection is used, the intersection coordinates are also obtained. Solution: first determine whether the two straight lines p1p2 and q1q2 are collocated. If not, then determine whether the straight lines are parallel. If not, then the two

The question is to determine a straight line for two points. The question is to give two straight lines (determined by four points) and determine the relationship between the two lines: parallel, same line, and intersection. If the intersection is used, the intersection coordinates are also obtained. Solution: first determine whether the two straight lines p1p2 and q1q2 are collocated. If not, then determine whether the straight lines are parallel. If not, then the two

The question is to determine a straight line for two points. The question is to give two straight lines (determined by four points) and determine the relationship between the two lines: parallel, same line, and intersection. If the intersection is used, the intersection coordinates are also obtained.


Solution:


First, determine whether the two straight lines p1p2 and q1q2 are collocated. If not, then determine whether the straight lines are parallel. If not, the two straight lines are intersecting.


Determine the collinearity: p1p2q1 is collocated and p1p2q2 is collocated, And the collinearity is determined by a cross multiplication value of 0,


Parallel judgment: p1p2 and q1q2 are both in line


Intersection:


The point on the p1p2 line can be expressed as p1 + t (P2-P1), and the point of intersection is on the q1q2 line, so there is (q2-q1) X (p1 + t (P2-P1)-q1) = 0


Intersection t = p1 + (q2-q1) X (q1-p1)/(q2-q1) X (P2-P1) * (P2-P1 ))

-----------------------------------------------------------------------
Note: The double type data cannot be directly = 0

------------------------------------------------------------------------

Cross-multiplication does not meet the exchange law


Const double eps = 1e-8; double add (double x, double y) {if (fabs (x + y) <eps * (fabs (x) + fabs (y ))) return 0; return x + y;} struct Point {double x, y; Point () {} Point (double _ x, double _ y): x (_ x ), y (_ y) {} Point operator + (Point o) {return Point (add (x, o. x), add (y, o. y);} Point operator-(Point o) {return Point (add (x,-o. x), add (y,-o. y);} Point operator * (double o) {return Point (x * o, y * o);} double operator ^ (Point o) {return add (x * o. y,-y * o. x);} double dist (Point o) {return sqrt (x-o.x) * (x-o.x) + (y-o.y) * (y-o.y);} void read () {scanf ("% lf", & x, & y) ;};// determine the positional relationship between two straight lines: int twoline (Point p1, Point p2, point q1, Point q2, Point & interp) {if (fabs (P2-P1) ^ (q1-p1) <eps & fabs (P2-P1) ^ (q2-p1) <eps) return 1; // if (fabs (P2-P1) ^ (q2-q1) <eps) return 2; // parallel double d1 = (q2-q1) ^ (q1-p1); double d2 = (q2-q1) ^ (p2-p1); double t = d1/d2; interp = p1 + (p2-p1) * t; // interp return 3; // intersection} int main () {int t, k; Point p1, p2, q1, q2, interp; puts ("intersecting lines output"); cin> t; while (t --) {p1.read (), p2.read (); q1.read (), q2.read (); k = twoline (p1, p2, q1, q2, interp); if (k = 1) puts ("LINE"); else if (k = 2) puts ("NONE"); else printf ("POINT %. 2lf %. 2lf \ n ", interp. x, interp. y);} puts ("end of output"); return 0 ;}

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.