- 思路
把每个点和其他的一些点的连线和已知的S-T线段的交点存起来,然后按照距离S点的远近排序。
比赛的时候敲错了一个模板,也怨自己还不是太熟悉计算几何的原理吧。。。
#include<bits/stdc++.h>
using namespace std;
const int N=1e3+10;
struct Point{
double x,y;
double dis;
Point(double x=0,double y=0):x(x),y(y){}
};
typedef Point Vector;
Vector operator + (Vector A,Vector B) {
return Vector(A.x+B.x,A.y+B.y);
}
Vector operator - (Vector A,Vector B) {
return Vector(A.x-B.x,A.y-B.y);
}
Vector operator * (Vector A,double p) {
return Vector(A.x*p,A.y*p);
}
Vector operator / (Vector A,double p) {
return Vector(A.x/p,A.y/p);
}
bool operator < (const Point &a,const Point &b){
return a.x<b.x||(a.x==b.x&&a.y<b.y);
}
const double eps=1e-10;
int dcmp(double x){
if(fabs(x)<eps){
return 0;
}
else return x<0?-1:1;
}
bool operator ==(const Point& a,const Point &b){
return dcmp(a.x-b.x)==0&&dcmp(a.y-b.y)==0;
}
double Dot(Vector A,Vector B){
return A.x*B.x+A.y*B.y;
}
double Length(Vector A){
return sqrt(Dot(A,A));
}
double Angle(Vector A,Vector B) {
return acos(Dot(A,B)/Length(A)/Length(B));
}
double Cross(Vector A,Vector B){
return A.x*B.y-A.y*B.x;
}
Vector Rotate(Vector A,double rad){
return Vector(A.x*cos(rad)-A.y*sin(rad),A.x*sin(rad)+A.y*(rad));
}
Point GetLineIntersection(Point P,Vector v,Vector Q,Vector w){
Vector u=P-Q;
double t=Cross(w,u)/Cross(v,w);
return P+v*t;
}
bool SegmentProperInterSection(Point a1,Point a2,Point b1,Point b2){
double c1=Cross(a2-a1,b1-a1),c2=Cross(a2-a1,b2-a1);
double c3=Cross(b2-b1,a1-b1),c4=Cross(b2-b1,a2-b1);
return dcmp(c1)*dcmp(c2)<0&&dcmp(c3)*dcmp(c4)<0;
}
bool OnSegment(Point p,Point a1,Point a2){
return dcmp(Dot(a1-p,a2-p))<=0;
}
double length(Point &a,Point &b){
return sqrt(pow(fabs(a.x-b.x),2)+pow(fabs(a.y-b.y),2));
}
struct node
{
Point p;
vector<Point> Node;
}a[N];
bool cmp(Point a,Point b){
return a.dis<b.dis;
}
int main()
{
int n,m;
cin>>n>>m;
Point S,T;
cin>>S.x>>S.y>>T.x>>T.y;
for(int i=1;i<=n;i++){
cin>>a[i].p.x>>a[i].p.y;
}
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
if(i==j){
continue;
}
Point temp;
Point P=a[i].p;
Vector v=a[i].p-a[j].p;
Vector w=S-T;
temp=GetLineIntersection(P,v,S,w);
temp.dis=length(temp,S);
if(OnSegment(temp,S,T)){
a[i].Node.push_back(temp);
}
}
}
while(m--){
int h,k;
cin>>h>>k;
if(a[h].Node.size()<k){
cout<<-1<<endl;
}
else{
sort(a[h].Node.begin(),a[h].Node.end(),cmp);
Point X=a[h].Node[k-1];
printf("%.10f %.10f\n",X.x,X.y);
}
}
}