POJ 2826 (+ seek intersection line of thinking)

Title: Portal

Meaning of the title: the two segments to you and ask you how much these two segments can be connected to rain, the rain from the positive y-axis to the y-axis negative axle axle vertical drop.

 

Ideas: There are a variety of situations, we can talk about.

1, if a line parallel to the x axis, then it is certainly not water.

2, if the two line segments do not intersect, then water can not.

3, the interface is hidden, nor can not take water

 

Determined by the third case, the endpoint determines whether the line segment intersects with another line to the y-axis positive axis ray intersects the interface is covered.

 

C ++ code can be had, G ++ had not

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <queue>
#include <map>
#include <vector>
#include <set>
#include <string>
#include <math.h>
#define LL long long
#define mem(i, j) memset(i, j, sizeof(i))
#define rep(i, j, k) for(int i = j; i <= k; i++)
#define dep(i, j, k) for(int i = k; i >= j; i--)
#define pb push_back
#define make make_pair
#define INF 1e20
#define inf LLONG_MAX
#define PI acos(-1)
using namespace std;

const int N = 55;
const double eps = 1e-8;

struct Point {
    double x, y;
    Point(double x = 0, double y = 0) : x(x), y(y) { }
};

int dcmp(double x) {
    if(fabs(x) < eps) return 0; else return x < 0 ? -1 : 1;
}

Point operator + (Point A, Point B) { return Point(A.x + B.x, A.y + B.y); }
Point operator - (Point A, Point B) { return Point(A.x - B.x, A.y - B.y); }
Point operator * (Point A, double p) { return Point(A.x * p, A.y * p); }
Point operator / (Point A, double p) { return Point(A.x / p, A.y / p); }

doubleCross (Point A, Point B) { return Ax of * By - Ay * Bx;}
 Double Dot (Point A, Point B) { return Ax of * Bx + Ay * By;} 

inline Point GetLineIntersection ( const Point P, const Point V , const Point Q, const Point w) { /// find the intersection of the straight line v * t p + and Q + w * t, the need to ensure that there is an intersection, v and w are direction vectors 
    Point U = P - Q;
     Double T = Cross (W, U) / Cross (V, W);
     return P + V * T; 
} 

inline BOOL Onsegment (point p, point A1, A2 point) { /// the point p is determined whether or not the line segment p1p2
    return dcmp(Cross(a1 - p, a2 - p)) == 0 && dcmp(Dot(a1 - p, a2 - p)) <= 0;
}

inline bool SegmentProperInsection(Point a1, Point a2, Point b1, Point b2) { /// 判断线段是否相交
    if(dcmp(Cross(a1 - a2, b1 - b2)) == 0) /// 两线段平行
        return Onsegment(b1, a1, a2) || Onsegment(b2, a1, a2) || Onsegment(a1, b1, b2) || Onsegment(a2, b1, b2);
    Point tmp = GetLineIntersection(a1, a2 - a1, b1, b2 - b1);
    return Onsegment(tmp, a1, a2) && Onsegment(tmp, b1, b2);
}

void solve() {
    A Point, B, C, D;
    Scanf ( " % LF LF%%% LF LF " , & a.x, & A. Y., & B.x, & B.y); 
    Scanf ( " % LF LF%%% LF LF " , & C.x, & C.y, D.x &, & D.y);
     IF (dCMP (Ay - By)> 0 ) the swap (A, B);
     IF (dCMP (Cy - of Dy)> 0 ) the swap (C, D); 
    
    /// If segment parallel to the x-axis, the water does not receive 
    IF (dCMP (Cy - of Dy) == 0 || dCMP (Ay - By) == 0 ) {the puts ( " 0.00 " ); return ;} 
    
    /// If two segments do not intersect, then water can not 
    IFD) == false(SegmentProperInsection (A, B, C, D) == ) {the puts ( " 0.00 " ); return ;} 

    /// If the interface can not be covered water receiving 
    IF (SegmentProperInsection (A, B, D, Point ( DX, 100000 )) || SegmentProperInsection (C, D, B, Point (Bx, 100000 ))) {the puts ( " 0.00 " ); return ;} 

    /// mensuration 
    Point tmp = GetLineIntersection (A, B - A, C, D - C); 
    Point Pl = Point ( 100000 , By), Point P2 = ( 100000 , of Dy); 

    Point P = GetLineIntersection (C, D - C, B, Pl - B);
     Double ANS1 = FABS (Cross (B - tmp, P - tmp)) /2.0;

    P = GetLineIntersection(A, B - A, D, P2 - D);
    double ans2 = fabs(Cross(D - tmp, P - tmp)) / 2.0;

    double ans = min(ans1, ans2);
    printf("%.2f\n", ans);
}

int main() {

    int _; scanf("%d", &_);

    while(_--) solve();

    return 0;
}

 

Guess you like

Origin www.cnblogs.com/Willems/p/12383639.html