POJ 1269 Intersecting Lines

Source: Internet
Author: User

POJ 1269 Intersecting Lines

This question is to judge the relationship between straight lines. My idea is to first determine whether the two lines are in the same line, and then determine whether the two lines are in parallel. The rest of them must be in the same line. Just find the intersection point directly. When judging the parallelism, you only need to check whether the slope of the two is equal. Because you have already determined whether the parallelism is the same before judging the parallelism, as long as the slope is equal, it must be parallel. The mathematical method is used to calculate the intersection point. Pay attention to the absence of slope.

# Include
 
  
# Include
  
   
# Include
   
    
Using namespace std; struct point {int x, y ;}; struct line {point s, e ;}; int cross (point p1, point p2, point p3) {int x1 = p2.x-p1.x, x2 = p3.x-p1.x; int y1 = p2.y-p1.y, y2 = p3.y-p1.y; return x1 * y2-x2 * y1 ;} int isline (line l1, line l2) // whether to be collocated {int a = cross (l1.s, l2.s, l2.e); int B = cross (l1.e, l2.s, l2.e ); if (a = 0 & B = 0) return 1; return 0;} int isparallel (line l1, line l2) // whether it is parallel {int x1 = l1.s. x-l1.e.x, y1 = l1.s. y-l1.e.y; int x2 = l2.s. x-l2.e.x, y2 = l2.s. y-l2.e.y; if (x1 * y2-x2 * y1 = 0) return 1; return 0;} void get_point (line l1, line l2) // calculates the intersection {double x, y; if (l1.s. x = l1.e. x) // the case where the slope does not exist should be discussed {double k2 = (l2.s. y-l2.e.y) x 1.0/(l2.s. x-l2.e.x); double b2 = l2.s. y * 1.0-k2 * l2.s. x; y = k2 * l1.s. x + b2; printf ("POINT %. 2lf %. 2lf \ n ", (double) l1.s. x, y); return;} if (l2.s. x = l2.e. x) {double k1 = (l1.s. y-l1.e.y) x 1.0/(l1.s. x-l1.e.x); double b1 = l1.s. y * 1.0-k1 * l1.s. x; y = k1 * l2.s. x + b1; printf ("POINT %. 2lf %. 2lf \ n ", (double) l2.s. x, y); return;} double k1 = (l1.s. y-l1.e.y) x 1.0/(l1.s. x-l1.e.x); double b1 = l1.s. y * 1.0-k1 * l1.s. x; double k2 = (l2.s. y-l2.e.y) x 1.0/(l2.s. x-l2.e.x); double b2 = l2.s. y * 1.0-k2 * l2.s. x; x = (b2-b1)/(K1-K2); y = k1 * x + b1; printf ("POINT %. 2lf %. 2lf \ n ", x, y);} int main () {int n, x1, y1, x2, y2, x3, y3, x4, y4; scanf ("% d", & n); printf ("intersecting lines output \ n"); for (int I = 1; I <= n; I ++) {scanf ("% d", & x1, & y1, & x2, & y2, & x3, & y3, & x4, & y4); line l1, l2; l1.s. x = x1, l1.s. y = y1, l1.e. x = x2, l1.e. y = y2; l2.s. x = x3, l2.s. y = y3, l2.e. x = x4, l2.e. y = y4; if (isline (l1, l2) = 1) {printf ("LINE \ n"); continue;} if (isparallel (l1, l2) = 1) {printf ("NONE \ n"); continue;} get_point (l1, l2);} printf ("end of output \ n"); 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.