HDU 6798 Triangle Collision (二分+几何)

题意:边长为L的等边三角形内有一颗小球,给出初始位置和速度,求第k次与边碰撞是在什么时候。

题解:二分+几何
把三角形复制成如下图,点每一次反射我们考虑成是经过了多少条边即可。
在这里插入图片描述
对于三角形下面一条边,是x轴,可以直接得到。
对于三角形左右的边,我们要进行旋转,即获得以左右两边为x轴时,点的坐标和速度。

eps改1e-6能过。

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stack>
#include<cmath>
#include<vector>
#include<fstream>
#include<set>
#include<map>
#include<sstream>
#include<iomanip>
#define ll long long
using namespace std;
const double eps = 1e-6;
const double pi = acos(-1.0);
int t;
double l, x, y, vx, vy, k;
double h;
double solve(double y, double vy, double t) {
    
    
	return abs(floor((y + vy * t) / h));
}
bool check(double mid) {
    
    
	double res = solve(y, vy, mid) +
		solve(fabs((y + sqrt(3) * x - h) / 2), (-sqrt(3) * vx - vy) / 2, mid) +
		solve(fabs((-y + sqrt(3) * x + h) / 2), (vx * sqrt(3) - vy) / 2, mid);
	return res >= k;
}
int main() {
    
    
	scanf("%d", &t);
	while (t--) {
    
    
		scanf("%lf%lf%lf%lf%lf%lf", &l, &x, &y, &vx, &vy, &k);
        h = l * sqrt(3) / 2;
		double l = 0, r = 1e11, mid;
		while (r - l > eps) {
    
    
			mid = (l + r) / 2;
			if (check(mid))
				r = mid;
			else
				l = mid;
		}
        printf("%.8f\n", mid);
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_43680965/article/details/107647739