zoukankan      html  css  js  c++  java
  • Intersecting Lines

    总时间限制: 
    1000ms
     
    内存限制: 
    65536kB
    描述
    We all know that a pair of distinct points on a plane defines a line and that a pair of lines on a plane will intersect in one of three ways: 1) no intersection because they are parallel, 2) intersect in a line because they are on top of one another (i.e. they are the same line), 3) intersect in a point. In this problem you will use your algebraic knowledge to create a program that determines how and where two lines intersect. 
    Your program will repeatedly read in four points that define two lines in the x-y plane and determine how and where the lines intersect. All numbers required by this problem will be reasonable, say between -1000 and 1000.
    输入
    The first line contains an integer N between 1 and 10 describing how many pairs of lines are represented. The next N lines will each contain eight integers. These integers represent the coordinates of four points on the plane in the order x1y1x2y2x3y3x4y4. Thus each of these input lines represents two lines on the plane: the line through (x1,y1) and (x2,y2) and the line through (x3,y3) and (x4,y4). The point (x1,y1) is always distinct from (x2,y2). Likewise with (x3,y3) and (x4,y4).
    输出
    There should be N+2 lines of output. The first line of output should read INTERSECTING LINES OUTPUT. There will then be one line of output for each pair of planar lines represented by a line of input, describing how the lines intersect: none, line, or point. If the intersection is a point then your program should output the x and y coordinates of the point, correct to two decimal places. The final line of output should read "END OF OUTPUT".
    【题解】
    计算几何模板题。关键是把和interset()函数相关的计算模板背下。
    【代码】
     1 #include <iostream>
     2 #include <cstdlib>
     3 #include <cmath>
     4 using namespace std;
     5 #define CPoint CVector
     6 #define PI acos(-1)
     7 #define INF 1e20
     8 #define EPS 1e-12
     9 
    10 struct CVector
    11 {
    12     double x, y;
    13     CVector(double _x, double _y) :
    14         x(_x), y(_y) {}
    15     CVector() :x(INF), y(INF) {}
    16 };
    17 
    18 struct CLine
    19 {
    20     CPoint a, b;
    21     CLine(CPoint _a, CPoint _b) {
    22         a = _a;
    23         b = _b;
    24     }
    25 };
    26 
    27 bool isZero(double x) {
    28     return -EPS < x && x < EPS;
    29 }
    30 
    31 CVector operator + (CVector p, CVector q) {
    32     return CVector(p.x + q.x, p.y + q.y);
    33 }
    34 
    35 CVector operator - (CVector p, CVector q){
    36     return CVector(p.x - q.x, p.y - q.y);
    37 }
    38 
    39 double operator ^(CVector p, CVector q) {
    40     return p.x * q.y - p.y * q.x;
    41 }
    42 
    43 double area(CVector p, CVector q) {
    44     return (p ^ q) / 2;
    45 }
    46 
    47 double operator *(CVector p, CVector q) {
    48     return p.x*q.x + p.y*q.y;
    49 }
    50 
    51 CVector operator *(double k, CVector p) {
    52     return CVector(k*p.x, k*p.y);
    53 }
    54 
    55 double length(CVector p) {
    56     return sqrt(p * p);
    57 }
    58 
    59 double dist(CPoint p, CLine l) {
    60     return fabs((p - l.a) ^ (l.b - l.a)) / length(l.b - l.a);
    61 }
    62 
    63 CPoint interset(CLine l, CLine m, string &msg) {
    64     double x = area(m.a - l.a, l.b - l.a);
    65     double y = area(l.b - l.a, m.b - l.a);
    66     if (isZero(x + y)) {
    67         if (isZero(dist(l.a, m)))
    68             msg = "LINE";
    69         else msg = "NONE";
    70         return CPoint();
    71     }
    72     msg = "POINT";
    73     return m.a + (x / (x + y)) * (m.b - m.a);
    74 }
    75 
    76 int main()
    77 {
    78     int N = 0;
    79     cin >> N;
    80     printf("INTERSECTING LINES OUTPUT
    ");
    81     while (N--) {
    82         int x1, y1, x2, y2, x3, y3, x4, y4;
    83         string msg;
    84         cin >> x1 >> y1 >> x2 >> y2 >> x3 >> y3 >> x4 >> y4;
    85         CLine l = CLine(CPoint(x1, y1), CPoint(x2, y2));
    86         CLine m = CLine(CPoint(x3, y3), CPoint(x4, y4));
    87         CPoint p = interset(l, m, msg);
    88         if (p.x == INF) printf("%s
    ", msg.c_str());
    89         else printf("%s %.2f %.2f
    ", msg.c_str(), p.x, p.y);
    90     }
    91     printf("END OF OUTPUT
    ");
    92     //system("pause");
    93     return 0;
    94 }
  • 相关阅读:
    2020 ICPC 沈阳站 M. United in Stormwind
    [笔记] 中世纪反演魔法
    [模板] 多项式工业
    HDU 1187 Big Event in HDU
    洛谷 P2000 拯救世界
    HDU 1085 Holding Bin-Laden Captive!
    HDU 1028 Ignatius and the Princess III
    性能测试——jmeter接口测试复习——请求元件之配置元件——参数化——txt与csv
    性能测试——jmeter接口测试复习——请求元件之用户自定义变量——即参数化
    性能测试——jmeter接口测试复习——断言之响应断言——放在某个http请求下面,用来判断返回code、header、message中是否包含预期的值
  • 原文地址:https://www.cnblogs.com/Jeffrey-Y/p/10166662.html
Copyright © 2011-2022 走看看