Poj Segments (Use cross multiplication to determine the intersection of line segments)

  • Segments

  • The meaning
    of the question: Give some line segments and ask if there is a straight line so that the projections of these line segments on the straight line have a common point

  • Ideas After
    reading a blog of a great god, I realized that the problem can be transformed into whether there is a straight line m that intersects all the line segments, and the straight line l perpendicular to m is the desired straight line.
    We can enumerate the two-by-two combination of the endpoints of two line segments (the two endpoints are taken from different line segments) as a line segment of the straight line m (actually because the given line segment is limited, so if there is a straight line m, it can Take a segment of the line to intersect all the given line segments), why do we enumerate the line formed by the two endpoints of two different line segments? Assuming this line exists, we can always move the line parallel until it reaches the end point of a certain line segment, and then rotate the line with the end point as the axis, and it will definitely rotate to the end point of another line segment, so It is proved, and it is the line segment that meets the condition (intersecting the given line segment) taken from the required line m. How to judge the intersection of two line segments? We know that a × ba \times ba×b=|a| |b| sin ⁡ θ \sin \theta withoutθ . When the angle between the two vectors is greater than 108°, the product of the cross product is negative (note thata × b ≠ b × aa \times b \not = b \times aa×b=b×a , although the absolute value is the same, the positive and negative are opposite).
    Use the right-hand rule to determine the direction of the cross
    If the line segments ab and cd intersect, there isca ⃗ × cb ⃗ \vec{ca}\times\vec{cb}c a ×cb da ⃗ × db ⃗ \ vec {da} \ times \ vec {db}d a ×db Different sign and da ⃗ × ca ⃗ \vec{da}\times\vec{ca}d a ×c a And db ⃗ × cb ⃗ \vec{db}\times\vec{cb}db ×cb Also different
    Insert picture description here

  • Code

#pragma GCC optimize(2)
//#include<bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>

using namespace std;

typedef long long ll;
typedef unsigned long ul;
typedef unsigned long long ull;
#define pi acos(-1.0)
#define e exp(1.0)
#define pb push_back
#define mk make_pair
#define fir first
#define sec second
#define scf scanf
#define prf printf
typedef pair<ll,ll> pa;
const ll INF=0x3f3f3f3f3f3f3f3f;
const int MAX_N=116;
const double eps=1e-8;
int N,T;
struct node{
    
    
	double x,y;
}seg_l[MAX_N],seg_r[MAX_N];
double gao(node a,node b,node tmp){
    
    
	double x1,x2,y1,y2;
	x1=a.x-tmp.x;
	y1=a.y-tmp.y;
	x2=b.x-tmp.x;
	y2=b.y-tmp.y;
	return x1*y2-x2*y1;
}
bool do_(node a,node b){
    
    
	int i,j;
	if(fabs(a.x-b.x)<eps&&fabs(a.y-b.y)<eps)
	return 0;
	for(i=1;i<=N;i++){
    
    
		if(gao(a,b,seg_l[i])*gao(a,b,seg_r[i])>eps)
		return 0;
	} 
	return 1;
	
} 
int main()
{
    
    
//  freopen(".../.txt","w",stdout);
//  freopen(".../.txt","r",stdin);
	ios::sync_with_stdio(false);
	cin>>T;
	int i,j;
	while(T--){
    
    
		cin>>N;
		for(i=1;i<=N;i++){
    
    
			cin>>seg_l[i].x>>seg_l[i].y>>seg_r[i].x>>seg_r[i].y;
		} 
		if(N==1){
    
    
			cout<<"Yes!"<<endl;
			continue;
		}
		bool flag=0;
		for(i=1;i<=N;i++){
    
    
			for(j=i+1;j<=N;j++){
    
    
				if(do_(seg_l[i],seg_l[j]))
				flag=1;
				else if(do_(seg_l[i],seg_r[j]))
				flag=1;
				else if(do_(seg_r[i],seg_l[j]))
				flag=1;
				else if(do_(seg_r[i],seg_r[j]))
				flag=1;
//				if(flag)
//				break;
			}
			if(flag)
			break;
		}
		if(flag)
		cout<<"Yes!"<<endl;
		else
		cout<<"No!"<<endl;
	}
	return 0;
}

Guess you like

Origin blog.csdn.net/weixin_43311695/article/details/108782366