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 ");}