题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=4595
题意概述:
给出一条射线和N条线段,射线遇到线段会发生反射,令入射角alpha,出射角beta,则beta=alpha*phi_i(即对于每条线段phi是不同的),输出至多10条遇见的线段,没有发生相交的话输出NONE。
N<=100.
分析:
实际上记得板子怎么打还是没什么问题的,问题就是我当时记不得了......
还有一个事情,用余弦定理求两个向量夹角的时候记得-eps控制精度!
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<queue>
#include<set>
#include<map>
#include<vector>
#include<cctype>
using namespace std;
const int maxn=;
const double eps=1e-;
const double pi=acos(-1.0); int X,Y,dx,dy,N;
struct Point{
double x,y;
Point(){ }
Point(double xx,double yy){ x=xx,y=yy; }
}P[maxn]; int cnt=;
typedef Point Vector;
struct Line{
Point p; Vector v;
Line(){ }
Line(Point pp,Vector vv){ p=pp,v=vv; }
};
struct data{
int a,b;
Point p1,p2;
}A[maxn];
Vector operator + (const Vector &a,const Vector &b){ return Vector(a.x+b.x,a.y+b.y); }
Vector operator - (const Vector &a,const Vector &b){ return Vector(a.x-b.x,a.y-b.y); }
Vector operator * (const Vector &a,const double &b){ return Vector(a.x*b,a.y*b); }
int dcmp(double x){ return fabs(x)<eps?:(x>?:-); }
double Dot(const Vector &a,const Vector &b){ return a.x*b.x+a.y*b.y; }
double Cross(const Vector &a,const Vector &b){ return a.x*b.y-a.y*b.x; }
double Length(const Vector &a){ return sqrt(a.x*a.x+a.y*a.y); }
double Angle(const Vector &a,const Vector &b){ return acos(fabs(Dot(a,b))/Length(a)/Length(b)-eps); }
Vector Rotate(const Vector &v,const double &rad){
return Vector(v.x*cos(rad)-v.y*sin(rad),v.y*cos(rad)+v.x*sin(rad));
}
Point GetLineIntersection(const Line &a,const Line &b){
Vector u=a.p-b.p;
double t=Cross(b.v,u)/Cross(a.v,b.v);
return a.p+a.v*t;
}
bool Onsegment(const Point &p,const Point &a,const Point &b){
return dcmp(Dot(a-p,b-p))<=&&dcmp(Cross(a-p,a-p))==;
} void data_in()
{
scanf("%d%d%d%d%d",&X,&Y,&dx,&dy,&N);
for(int i=;i<=N;i++)
scanf("%lf%lf%lf%lf%d%d",&A[i].p1.x,&A[i].p1.y,&A[i].p2.x,&A[i].p2.y,&A[i].a,&A[i].b);
}
void work()
{
int t=;
Point p=Point(X,Y),pp;
Vector v=Vector(dx,dy),vv;
while(t<=){
double md=1e9,dis; int id=;
for(int i=;i<=N;i++){
if(dcmp(Cross(A[i].p1-A[i].p2,v))==) continue;
pp=GetLineIntersection(Line(A[i].p1,A[i].p2-A[i].p1),Line(p,v));
if(Onsegment(pp,A[i].p1,A[i].p2)&&dcmp(Dot(v,pp-p))>){
dis=Length(p-pp);
if(dis<md) md=dis,id=i;
}
}
if(!id) break;
p=GetLineIntersection(Line(A[id].p1,A[id].p2-A[id].p1),Line(p,v));
if(dcmp(Dot(A[id].p1-A[id].p2,v))==) v=v*-;
else{
if(dcmp(Dot(A[id].p1-A[id].p2,v))>) vv=A[id].p1-A[id].p2;
else vv=A[id].p2-A[id].p1;
double alp=pi/-Angle(vv,v);
if(dcmp(Cross(vv,v))>) v=Rotate(vv,alp*A[id].a/A[id].b-pi/);
else v=Rotate(vv,pi/-alp*A[id].a/A[id].b);
}
printf("%d ",id);
t++;
}
if(t==) printf("NONE\n");
}
int main()
{
data_in();
work();
return ;
}