Topic links: Click here
Subject to the effect: Given n line segments, now ask whether there is a straight line, so that all segments of this line there is the projection of a common point of intersection
Topic Analysis: The meaning of the questions a little abstract, need to change it, because there is a projection of a straight line to the intersection of all the segments, then on that straight line, starting from the intersection position, do another line in the direction of a line along the vertical, this line will be found and the n line segments exists a point of intersection, i.e. intersect, this subject was converted to whether there is a straight line, so that line segments have an intersection with the n
Because of the straight line is composed of two points, we enumerate all points on the line segment, as two points on a straight line configuration, each time to determine what the condition is satisfied, the time complexity is (2 * n) * (2 * n) * n, because n is relatively small, so the direct implementation on the line
There is a need to look at the details, there may be a case of coincidence of two points, this time about the need for special sentence
Code:
#include<iostream>
#include<cstdio>
#include<string>
#include<ctime>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<sstream>
using namespace std;
typedef long long LL;
const int inf=0x3f3f3f3f;
const int N=110;
const double eps = 1e-8;
int n;
int sgn(double x){
if(fabs(x) < eps)return 0;
if(x < 0)return -1;
else return 1;
}
struct Point{
double x,y;
Point(){}
Point(double _x,double _y){
x = _x;
y = _y;
}
void input(){
scanf("%lf%lf",&x,&y);
}
double distance(Point p){
return hypot(x-p.x,y-p.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;
}
//点积
double operator *(const Point &b)const{
return x*b.x + y*b.y;
}
};
struct Line{
Point s,e;
Line(){}
Line(Point _s,Point _e){
s = _s;
e = _e;
}
void input(){
s.input();
e.input();
}
//`直线和线段相交判断`
//`-*this line -v seg`
//`2 规范相交`
//`1 非规范相交`
//`0 不相交`
int linecrossseg(Line v){
int d1 = sgn((e-s)^(v.s-s));
int d2 = sgn((e-s)^(v.e-s));
if((d1^d2)==-2) return 2;
return (d1==0||d2==0);
}
}line[N];
bool check(Line l)
{
if(sgn(l.s.distance(l.e))==0)
return false;
for(int i=1;i<=n;i++)
if(!l.linecrossseg(line[i]))
return false;
return true;
}
int main()
{
// freopen("input.txt","r",stdin);
// ios::sync_with_stdio(false);
int w;
cin>>w;
while(w--)
{
vector<Point>point;
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
line[i].input();
point.push_back(line[i].s);
point.push_back(line[i].e);
}
for(int i=0;i<point.size();i++)
for(int j=0;j<point.size();j++)
if(check(Line(point[i],point[j])))
{
puts("Yes!");
goto end;
}
puts("No!");
end:;
}
return 0;
}