POJ 3304 Segments(计算几何)
意甲冠军:给出的一些段的。问:能否找到一条直线,通过所有的行
思维:假设一条直线的存在,所以必须有该过两点的线,然后列举两点,然后推断是否存在与所有的行的交点可以是
代码:
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std; struct Point {
double x, y;
Point() {}
Point(double x, double y) {
this->x = x;
this->y = y;
}
void read() {
scanf("%lf%lf", &x, &y);
}
}; typedef Point Vector; Vector operator + (Vector A, Vector B) {
return Vector(A.x + B.x, A.y + B.y);
} Vector operator - (Vector A, Vector B) {
return Vector(A.x - B.x, A.y - B.y);
} Vector operator * (Vector A, double p) {
return Vector(A.x * p, A.y * p);
} Vector operator / (Vector A, double p) {
return Vector(A.x / p, A.y / p);
} bool operator < (const Point& a, const Point& b) {
return a.x < b.x || (a.x == b.x && a.y < b.y);
} const double eps = 1e-8; int dcmp(double x) {
if (fabs(x) < eps) return 0;
else return x < 0 ? -1 : 1;
} bool operator == (const Point& a, const Point& b) {
return dcmp(a.x - b.x) == 0 && dcmp(a.y - b.y) == 0;
} double Dot(Vector A, Vector B) {return A.x * B.x + A.y * B.y;} //点积
double Length(Vector A) {return sqrt(Dot(A, A));} //向量的模
double Angle(Vector A, Vector B) {return acos(Dot(A, B) / Length(A) / Length(B));} //向量夹角
double Cross(Vector A, Vector B) {return A.x * B.y - A.y * B.x;} //叉积
double Area2(Point A, Point B, Point C) {return Cross(B - A, C - A);} //有向面积 //向量旋转
Vector Rotate(Vector A, double rad) {
return Vector(A.x * cos(rad) - A.y * sin(rad), A.x * sin(rad) + A.y * cos(rad));
} //推断3点共线
bool LineCoincide(Point p1, Point p2, Point p3) {
return dcmp(Cross(p2 - p1, p3 - p1)) == 0;
} //推断向量平行
bool LineParallel(Vector v, Vector w) {
return Cross(v, w) == 0;
} //推断向量垂直
bool LineVertical(Vector v, Vector w) {
return Dot(v, w) == 0;
} //计算两直线交点,平行,重合要先推断
Point GetLineIntersection(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 DistanceToLine(Point P, Point A, Point B) {
Vector v1 = B - A, v2 = P - A;
return fabs(Cross(v1, v2)) / Length(v1);
} //点到线段距离
double DistanceToSegment(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 GetLineProjection(Point P, Point A, Point B) {
Vector v = B - A;
return A + v * (Dot(v, P - A) / Dot(v, v));
} //线段相交判定(规范相交)
bool SegmentProperIntersection(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);
//dcmp(c1) * dcmp(c2) == 0 || dcmp(c3) * dcmp(c4) == 0为不规范相交
return dcmp(c1) * dcmp(c2) <= 0;// && dcmp(c3) * dcmp(c4) <= 0;
} //推断点在线段上, 不包括端点
bool OnSegment(Point p, Point a1, Point a2) {
return dcmp(Cross(a1 - p, a2 - p)) == 0 && dcmp(Dot(a1 - p, a2 - p)) < 0;
} //n边形的面积
double PolygonArea(Point *p, int n) {
double area = 0;
for (int i = 1; i < n - 1; i++)
area += Cross(p[i] - p[0], p[i + 1] - p[0]);
return area / 2;
} const int N = 105; int t, n; struct Line {
Point a, b;
void read() {
a.read();
b.read();
}
} line[N]; bool judge(Point a, Point b) {
if (dcmp(a.x - b.x) == 0 && dcmp(a.y - b.y) == 0) return false;
for (int i = 0; i < n; i++)
if (!SegmentProperIntersection(a, b, line[i].a, line[i].b)) return false;
return true;
} bool gao() {
for (int i = 0; i < n; i++) {
if (judge(line[i].a, line[i].b)) return true;
for (int j = 0; j < i; j++) {
if (judge(line[i].a, line[j].a)) return true;
if (judge(line[i].a, line[j].b)) return true;
if (judge(line[i].b, line[j].a)) return true;
if (judge(line[i].b, line[j].b)) return true;
}
}
return false;
} int main() {
scanf("%d", &t);
while (t--) {
scanf("%d", &n);
for (int i = 0; i < n; i++)
line[i].read();
if (gao()) printf("Yes!\n");
else printf("No!\n");
}
return 0;
}
版权声明:本文博客原创文章,博客,未经同意,不得转载。
POJ 3304 Segments(计算几何)的更多相关文章
- POJ 3304 Segments(计算几何:直线与线段相交)
POJ 3304 Segments 大意:给你一些线段,找出一条直线可以穿过全部的线段,相交包含端点. 思路:遍历全部的端点,取两个点形成直线,推断直线是否与全部线段相交,假设存在这种直线,输出Yes ...
- POJ 3304 Segments 判断直线和线段相交
POJ 3304 Segments 题意:给定n(n<=100)条线段,问你是否存在这样的一条直线,使得所有线段投影下去后,至少都有一个交点. 思路:对于投影在所求直线上面的相交阴影,我们可以 ...
- POJ 3304 Segments(判断直线与线段是否相交)
题目传送门:POJ 3304 Segments Description Given n segments in the two dimensional space, write a program, ...
- POJ 3304 Segments (判断直线与线段相交)
题目链接:POJ 3304 Problem Description Given n segments in the two dimensional space, write a program, wh ...
- POJ 3304 Segments 基础线段交判断
LINK 题意:询问是否存在直线,使得所有线段在其上的投影拥有公共点 思路:如果投影拥有公共区域,那么从投影的公共区域作垂线,显然能够与所有线段相交,那么题目转换为询问是否存在直线与所有线段相交.判断 ...
- 2018.07.04 POJ 3304 Segments(简单计算几何)
Segments Time Limit: 1000MS Memory Limit: 65536K Description Given n segments in the two dimensional ...
- POJ 3304 Segments (直线和线段相交判断)
Segments Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7739 Accepted: 2316 Descript ...
- poj 3304 Segments
Segments 题意:给你100以内的n条线段,问你是否存在一条直线,使得题给的线段在这条直线上的“投影” 相交于一点: 思路: 1.先要将线段投影相交于一点转变为存在一条直线与所有的线段相交: 很 ...
- poj 3304 Segments(计算几何基础)
Segments Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11593 Accepted: 3657 Descr ...
随机推荐
- KMP算法解析
介绍一种高效的KMP算法:代码可以直接运行 #include <iostream> #include <iomanip> using namespace std; void p ...
- select into from 和 insert into select 的用法和区别(转)
转自:http://www.studyofnet.com/news/182.html select into from 和 insert into select都是用来复制表,两者的主要区别为: se ...
- jquery选择器控制Html元素
1.JQuery中有addClass,removeClass,toggleClass addClass(class):为每个匹配的元素添加指定的类名 removeClass(class):从所有匹配的 ...
- 查询sql语句耗时的方法!
declare @times datetimeset @times=getdate()--要查询的sql语句select [注册数花费时间(毫秒)]=datediff(ms,@times,getdat ...
- 腾讯QQ是用什么语言开发的(转)
腾讯QQ的部分COM组件用的VC6,用exescope看其中几个dll的依赖,依赖于MFC42.dll,MSVCRT.dll,MSVCP60.dll都说明是VC6写的. 还有一部分用的VS2005,包 ...
- 转:30分钟掌握STL
三十分钟掌握STL 这是本小人书.原名是<using stl>,不知道是谁写的.不过我倒觉得很有趣,所以化了两个晚上把它翻译出来.我没有对翻译出来的内容校验过.如果你没法在三十分钟内觉得有 ...
- ThinkPHP 3.1.2 模板的使用技巧
本节课大纲: 一.模板包含 <include file="完整模板文件名" /> <include file="./Tpl/default/Public ...
- Uva 11694 Gokigen Naname
基本思路是Dfs: 1. 一个一个格子摆放,以每个各自的左上角的点为基准点代表格子,比如(0,0)代表(0,0)(0,1)(1,0)(1,1)组成的格子,(0,1)代表(0,1)(0,2)(1,1), ...
- poj 3984 迷宫问题(dfs)
题目链接:http://poj.org/problem?id=3984 思路:经典型的DFS题目.搜索时注意剪枝:越界处理,不能访问处理. 代码: #include <iostream> ...
- Distinguishing Between Embedded and General-Purpose Computing
标题:嵌入式系统与通用计算机系统的区别 To understand what falls into the category of embedded computing ,it is instruct ...