标签:
题目传送门
题意:
分析:
/************************************************ * Author :Running_Time * Created Time :2015/11/2 星期一 20:33:38 * File Name :POJ_2074.cpp ************************************************/ #include <cstdio> #include <algorithm> #include <iostream> #include <sstream> #include <cstring> #include <cmath> #include <string> #include <vector> #include <queue> #include <deque> #include <stack> #include <list> #include <map> #include <set> #include <bitset> #include <cstdlib> #include <ctime> using namespace std; #define lson l, mid, rt << 1 #define rson mid + 1, r, rt << 1 | 1 typedef long long ll; const int N = 1e5 + 10; const int INF = 0x3f3f3f3f; const int MOD = 1e9 + 7; const double EPS = 1e-10; const double PI = acos (-1.0); int dcmp(double x) { //三态函数,减少精度问题 if (fabs (x) < EPS) return 0; else return x < 0 ? -1 : 1; } struct Point { //点的定义 double x, y; Point () {} Point (double x, double y) : x (x), y (y) {} Point operator + (const Point &r) const { //向量加法 return Point (x + r.x, y + r.y); } Point operator - (const Point &r) const { //向量减法 return Point (x - r.x, y - r.y); } Point operator * (double p) const { //向量乘以标量 return Point (x * p, y * p); } Point operator / (double p) const { //向量除以标量 return Point (x / p, y / p); } bool operator < (const Point &r) const { //点的坐标排序 return x < r.x || (x == r.x && y < r.y); } bool operator == (const Point &r) const { //判断同一个点 return dcmp (x - r.x) == 0 && dcmp (y - r.y) == 0; } }; typedef Point Vector; //向量的定义 Point read_point(void) { //点的读入 double x, y; scanf ("%lf%lf", &x, &y); return Point (x, y); } double dot(Vector A, Vector B) { //向量点积 return A.x * B.x + A.y * B.y; } double cross(Vector A, Vector B) { //向量叉积 return A.x * B.y - A.y * B.x; } double polar_angle(Vector A) { //向量极角 return atan2 (A.y, A.x); } double length(Vector A) { //向量长度,点积 return sqrt (dot (A, A)); } double angle(Vector A, Vector B) { //向量转角,逆时针,点积 return acos (dot (A, B) / length (A) / length (B)); } Vector rotate(Vector A, double rad) { //向量旋转,逆时针 return Vector (A.x * cos (rad) - A.y * sin (rad), A.x * sin (rad) + A.y * cos (rad)); } Vector nomal(Vector A) { //向量的单位法向量 double len = length (A); return Vector (-A.y / len, A.x / len); } Point line_line_inter(Point p, Vector V, Point q, Vector W) { //两直线交点,参数方程 Vector U = p - q; double t = cross (W, U) / cross (V, W); return p + V * t; } double point_to_line(Point p, Point a, Point b) { //点到直线的距离,两点式 Vector V1 = b - a, V2 = p - a; return fabs (cross (V1, V2)) / length (V1); } double point_to_seg(Point p, Point a, Point b) { //点到线段的距离,两点式 if (a == b) return length (p - a); Vector V1 = b - a, V2 = p - a, V3 = p - b; if (dcmp (dot (V1, V2)) < 0) return length (V2); else if (dcmp (dot (V1, V3)) > 0) return length (V3); else return fabs (cross (V1, V2)) / length (V1); } Point point_line_proj(Point p, Point a, Point b) { //点在直线上的投影,两点式 Vector V = b - a; return a + V * (dot (V, p - a) / dot (V, V)); } bool can_seg_seg_inter(Point a1, Point a2, Point b1, Point b2) { //判断线段相交,两点式 double c1 = cross (a2 - a1, b1 - a1), c2 = cross (a2 - a1, b2 - a1), c3 = cross (b2 - b1, a1 - b1), c4 = cross (b2 - b1, a2 - b1); return dcmp (c1) * dcmp (c2) < 0 && dcmp (c3) * dcmp (c4) < 0; } bool can_line_seg_inter(Point a1, Point a2, Point b1, Point b2) { //判断直线与线段相交,两点式 double c1 = cross (a2 - a1, b1 - a1), c2 = cross (a2 - a1, b2 - a1); return dcmp (c1 * c2) <= 0; } bool on_seg(Point p, Point a1, Point a2) { //判断点在线段上,两点式 return dcmp (cross (a1 - p, a2 - p)) == 0 && dcmp (dot (a1 - p, a2 - p)) < 0; } double area_triangle(Point a, Point b, Point c) { //三角形面积,叉积 return fabs (cross (b - a, c - a)) / 2.0; } double area_poly(Point *p, int n) { //多边形面积,叉积 double ret = 0; for (int i=1; i<n-1; ++i) { ret += fabs (cross (p[i] - p[0], p[i+1] - p[0])); } return ret / 2; } struct Line { Point a, b; Line () {} Line (Point a, Point b) : a (a), b (b) {} bool operator < (const Line &r) const { return a.x < r.a.x || (a.x == r.a.x && b.x < r.b.x); } }; int main(void) { vector<Line> ls; Line h, p; double x1, x2, y; while (scanf ("%lf%lf%lf", &x1, &x2, &y) == 3) { if (!x1 && !x2 && !y) break; h.a = Point (x1, y); h.b = Point (x2, y); scanf ("%lf%lf%lf", &x1, &x2, &y); p.a = Point (x1, y); p.b = Point (x2, y); int n; scanf ("%d", &n); ls.clear (); for (int i=1; i<=n; ++i) { scanf ("%lf%lf%lf", &x1, &x2, &y); ls.push_back (Line (Point (x1, y), Point (x2, y))); } sort (ls.begin (), ls.end ()); double ans = n == 0 ? p.b.x - p.a.x : 0, x3, x4; bool flag = true; for (int i=0; i<n-1; ++i) { if (dcmp (ls[i].b.x - ls[i+1].a.x) >= 0 || dcmp (ls[i].b.y - h.a.y) >= 0 || dcmp (ls[i+1].a.y - h.a.y) >= 0) continue; x3 = line_line_inter (h.a, h.a - ls[i].b, p.a, p.b).x; x4 = line_line_inter (h.b, h.b - ls[i+1].a, p.a, p.b).x; if (dcmp (x3 - x4) >= 0) continue; flag = true; for (int j=0; j<i; ++j) { if (can_line_seg_inter (h.a, ls[i].b, ls[j].a, ls[j].b) || can_line_seg_inter (h.b, ls[i+1].a, ls[j].a, ls[j].b)) { flag = false; break; } } if (flag) ans = max (ans, x4 - x3); } x3 = line_line_inter (h.a, h.a - ls[n-1].b, p.a, p.b).x; if (x3 < p.b.x) { flag = true; for (int i=0; i<n-1; ++i) { if (can_line_seg_inter (h.a, ls[n-1].b, ls[i].a, ls[i].b)) { flag = false; break; } } if (flag) ans = max (ans, p.b.x - x3); } x4 = line_line_inter (h.b, h.b - ls[0].a, p.a, p.b).x; if (x4 > p.a.x) { flag = true; for (int i=1; i<n; ++i) { if (can_line_seg_inter (h.b, ls[0].a, ls[i].a, ls[i].b)) { flag = false; break; } } if (flag) ans = max (ans, x4 - p.a.x); } if (dcmp (ans - 0) == 0) puts ("No View"); else printf ("%.2f\n", ans); } //cout << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n"; return 0; }
简单几何(WA中) POJ 2074 Line of Sight
标签:
原文地址:http://www.cnblogs.com/Running-Time/p/4931459.html