题目链接: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(判断直线相交)的更多相关文章

  1. Revit API判断直线相交关系移动风管

    start )             );         )) )) );         XYZ xyz12 = lCurve1.Curve.get_EndPoint();         XY ...

  2. [poj] 1066 Treasure Hunt || 判断直线相交

    原题 在金字塔内有一个宝藏p(x,y),现在要取出这个宝藏. 在金字塔内有许多墙,为了进入宝藏所在的房间必须把墙炸开,但是炸墙只能炸每个房间墙的中点. 求将宝藏运出城堡所需要的最小炸墙数. 判断点和直 ...

  3. POJ 1269 Intersecting Lines【判断直线相交】

    题意:给两条直线,判断相交,重合或者平行 思路:判断重合可以用叉积,平行用斜率,其他情况即为相交. 求交点: 这里也用到叉积的原理.假设交点为p0(x0,y0).则有: (p1-p0)X(p2-p0) ...

  4. CC34:判断直线相交

    题目 解法 水题,判断斜率.判断截距,ok..... class CrossLine { public: bool checkCrossLine(double s1, double s2, doubl ...

  5. poj 1127(直线相交+并查集)

    Jack Straws Description In the game of Jack Straws, a number of plastic or wooden "straws" ...

  6. poj 1269 Intersecting Lines(直线相交)

    Intersecting Lines Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 8637   Accepted: 391 ...

  7. POJ 1269 Intersecting Lines(直线相交判断,求交点)

    Intersecting Lines Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 8342   Accepted: 378 ...

  8. 判断线段和直线相交 POJ 3304

    // 判断线段和直线相交 POJ 3304 // 思路: // 如果存在一条直线和所有线段相交,那么平移该直线一定可以经过线段上任意两个点,并且和所有线段相交. #include <cstdio ...

  9. 判断直线与线段相交 POJ 3304 Segments

    题意:在二维平面中,给定一些线段,然后判断在某直线上的投影是否有公共点. 转化,既然是投影,那么就是求是否存在一条直线L和所有的线段都相交. 证明: 下面给出具体的分析:先考虑一个特殊的情况,即n=1 ...

随机推荐

  1. BZOJ 4807 車 组合数学

    思路:高精度\((what)\) 提交:2次(后来发现有种更快的写法) 题解: 设\(n>m\),那么显然答案为\(C(n,m)\),相当于只能放\(m\)个棋子,可以在\(n\)列中选任意不同 ...

  2. javaMail 详解

    原文:http://www.matrix.org.cn/resource/article/44/44101_JavaMail.html 一.JavaMail API简介JavaMail API是读取. ...

  3. java里getPath、 getAbsolutePath、getCanonicalPath的区别

    本文链接:https://blog.csdn.net/wh_19910525/article/details/9314675 File的这三个方法在api中都有说明,仅以程序为例说明. package ...

  4. 012_使用死循环实时显示 eth0 网卡发送的数据包流量

    #!/bin/bash while : do echo '本地网卡 eth0 流量信息如下: ' #grep输出所找整行,awk直接输出第5列 ifconfig eth0 | grep "R ...

  5. learning armbian steps(5) ----- armbian 构建arm rootfs

    基于learning armbian step(4) 的总结,我们来实践一下,接下来的会把整个构建的log都贴出来: vmuser@vmuser-virtual-machine:~/qemu-arm$ ...

  6. 域渗透-企业应用SAML签名攻击

    在项目中遇到SAML企业应用      想留个后门时候一脸懵 随便的整理记录 记录项目中SAML渗透的知识点. 0x01 前置知识  SAML单点登陆 SAML(Security Assertion ...

  7. Go语言 之TCP文件传输

    服务端实现流程大致如下: 创建监听listener,程序结束时关闭. 阻塞等待客户端连接,程序结束时关闭conn. 读取客户端发送文件名.保存fileName. 回发“ok”给客户端做应答 封装函数 ...

  8. Centos 6 can't found command subscription-manager

    [root@localhost ~]# subscription-manager: command not found-bash: -bash:: command not found resoluti ...

  9. php中_initialize()函数与 __construct()函数的区别说明

    _initialize()方法是在任何方法执行之前,都要执行的,当然也包括 __construct构造函数. 也就是说如果存在_initialize()函数,调用对象的任何方法都会导致_initial ...

  10. SQL-W3School-高级:SQL ALTER TABLE 语句

    ylbtech-SQL-W3School-高级:SQL ALTER TABLE 语句 1.返回顶部 1. ALTER TABLE 语句 ALTER TABLE 语句用于在已有的表中添加.修改或删除列. ...