计算几何--判断两条线段相交--poj 2653
| Time Limit: 3000MS | Memory Limit: 65536K | |
| Total Submissions: 8862 | Accepted: 3262 |
Description
stick is always on top but he wants to know all the sticks that are on top. Stan sticks are very, very thin such that their thickness can be neglected.
Input
are listed in the order in which Stan has thrown them. You may assume that there are no more than 1000 top sticks. The input is ended by the case with n=0. This case should not be processed.
Output
The picture to the right below illustrates the first case from input.

Sample Input
5
1 1 4 2
2 3 3 1
1 -2.0 8 4
1 4 8 2
3 3 6 -2.0
3
0 0 1 1
1 0 2 1
2 0 3 1
0
Sample Output
Top sticks: 2, 4, 5.
Top sticks: 1, 2, 3.
Hint
Source
#include "cstdio"
#include "cmath"
#include "vector"
#include "iostream" using namespace std;
const double eps = 1e-8; double max(double a,double b){ return a>b?a:b; }
double min(double a,double b){ return a<b?a:b; } int cmp(double x){
if(fabs(x)<eps) return 0;
if(x>0) return 1;
return -1;
} inline double sqr(double x){
return x*x;
} struct point{ //点结构体
double x,y;
point(){}
point (double a,double b):x(a),y(b) {} //重载
void input(){
scanf("%lf%lf",&x,&y);
}
friend point operator + (const point a,const point b){
return point(a.x+b.x,a.y+b.y);
}
friend point operator - (const point a,const point b){
return point(a.x-b.x,a.y-b.y);
}
}; double det(const point &a,const point &b){ //向量a与向量b的叉积
return a.x*b.y-a.y*b.x;
} struct line{ //线结构体
point a,b;
line(){}
line(point x,point y):a(x),b(y){}
}; bool line_make_point_one(line a,line b){ //判断两线段是否相交,完美代码!
return
max(a.a.x,a.b.x) >= min(b.a.x,b.b.x) && //前四行判断两向量所形成的矩形是否相交,排除两线段在同一条直线但不相交的可能
max(b.a.x,b.b.x) >= min(a.a.x,a.b.x) &&
max(a.a.y,a.b.y) >= min(b.a.y,b.b.y) &&
max(b.a.y,b.b.y) >= min(a.a.y,a.b.y) &&
cmp(det(a.a-b.b,b.a-b.b))*cmp(det(a.b-b.b,b.a-b.b))<=0 && //判断两线段是否相交
cmp(det(b.a-a.a,a.b-a.a))*cmp(det(b.b-a.a,a.b-a.a))<=0;
} int main(){
int n;
while(scanf("%d",&n),n!=0)
{
line a;
vector<line> p; //线段向量
vector<int> v; //记录线段向量的下标
p.clear();
v.clear(); scanf("%lf %lf %lf %lf",&a.a.x,&a.a.y,&a.b.x,&a.b.y);
p.push_back(a);
v.push_back(1);
for(int k=2;k<=n;++k)
{
scanf("%lf %lf %lf %lf",&a.a.x,&a.a.y,&a.b.x,&a.b.y);
for(int i=0; i<(int)p.size(); ++i)
{
bool flag = line_make_point_one(a,p[i]);
if(flag==true)
{
p.erase(p.begin()+i);
v.erase(v.begin()+i);
i--;
}
}
p.push_back(a);
v.push_back(k);
}
printf("Top sticks:");
int i;
for( i=0; i<(int)v.size()-1; ++i)
printf(" %d,",v[i]);
printf(" %d.\n",v[i]);
}
return 0;
}
计算几何--判断两条线段相交--poj 2653的更多相关文章
- Pick-up sticks(判断两条线段是否相交)
Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 8351 Accepted: 3068 Description Stan has ...
- [CSharpTips]判断两条线段是否相交
判断两条线段是否相交 主要用到了通过向量积的正负判断两个向量位置关系 向量a×向量b(×为向量叉乘),若结果小于0,表示向量b在向量a的顺时针方向:若结果大于0,表示向量b在向量a的逆时针方向:若等于 ...
- 线段相交 POJ 2653
// 线段相交 POJ 2653 // 思路:数据比较水,据说n^2也可以过 // 我是每次枚举线段,和最上面的线段比较 // O(n*m) // #include <bits/stdc++.h ...
- poj 1127 -- Jack Straws(计算几何判断两线段相交 + 并查集)
Jack Straws In the game of Jack Straws, a number of plastic or wooden "straws" are dumped ...
- c# 判断两条线段是否相交(判断地图多边形是否相交)
private void button1_Click(object sender, EventArgs e) { //var result = intersect3(point1, point2, p ...
- 简单几何(线段相交) POJ 2653 Pick-up sticks
题目传送门 题意:就是小时候玩的一种游戏,问有多少线段盖在最上面 分析:简单线段相交,队列维护当前最上的线段 /******************************************** ...
- hdu 1086:You can Solve a Geometry Problem too(计算几何,判断两线段相交,水题)
You can Solve a Geometry Problem too Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/3 ...
- 平面内,线与线 两条线找交点 两条线段的位置关系(相交)判定与交点求解 C#
个人亲自编写.测试,可以正常使用 道理看原文,这里不多说 网上找到的几篇基本都不能用的 C#代码 bool Equal(float f1, float f2) { return (Math ...
- Jack Straws(判断线段是否相交 + 并查集)
/** http://acm.tzc.edu.cn/acmhome/problemdetail.do?&method=showdetail&id=1840 题意: 判断线段 ...
随机推荐
- Node.js爬虫抓取数据 -- HTML 实体编码处理办法
cheerio DOM化并解析的时候 1.假如使用了 .text()方法,则一般不会有html实体编码的问题出现 2.如果使用了 .html()方法,则很多情况下(多数是非英文的时候)都会出现,这时, ...
- Linux - Ubuntu下JDK配置
系统版本: ubuntu 14.04 x64JDK版本: jdk-8u60-linux-x64 1.查看系统位数,输入以下命令即可 getconf LONG_BIT 2.下载对应的JDK文件,我这里下 ...
- document.documentElement.clientWidth
document.documentElement.clientWidth 摘自:http://blog.sina.com.cn/s/blog_6f1f9ead0100n1f6.html 关于获取各种浏 ...
- 前端技巧:禁止浏览器static files缓存篇(转)
前端技巧:禁止浏览器static files缓存篇 由于CSS/JS文件经常需要改动,前端调试时是不希望浏览器缓存这些文件的. 本文记录博主的经验. Meta法 目前在chrome调试还没有遇到问题, ...
- mysql服务器io等待高定位与分析
这两天发现公司好几台阿里云ECS上的mysql生产服务器繁忙期间io等待高达百分之二三十(估计九成是没有write back),而且确定是mysql进程产生,由于跑的应用过多,开发和维护无法直接确定哪 ...
- 【GOF23设计模式】组合模式
来源:http://www.bjsxt.com/ 一.[GOF23设计模式]_组合模式.树状结构.杀毒软件架构.JUnite底层架构.常见开发场景 package com.test.composite ...
- java微信开发
所谓的微信开发就是在微信开发模式之下,对微信进行公众号和企业号的扩展开发. 如果要让你的微信公众号有更多的功能,比如菜单支持,自动的信息服务,查询,消息推送等,就必须开启微信的开发模式.进入微 ...
- DigitalOcean上SSH Key的创建(附DigitalOcean邀请)
DigitalOcean是一家云主机商家,最低配置512M内存,20G的SSD,每月只有5刀.半个月前刚刚在这上面买了一个VPS,创建Droplet的时候看见创建SSH Key的时候就有点懵,不知道这 ...
- Javascript中的Label语句
在javascript中,我们可能很少会去用到 Label 语句,但是熟练的应用 Label 语句,尤其是在嵌套循环中熟练应用 break, continue 与 Label 可以精确的返回到你想要的 ...
- 使用WCF对外提供接口
本篇将通过WCF以webservices的方式对外提供接口.同时使用NUnit对webservices中的方法进行单元测试. 开发契约 contract Contract项目为类库项目,该项目下会包含 ...