poj3449(判断直线相交)
题目链接:https://vjudge.net/problem/POJ-3449
题意:给出若干几何体,判断每个几何体与其它几何体的相交情况,并依次输出。
思路:
首先要知道的是根据正方形对角线的两个点怎么求其它两个点,比如已知(x0,y0),(x2,y2),那么:
x1+x3=x0+x2,
x1-x3=y2-y0,
y1+y3=y0+y2,
y1-y3=x0-x2
之后就暴力枚举了,枚举所有几何体的所有边,用线段相交判断几何体相交。这题的输入输出很恶心。
AC代码:
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cmath>
#include<vector>
#include<cstring>
using namespace std; const double eps=1e-;
int sgn(double x){
if(abs(x)<eps) return ;
else if(x<) return -;
else return ;
} struct Point{
double x,y;
Point(){}
Point(double xx,double yy){
x=xx,y=yy;
}
Point operator + (const Point& b)const{
return Point(x+b.x,y+b.y);
}
Point operator - (const Point& b)const{
return Point(x-b.x,y-b.y);
}
double operator ^ (const Point& b)const{
return x*b.y-y*b.x;
}
}; struct Line{
Point s,e;
Line(){}
Line(Point ss,Point ee){
s=ss,e=ee;
}
}; bool inter(Line l1,Line l2){
return
max(l1.s.x,l1.e.x)>=min(l2.s.x,l2.e.x)&&
max(l2.s.x,l2.e.x)>=min(l1.s.x,l1.e.x)&&
max(l1.s.y,l1.e.y)>=min(l2.s.y,l2.e.y)&&
max(l2.s.y,l2.e.y)>=min(l1.s.y,l1.e.y)&&
sgn((l2.s-l1.e)^(l1.s-l1.e))*sgn((l2.e-l1.e)^(l1.s-l1.e))<=&&
sgn((l1.s-l2.e)^(l2.s-l2.e))*sgn((l1.e-l2.e)^(l2.s-l2.e))<=;
} struct node{
char id;
int num;
Point pt[];
}sp[]; bool cmp(node a,node b){
return a.id<b.id;
} bool check(node a,node b){
for(int i=;i<a.num;++i)
for(int j=;j<b.num;++j)
if(inter(Line(a.pt[i],a.pt[(i+)%a.num]),Line(b.pt[j],b.pt[(j+)%b.num])))
return true;
return false;
} vector<char> vc;
int n;
char str[]; int main(){
while(scanf("%s",str),str[]!='.'){
sp[].id=str[];
scanf("%s",str);
if(strcmp(str,"square")==){
sp[].num=;
scanf(" (%lf,%lf)",&sp[].pt[].x,&sp[].pt[].y);
scanf(" (%lf,%lf)",&sp[].pt[].x,&sp[].pt[].y);
sp[].pt[].x=(sp[].pt[].x+sp[].pt[].x+(sp[].pt[].y-sp[].pt[].y))/;
sp[].pt[].x=(sp[].pt[].x+sp[].pt[].x-(sp[].pt[].y-sp[].pt[].y))/;
sp[].pt[].y=(sp[].pt[].y+sp[].pt[].y+(sp[].pt[].x-sp[].pt[].x))/;
sp[].pt[].y=(sp[].pt[].y+sp[].pt[].y-(sp[].pt[].x-sp[].pt[].x))/;
}
else if(strcmp(str,"rectangle")==){
sp[].num=;
scanf(" (%lf,%lf)",&sp[].pt[].x,&sp[].pt[].y);
scanf(" (%lf,%lf)",&sp[].pt[].x,&sp[].pt[].y);
scanf(" (%lf,%lf)",&sp[].pt[].x,&sp[].pt[].y);
sp[].pt[].x=sp[].pt[].x+(sp[].pt[].x-sp[].pt[].x);
sp[].pt[].y=sp[].pt[].y+(sp[].pt[].y-sp[].pt[].y);
}
else if(strcmp(str,"line")==){
sp[].num=;
scanf(" (%lf,%lf)",&sp[].pt[].x,&sp[].pt[].y);
scanf(" (%lf,%lf)",&sp[].pt[].x,&sp[].pt[].y);
}
else if(strcmp(str,"triangle")==){
sp[].num=;
scanf(" (%lf,%lf)",&sp[].pt[].x,&sp[].pt[].y);
scanf(" (%lf,%lf)",&sp[].pt[].x,&sp[].pt[].y);
scanf(" (%lf,%lf)",&sp[].pt[].x,&sp[].pt[].y);
}
else{
scanf("%d",&sp[].num);
for(int i=;i<sp[].num;++i)
scanf(" (%lf,%lf)",&sp[].pt[i].x,&sp[].pt[i].y);
}
n=;
while(scanf("%s",str),str[]!='-'){
sp[n].id=str[];
scanf("%s",str);
if(strcmp(str,"square")==){
sp[n].num=;
scanf(" (%lf,%lf)",&sp[n].pt[].x,&sp[n].pt[].y);
scanf(" (%lf,%lf)",&sp[n].pt[].x,&sp[n].pt[].y);
sp[n].pt[].x=(sp[n].pt[].x+sp[n].pt[].x+(sp[n].pt[].y-sp[n].pt[].y))/;
sp[n].pt[].x=(sp[n].pt[].x+sp[n].pt[].x-(sp[n].pt[].y-sp[n].pt[].y))/;
sp[n].pt[].y=(sp[n].pt[].y+sp[n].pt[].y+(sp[n].pt[].x-sp[n].pt[].x))/;
sp[n].pt[].y=(sp[n].pt[].y+sp[n].pt[].y-(sp[n].pt[].x-sp[n].pt[].x))/;
}
else if(strcmp(str,"rectangle")==){
sp[n].num=;
scanf(" (%lf,%lf)",&sp[n].pt[].x,&sp[n].pt[].y);
scanf(" (%lf,%lf)",&sp[n].pt[].x,&sp[n].pt[].y);
scanf(" (%lf,%lf)",&sp[n].pt[].x,&sp[n].pt[].y);
sp[n].pt[].x=sp[n].pt[].x+(sp[n].pt[].x-sp[n].pt[].x);
sp[n].pt[].y=sp[n].pt[].y+(sp[n].pt[].y-sp[n].pt[].y);
}
else if(strcmp(str,"line")==){
sp[n].num=;
scanf(" (%lf,%lf)",&sp[n].pt[].x,&sp[n].pt[].y);
scanf(" (%lf,%lf)",&sp[n].pt[].x,&sp[n].pt[].y);
}
else if(strcmp(str,"triangle")==){
sp[n].num=;
scanf(" (%lf,%lf)",&sp[n].pt[].x,&sp[n].pt[].y);
scanf(" (%lf,%lf)",&sp[n].pt[].x,&sp[n].pt[].y);
scanf(" (%lf,%lf)",&sp[n].pt[].x,&sp[n].pt[].y);
}
else{
scanf("%d",&sp[n].num);
for(int i=;i<sp[n].num;++i)
scanf(" (%lf,%lf)",&sp[n].pt[i].x,&sp[n].pt[i].y);
}
++n;
}
sort(sp,sp+n,cmp);
for(int i=;i<n;++i){
printf("%c ",sp[i].id);
vc.clear();
for(int j=;j<n;++j)
if(j!=i)
if(check(sp[i],sp[j]))
vc.push_back(sp[j].id);
if(vc.size()==){
printf("has no intersections\n");
}
else if(vc.size()==){
printf("intersects with %c\n",vc[]);
}
else if(vc.size()==){
printf("intersects with %c and %c\n",vc[],vc[]);
}
else{
printf("intersects with ");
for(int j=;j<vc.size()-;++j)
printf("%c, ",vc[j]);
printf("and %c\n",vc[vc.size()-]);
}
}
printf("\n");
}
return ;
}
poj3449(判断直线相交)的更多相关文章
- Revit API判断直线相交关系移动风管
start ) ); )) )) ); XYZ xyz12 = lCurve1.Curve.get_EndPoint(); XY ...
- [poj] 1066 Treasure Hunt || 判断直线相交
原题 在金字塔内有一个宝藏p(x,y),现在要取出这个宝藏. 在金字塔内有许多墙,为了进入宝藏所在的房间必须把墙炸开,但是炸墙只能炸每个房间墙的中点. 求将宝藏运出城堡所需要的最小炸墙数. 判断点和直 ...
- POJ 1269 Intersecting Lines【判断直线相交】
题意:给两条直线,判断相交,重合或者平行 思路:判断重合可以用叉积,平行用斜率,其他情况即为相交. 求交点: 这里也用到叉积的原理.假设交点为p0(x0,y0).则有: (p1-p0)X(p2-p0) ...
- CC34:判断直线相交
题目 解法 水题,判断斜率.判断截距,ok..... class CrossLine { public: bool checkCrossLine(double s1, double s2, doubl ...
- poj 1127(直线相交+并查集)
Jack Straws Description In the game of Jack Straws, a number of plastic or wooden "straws" ...
- poj 1269 Intersecting Lines(直线相交)
Intersecting Lines Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 8637 Accepted: 391 ...
- POJ 1269 Intersecting Lines(直线相交判断,求交点)
Intersecting Lines Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 8342 Accepted: 378 ...
- 判断线段和直线相交 POJ 3304
// 判断线段和直线相交 POJ 3304 // 思路: // 如果存在一条直线和所有线段相交,那么平移该直线一定可以经过线段上任意两个点,并且和所有线段相交. #include <cstdio ...
- 判断直线与线段相交 POJ 3304 Segments
题意:在二维平面中,给定一些线段,然后判断在某直线上的投影是否有公共点. 转化,既然是投影,那么就是求是否存在一条直线L和所有的线段都相交. 证明: 下面给出具体的分析:先考虑一个特殊的情况,即n=1 ...
随机推荐
- PHP mysqli_error() 函数
返回最近调用函数的最后一个错误描述: <?php // 假定数据库用户名:root,密码:123456,数据库:RUNOOB $con=mysqli_connect("localh ...
- 2018 Nowcoder Multi-University Training Contest 10
Practice Link J. Rikka with Nickname 题意: 给出\(n\)个字符串,要求依次合并两个串\(s, t\),满足将\(t\)合并到\(s\)中变成\(r\),使得\( ...
- zabbix之自定义告警
zabbix支持内置的告警类型.email,sms,等 有时候需要自定义类型的. [其他微信,钉钉都差不多方式,只是脚本不一样] 自定义告警类型[自定义邮件] 编写自定义脚本,并测试成功. [脚本需要 ...
- MySQL中使用LIMIT分页
需求:客户端通过传递pageNum(页码)和pageSize(每页显示的条数)两个参数去分页查询数据库表中的数据. 我们知道MySQL提供了分页函数limit m,n,但是该函数的用法和需求不一样,所 ...
- 虚拟机安装WIN7教程
1.去下载win7原装镜像,推荐去官方网站下载:https://msdn.itellyou.cn/ 也可以直接使用Win7系统和激活工具链接:https://pan.baidu.com/s/1SJSE ...
- java代理,手把手交你写java代理
一:常用的java代理模式 一般经常做java开发的知道java的代理模式一共有三种,第一种也就是静态代理,这种用法比较简单,没有什么魔法棒,比较好理解,另外两种分别是JDK代理和cglib代理,他们 ...
- Python学习日记(六)——内置函数和文件操作(lambda)
lambda表达式 学习条件运算时,对于简单的 if else 语句,可以使用三元运算来表示,即: # 普通条件语句 if 1 == 1: name = 'prime' else: name = 'c ...
- 应用fluent二维单向流泥沙冲刷作用下河床变形代码【转载】
本代码转载自:http://www.codeforge.cn/read/260028/keti_2d_b.c__html #include "udf.h" #define Rho ...
- Hadoop环境搭建|第二篇:hadoop环境搭建
硬件配置:1台NameNode节点.2台DataNode节点 一.Linux环境配置 这里我只配置NameNode节点,DataNode节点的操作相同. 1.1.修改主机名 命令:vi /etc/sy ...
- tp5 回滚事务记录,其中一条语句报错,全部回滚
#################################### 测试事务 // 启动事务 Db::startTrans(); try { //插入行为表 $data = [ 'userId' ...