POJ 1410 Intersection G++判断两条线段是否相交 背

xiaoxiao2021-02-28  10

#include <iostream> #include <cstdio> #include <algorithm> using namespace std; //英语 看博友注释 抄博友程序 判断两条线段是否相交 背 struct point{ double x; double y; }; point rec[4]; point sa,ea; double cross(point a, point b, point c) { return (a.x-b.x)*(c.y-b.y)-(a.y-b.y)*(c.x-b.x); } bool intersection(point s1,point e1,point s2,point e2)//判断两条线段是否相交 { return (max(s1.x,e1.x)>=min(s2.x,e2.x)&& max(s2.x,e2.x)>=min(s1.x,e1.x)&& max(s1.y,e1.y)>=min(s2.y,e2.y)&& max(s2.y,e2.y)>=min(s1.y,e1.y)&& cross(s2,e2,e1)*cross(e2,s2,s1)>=0&& cross(e2,s2,e1)*cross(s2,e2,s1)>=0); } bool in(point a)//抄博友程序 需使用min max { return (a.y>=min(rec[0].y,rec[1].y)&& a.y<=max(rec[0].y,rec[1].y)&& a.x>=min(rec[0].x,rec[3].x)&& a.x<=max(rec[0].x,rec[3].x)); } int main() { int T; cin>>T; while(T--) { cin>>sa.x>>sa.y>>ea.x>>ea.y; cin>>rec[0].x>>rec[0].y>>rec[2].x>>rec[2].y; rec[1].x=rec[0].x; rec[1].y=rec[2].y; rec[3].x=rec[2].x; rec[3].y=rec[0].y; int flag=0;//不相交 if(in(sa)||in(ea)) { //cout<<"hi0"<<endl; flag=1; }else { if(intersection(rec[0],rec[1],sa,ea)) { //cout<<"hi1"<<endl; flag=1; } if(intersection(rec[0],rec[3],sa,ea)) { //cout<<"hi2"<<endl; flag=1; } if(intersection(rec[3],rec[2],sa,ea)) { //cout<<"hi3"<<endl; flag=1; } if(intersection(rec[1],rec[2],sa,ea)) { //cout<<"hi4"<<endl; flag=1; } } if(flag) { cout<<"T"<<endl; }else { cout<<"F"<<endl; } } return 0; }

 

转载请注明原文地址: https://www.6miu.com/read-800273.html

最新回复(0)