Out-out-control cars
来源:互联网 发布:劳动法律师事务所知乎 编辑:程序博客网 时间:2024/06/05 00:47
新疆网络赛的几何题,当时没做出来,分别将两个三角形看成不动,只有一个三角形在动,那么问题就转变为求一个射线与线段是否有交点,我们可以在求两条直线交点的基础上再做处理:
1.要求交点在线段范围内(横纵坐标满足)
2.要求交点与射线的向量同向
#include<bits/stdc++.h>using namespace std;const double eps=1e-9; int sgn(double x) { if( x < -eps ) return -1; if( x > eps ) return 1; return 0; }struct vec { double x,y; vec(){x=y=0;} vec(double _x,double _y){x=_x,y=_y;} vec operator + (vec v) {return vec(x+v.x,y+v.y);} vec operator - (vec v) {return vec(x-v.x,y-v.y);} vec operator * (double v) {return vec(x*v,y*v);} vec operator / (double v) {return vec(x/v,y/v);} double operator * (vec v) {return x*v.x + y*v.y;} double len() {return hypot(x,y); } double len_sqr() {return x*x + y*y; } //逆时针旋转 vec rotate(double c) {return vec(x*cos(c)-y*sin(c),x*sin(c)+y*cos(c));} vec trunc (double l) {return (*this) * l / len();} vec rot90 () {return vec(-y,x);}}v[20][32];double cross(vec a,vec b) {return a.x*b.y - a.y*b.x;} //直线求交点,需保证p!=q,a!=bint line_intersection(vec A,vec B,vec C,vec D,vec &o) { // cout<<A.x<<' '<<A.y<<endl; double a1,a2,b1,b2,c1,c2; double Delta , Delta_x , Delta_y; a1 = B.x - A.x; b1 = C.x - D.x; c1 = C.x - A.x; a2 = B.y - A.y; b2 = C.y - D.y; c2 = C.y - A.y; Delta=a1*b2-a2*b1; Delta_x=c1*b2-c2*b1;Delta_y=a1*c2-a2*c1; if(Delta){ o.x = A.x+a1*(Delta_x/Delta); o.y = A.y+a2*(Delta_x/Delta); return 1; //返回1: 表示两条直线相交,且交点是(x , y) } else { if(!Delta_x && !Delta_y) return -1; //返回是-1: 表示两条直线是重合关系 else return 0; //返回0:表示两条直线是平行不相交关系 }}bool judge(vec a,vec b,vec p,vec q,double dx) //a,b三角形的点{ // cout<<"dx="<<dx<<endl; vec o; double t; int res=line_intersection(a,b,p,q,o); t=(o.x-p.x)/dx; // cout<<"t="<<t<<endl; // cout<<o.x<<' '<<o.y<<endl; if(t>0&&res==1&&o.x>=min(a.x,b.x)&&o.x<=max(a.x,b.x)&&o.y>=min(a.y,b.y)&&o.y<=max(a.y,b.y)) { return true; } return false;}int main(){ int t; cin>>t; int ti=0; while(t--) { for(int i=0;i<2;i++) for(int j=0;j<4;j++) { scanf("%lf%lf",&v[i][j].x,&v[i][j].y); } bool flag=false; double dx=v[1][3].x-v[0][3].x; double dy=v[1][3].y-v[0][3].y; for(int i=0;i<3;i++) { for(int j=0;j<3;j++) { vec vv(v[1][j].x+dx,v[1][j].y+dy); vec vvv(v[0][j].x-dx,v[0][j].y-dy); if(judge(v[0][i],v[0][(i+1)%2],v[1][j],vv,dx)) flag=true; if(judge(v[1][i],v[1][(i+1)%2],v[0][j],vvv,-dx)) flag=true; } } if (flag) printf("Case #%d: YES\n",++ti); else printf("Case #%d: NO\n",++ti); }}
阅读全文
0 0
- Out-out-control cars
- 2017乌鲁木齐网赛 B.Out-out-control cars
- 2017 ACM-ICPC乌鲁木齐网络赛 B. Out-out-control cars(几何)
- 2017 ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛 Out-out-control cars
- 2017 ACM-ICPC乌鲁木齐网络赛 B. Out-out-control cars【计算几何||判断射线与线段是否相交】
- [计蒜客16951] Out-out-control cars [2017 ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛 B]
- out of control
- out
- out
- out
- Out
- out
- Everything can be out of control
- The WPF Tab Control - Inside and Out
- The WPF Tab Control - Inside and Out
- CONTROL OUT transfer failed with Error Code:997
- out函数
- ref&out
- iOS 视频音频播放器
- python处理图像的相关程序
- 一张图告诉你17款MBP和16款MBP的区别
- CodeForces
- 把普通应用打包成系统应用
- Out-out-control cars
- Hibernate面试题详解
- 添加 删除 修改表单
- CF B. Working out dp 递推
- 28、数据结构笔记之二十八数组之矩阵
- zoj-2110-Tempter of the Bone-DFS-剪枝-java
- swift学习记录(1)两种textfield隐藏键盘的方法
- springCloud(八)
- 洛谷P1445(数学+画柿子+线性筛)