• HDU 4793 Collision【计算机几何】【经典】


    Collision

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 1071    Accepted Submission(s): 402
    Special Judge


    Problem Description
    There's a round medal fixed on an ideal smooth table, Fancy is trying to throw some coins and make them slip towards the medal to collide. There's also a round range which shares exact the same center as the round medal, and radius of the medal is strictly less than radius of the round range. Since that the round medal is fixed and the coin is a piece of solid metal, we can assume that energy of the coin will not lose, the coin will collide and then moving as reflect.
    Now assume that the center of the round medal and the round range is origin ( Namely (0, 0) ) and the coin's initial position is strictly outside the round range.
    Given radius of the medal Rm, radius of coin r, radius of the round range R, initial position (x, y) and initial speed vector (vx, vy) of the coin, please calculate the total time that any part of the coin is inside the round range. Please note that the coin might not even touch the medal or slip through the round range.
     

    Input
    There will be several test cases. Each test case contains 7 integers Rm, R, r, x, y, vx and vy in one line. Here 1 ≤ Rm < R ≤ 2000, 1 ≤ r ≤ 1000, R + r < |(x, y)| ≤ 20000, 1 ≤ |(vx, vy)| ≤ 100.
     

    Output
    For each test case, please calculate the total time that any part of the coin is inside the round range. Please output the time in one line, an absolute error not more than 1e -3 is acceptable.
     

    Sample Input
    5 20 1 0 100 0 -1 5 20 1 30 15 -1 0
     

    Sample Output
    30.000 29.394

    简单的几何问题,但写的过程中有以下问题需要注意:

    判断硬币射向的方向不能用x0/y0=x1*c/y1来写,因为可能会出现精度问题,应该利用圆心在(0,0)这个条件

    利用向量求解:

    a*b=|a|*|b|*cos=√[(m²+n²)*(p²+q²)]*cos=(mp+nq)

    所以cos=(mp+nq)/√[(m²+n²)*(p²+q²)]

    通过判断cos的正负来判断夹角是否为钝角来免去精度问题

    刚开始没有想到利用原点,是通过求与圆的交点,然后利用向量解的,最后判断向量同向的时候还是死在了x0/y0=x1*c/y1上,非常僵硬。


    刚开始写的,改了精度后的代码(AC):

    #include<iostream>    
    #include<algorithm>
    #include<cmath>
    #include<cstdio>
    #include<cstdlib>
    #include<queue>
    #include<map>
    #include<set>
    #include<stack>
    #include<bitset>
    #include<numeric>
    #include<vector>
    #include<string>
    #include<iterator>
    #include<cstring>
    #include<functional>
    #define INF 0x3f3f3f3f
    #define ms(a,b) memset(a,b,sizeof(a))
    using namespace std;
    
    const int maxn = 55;
    const int mod = 1e9 + 7;
    const double pi = acos(-1.0);
    
    typedef pair<int, int> P;
    typedef long long ll;
    typedef unsigned long long ull;
    
    double Rm, R, r, x0, y00, vx, vy;
    
    bool solve()
    {
        double x_, y_;
        double x1 = vx + x0, y1 = vy + y00;
        double px0 = x1 - x0, py0 = y1 - y00;
        if (vx == 0)
        {
            x_ = x0, y_ = sqrt(abs(R*R - x_*x_));
        }
        else
        {
            double a = vy / vx, b = y00 - vy*x0 / vx;
            x_ = (-a*b - sqrt(abs(-b*b + R*R + a*a*R*R))) / (1 + a*a), y_ = b - a*a*b / (1 + a*a) - (a*sqrt(abs(-b*b + R*R + a*a*R*R)) / (1 + a*a));
        }
        double px1 = x_ - x0, py1 = y_ - y00;
        if (px0*px1 + py0*py1 > 0) return 1;
        return 0;
    }
    
    int main()
    {
        while (~scanf("%lf%lf%lf%lf%lf%lf%lf", &Rm, &R, &r, &x0, &y00, &vx, &vy))
        {
            Rm += r;
            R += r;
            double up = abs(y00*vx - x0*vy);
            double down = sqrt(vy*vy + vx*vx);
            double d = up / down;
            double t = 0, len = 0, v = sqrt(vx*vx + vy*vy);
            if (d > R)
            {
                t = 0;
            }
            else
            {
                if (solve())
                {
                    if (d - Rm >= 0.000001)
                    {
                        len = sqrt(abs(R*R - d*d));
                        len *= 2.0;
                    }
                    else
                    {
                        len = sqrt(abs(R*R - d*d));
                        double len2 = sqrt(abs(Rm*Rm - d*d));
                        len -= len2;
                        len *= 2.0;
                    }
                }
            }
            t = len / v;
            printf("%.3f
    ", t);
        }
    }


    利用(0,0)后AC的代码:

    #include<iostream>	
    #include<algorithm>
    #include<cmath>
    #include<cstdio>
    #include<cstdlib>
    #include<queue>
    #include<map>
    #include<set>
    #include<stack>
    #include<bitset>
    #include<numeric>
    #include<vector>
    #include<string>
    #include<iterator>
    #include<cstring>
    #include<functional>
    #define INF 0x3f3f3f3f
    #define ms(a,b) memset(a,b,sizeof(a))
    using namespace std;
    
    const int maxn = 55;
    const int mod = 1e9 + 7;
    const double pi = acos(-1.0);
    
    typedef pair<int, int> P;
    typedef long long ll;
    typedef unsigned long long ull;
    
    double Rm, R, r, x0, y00, vx, vy;
    
    int main()
    {
    	while (~scanf("%lf%lf%lf%lf%lf%lf%lf", &Rm, &R, &r, &x0, &y00, &vx, &vy))
    	{
    		Rm += r;
    		R += r;
    		double up = abs(y00*vx - x0*vy);
    		double down = sqrt(vy*vy + vx*vx);
    		double d = up / down;
    		double t = 0, len = 0, v = sqrt(vx*vx + vy*vy);
    		if (d > R)
    		{
    			t = 0;
    		}
    		else
    		{
    			if (x0*vx + y00*vy < 0)
    			{
    				if (d >= Rm)
    				{
    					len = sqrt(abs(R*R - d*d));
    					len *= 2.0;
    				}
    				else
    				{
    					len = sqrt(abs(R*R - d*d));
    					double len2 = sqrt(abs(Rm*Rm - d*d));
    					len -= len2;
    					len *= 2.0;
    				}
    			}
    		}
    		t = len / v;
    		printf("%.3f
    ", t);
    	}
    }



    Fighting~
  • 相关阅读:
    TransmitFile
    xml
    鼠标划过表格行变色-简洁实现
    关于表变量
    显式接口成员实现
    华为致新员工书
    C#实现的堆栈
    Gridview中合并单元格,某字段的内容相同时如何只显示一个,屏蔽相同列或行的内容(转)
    ASP.NET 验证控件
    动态SQL EXEC
  • 原文地址:https://www.cnblogs.com/Archger/p/12774763.html
Copyright © 2020-2023  润新知