zoj3728_Collision(简单计算几何)

来源:互联网 发布:淘宝店铺装修公司 编辑:程序博客网 时间:2024/05/29 02:39

///////////////////////////////////////////////////////////////////////////////////////////////////////
作者:tt2767
声明:本文遵循以下协议自由转载-非商用-非衍生-保持署名|Creative Commons BY-NC-ND 3.0
查看本文更新与讨论请点击:http://blog.csdn.net/tt2767
链接被删请百度: CSDN tt2767
///////////////////////////////////////////////////////////////////////////////////////////////////////


题解:这题看明白之后主要是套模板去算。
1.用点积(dot)去判断是否不经过圆
2.用叉积求出原点到直线的距离
3.两种情况:撞击后反弹 或者 不撞击
4.记得只保留3位小数

#include<sstream>#include<string>#include<iostream>#include<algorithm>#include<cstdio>#include<cstdlib>#include<cstring>#include<math.h>#include <iterator>#include<map>#include <stack>#include<queue>#include<set>#include <list>#include<functional>#include<numeric>using namespace std;inline int lowbit(int x){ return x&(-x);}struct Point{    double x,y;    Point(double x,double y):x(x),y(y){}    Point(){}};typedef Point Vector;double cross(Vector a , Vector b){    return a.x*b.y-a.y*b.x;}double dot(Vector a , Vector b){    return a.x*b.x+a.y*b.y;}typedef  long long int LL;const double eps = 1e-9;const int INF = 0x3f3f3f3f ;const long double PI = acos(0.0) * 2.0;const int N = 100009;int main(){    double Rm,R,r,x,y,vx,vy;    while(scanf("%lf%lf%lf%lf%lf%lf%lf",&Rm,&R,&r,&x,&y,&vx,&vy)==7 )    {        Point p(x,y);        Vector s(0-x,0-y),v(vx,vy);        Rm+=r;        R+=r;        double vel = sqrt(v.x*v.x+v.y*v.y);        double d = fabs(cross(s,v))/vel;        if(dot(s,v) < eps || d-R > eps)            printf("0.000\n");        else if(d - Rm > eps)            printf("%.3lf\n",2*sqrt(R*R-d*d)/vel);        else            printf("%lf\n",2*( sqrt(R*R-d*d) -  sqrt(Rm*Rm-d*d) )/vel );    }    return 0;}
0 0
原创粉丝点击