Determination and intersection of POJ-1269 straight line intersection

Source: Internet
Author: User

Question:

Two coordinates of two straight lines are given. Find the type of their intersection, if the regular intersection and obtain the intersection.

Analysis:

The point cross product can be used to determine the intersection type of a straight line (parallel, collinearity, intersection). The approximate is:

If the intersection is set to P0 (x0, y0), there is an equation on the two straight lines based on the intersection:

(P1-p0) x (p2-p0) = 0;

(P3-p0) x (p4-p0) = 0;

Expand.

Read the code and you will know:

# Include <cstdio> # include <cmath> using namespace STD; struct node {Double X, Y;} p [5]; double T1, T2; double count (node, node B, node c) {return (. x-c.x) * (B. y-c.y)-(. y-c.y) * (B. x-c.x);} int cross_type (node A, Node B, node C, node D) {If (! T1 &&! T2) return 1; // T1 = 0 & t2 = 0 is a collinearity. If (B. x-. x) * (D. y-C. y) = (D. x-C. x) * (B. y-. y) return 0; // Parallel return 2; // intersection} int main () {int T, flag; scanf ("% d", & T ); puts ("intersecting lines output"); While (t --) {for (INT I = 1; I <= 4; I ++) scanf ("% lf ", & P [I]. x, & P [I]. y); T1 = count (P [1], p [2], p [3]); t2 = count (P [1], p [2], P [4]); flag = cross_type (P [1], p [2], p [3], p [4]); If (flag = 0) puts ("NONE"); else if (flag = 1) puts ("line"); else {P [0]. X = (T1 * P [4]. x-t2 * P [3 ]. X)/(t1-t2); // find X, T1! = T2; P [0]. y = (T1 * P [4]. y-t2 * P [3]. y)/(t1-t2); printf ("Point %. 2f %. 2f \ n ", P [0]. x, p [0]. y); // cup, cannot % lf, WA kneel! } Puts ("End of output ");}

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.