POJ-3304Segments[计算几何]
Posted zprhhs
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POJ-3304Segments[计算几何]相关的知识,希望对你有一定的参考价值。
Segments
Description
Given n segments in the two dimensional space, write a program, which determines if there exists a line such that after projecting these segments on it, all projected segments have at least one point in common.
Input
Input begins with a number T showing the number of test cases and then, T test cases follow. Each test case begins with a line containing a positive integer n ≤ 100 showing the number of segments. After that, n lines containing four real numbers x1 y1 x2 y2 follow, in which (x1, y1) and (x2, y2) are the coordinates of the two endpoints for one of the segments.
Output
For each test case, your program must output "Yes!", if a line with desired property exists and must output "No!" otherwise. You must assume that two floating point numbers a and b are equal if |a - b| < 10-8.
Sample Input
3 2 1.0 2.0 3.0 4.0 4.0 5.0 6.0 7.0 3 0.0 0.0 0.0 1.0 0.0 1.0 0.0 2.0 1.0 1.0 2.0 1.0 3 0.0 0.0 0.0 1.0 0.0 2.0 0.0 3.0 1.0 1.0 2.0 1.0
Sample Output
Yes! Yes! No!
题意:
求n条线段在一条直线上的投影是否有交点
思路:
假设存在这样的一个交点,那么过交点做投影直线的垂线,必定和所有的线段相交,然后就将问题化为构造一条和所有线段相交的直线。通过每次枚举两个线段的
端点作出直线。
#include "stdio.h" #include "iostream" #include "algorithm" #include "cmath" #include "string.h" using namespace std; const int maxn = 100 + 10; const double eps = 1e-8; struct Point { double x, y; Point(double x=0.0, double y=0.0) :x(x), y(y) {} }; typedef Point Vector; Vector operator - (Point A,Point B) {return Vector(A.x-B.x,A.y-B.y);} int dcmp(double x) {if (fabs(x) < eps) return 0;return x < 0? -1: 1;} double Dot(Vector A,Vector B) {return A.x*B.x+A.y*B.y;} double Length(Point A) {return sqrt(Dot(A,A));} double Cross(Vector A,Vector B) {return A.x*B.y-A.y*B.x;} int N; Point p[maxn][4]; bool getcross(Point L, Point S) { if (dcmp(Length(S - L)) == 0) return false; for (int i = 0; i < N; i++) { if (dcmp(Cross(S-L, p[i][0]-L)*Cross(S-L, p[i][1]-L))>0) return false; } return true; } void solve() { for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { if (getcross(p[j][0],p[i][0])||getcross(p[j][1],p[i][0])|| getcross(p[j][1],p[i][1])||getcross(p[j][0],p[i][1])) { printf("Yes!\n"); return ; } } } printf("No!\n"); } int main(int argc, char const *argv[]) { int T; scanf("%d", &T); while (T--) { scanf("%d", &N); for (int i = 0; i < N; i++) { scanf("%lf%lf%lf%lf", &p[i][0].x, &p[i][0].y, &p[i][1].x, &p[i][1].y); } solve(); } return 0; } /* 1 6 75 93 42 45 47 44 87 30 10 55 36 14 32 81 73 57 29 84 75 23 18 38 99 95 */
以上是关于POJ-3304Segments[计算几何]的主要内容,如果未能解决你的问题,请参考以下文章
POJ 3304 Segments [计算几何,判断直线和线段相交]
POJ 3304 Segments(计算几何:直线与线段相交)