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 ...
随机推荐
- codeforces 623A. Graph and String 构造
题目链接 给出一个图, 每个节点只有三种情况, a,b, c. a能和a, b连边, b能和a, b, c,连边, c能和b, c连边, 且无重边以及自环.给出初始的连边情况, 判断这个图是否满足条件 ...
- Python学习之路——函数
一.Python2.X内置函数表: 注:以上为pyton2.X内置函数,官方网址:https://docs.python.org/2/library/functions.html 二.Python3. ...
- 解决phpmyadmin配置文件的权限问题
如果部署的phpmyadmin权限为所有人可写,即权限为777,就会报”Wrong permissions on configuration file, should not be world wri ...
- 文字适应DIV
今天突然碰到了一个奇怪的问题 那就是对于纯数字和英文字母 文字多了会超出div 且即使是设置了height:auto overflow-y:auto 也不管用 只是在x轴上出现滚动条 不论用 ...
- page分页
首先封装一个分页类 public class Page<T> { /** * 当前页号 */ private int pageNumber; /** * 总条数 */ private in ...
- Debian下Apache配置多域名访问
请见Github博客:http://wuxichen.github.io/Myblog/php/2014/10/10/DebianApacheSetting.html
- 免费的HTML5连载来了《HTML5网页开发实例具体解释》连载(六)媒体查询
响应式设计的还有一个重要技术手段是媒体查询.假设仅仅是简单的设计一个流式布局系统,那么能够保证每一个网格按比例的放大和缩小,但有可能会使得在小屏幕下(如手机设备)网格太小而严重影响阅读,这种设计称不上 ...
- ASP.NET站点安全
<configuration> <appSettings/> <connectionStrings> <add name="MyBookShop&q ...
- 【Hibernate】set排序
使用hibernate进行一对多操作的时候,普遍使用HashSet进行操作.但是HashSet是无序集合,对此可以使用TreeSet进行排序. 1.将HashSet改为TreeSet private ...
- HDU2138 随机素数测试 Miller-Rabin算法
题目描述 Give you a lot of positive integers, just to find out how many prime numbers there are.. In eac ...