HDU - 4637(几何计算+区间覆盖)

本文介绍了一个涉及几何计算的问题,通过定义点、向量、圆等基本几何元素,并利用这些元素进行复杂的几何运算来解决实际问题。包括求解直线与圆的交点、线段是否存在交点、判断点是否在线段上等问题。

题解 :我们可以假设雨滴是不动的,那么妹子就是 每秒向上移动v,每秒向右移动v1,画出该条直接可以看成,直线与半圆的交点和直接与三角形的交点,注意一些可能胖哥提前追上了妹子已经给他伞了那么妹子就不用再淋雨,注意我们求的是线段与线段的交点,最后求一下被覆盖的区间多长再除以妹子的速度即可

#include <cstdio>
#include <cstring>
#include <cmath>
#include <vector>
#include <complex>
#include <algorithm>

using namespace std;
typedef pair<int,int> pii;
const double pi = 4 * atan(1);
const double eps = 1e-8;
const int mx = 1e5+5;
inline int dcmp (double x) { if (fabs(x) < eps) return 0; else return x < 0 ? -1 : 1; }
inline double getDistance (double x, double y) { return sqrt(x * x + y * y); }
inline double torad(double deg) { return deg / 180 * pi; }

struct Point {
    double x, y;
    Point (double x = 0, double y = 0): x(x), y(y) {}
    void read () { scanf("%lf%lf", &x, &y); }
    void write () { printf("%lf %lf", x, y); }

    bool operator == (const Point& u) const { return dcmp(x - u.x) == 0 && dcmp(y - u.y) == 0; }
    bool operator != (const Point& u) const { return !(*this == u); }
    bool operator < (const Point& u) const { return dcmp(x - u.x) < 0 || (dcmp(x-u.x)==0 && dcmp(y-u.y) < 0); }
    bool operator > (const Point& u) const { return u < *this; }
    bool operator <= (const Point& u) const { return *this < u || *this == u; }
    bool operator >= (const Point& u) const { return *this > u || *this == u; }
    Point operator + (const Point& u) { return Point(x + u.x, y + u.y); }
    Point operator - (const Point& u) { return Point(x - u.x, y - u.y); }
    Point operator * (const double u) { return Point(x * u, y * u); }
    Point operator / (const double u) { return Point(x / u, y / u); }
    double operator ^ (const Point& u) { return x*u.y - y*u.x; }
};
typedef Point Vector;

struct Circle {
    Point o;
    double r;
    Circle () {}
    Circle (Point o, double r = 0): o(o), r(r) {}
    void read () { o.read(), scanf("%lf", &r); }
    Point point(double rad) { return Point(o.x + cos(rad)*r, o.y + sin(rad)*r); }
    double getArea (double rad) { return rad * r * r / 2; }
};

/* 点积: 两向量长度的乘积再乘上它们夹角的余弦, 夹角大于90度时点积为负 */
double getDot (Vector a, Vector b) { return a.x * b.x + a.y * b.y; }
/* 叉积: 叉积等于两向量组成的三角形有向面积的两倍, cross(v, w) = -cross(w, v) */
double getCross (Vector a, Vector b) { return a.x * b.y - a.y * b.x; }

double getLength (Vector a) { return sqrt(getDot(a, a)); }
double getAngle (Vector u) { return atan2(u.y, u.x); }
double getAngle (Vector a, Vector b) { return acos(getDot(a, b) / getLength(a) / getLength(b)); }

/* 直线pv和直线qw的交点 */
bool getIntersection (Point p, Vector v, Point q, Vector w, Point& o) {
    if (dcmp(getCross(v, w)) == 0) return false;
    Vector u = p - q;
    double k = getCross(w, u) / getCross(v, w);
    o = p + v * k;
    return true;
}

/* 判断线段是否存在交点 */
bool haveIntersection (Point a1, Point a2, Point b1, Point b2) {
    double c1=getCross(a2-a1, b1-a1), c2=getCross(a2-a1, b2-a1), c3=getCross(b2-b1, a1-b1), c4=getCross(b2-b1,a2-b1);
    return dcmp(c1)*dcmp(c2) < 0 && dcmp(c3)*dcmp(c4) < 0;
}

/* 判断点是否在线段上 */
bool onSegment (Point p, Point a, Point b) { return dcmp(getCross(a-p, b-p)) == 0 && dcmp(getDot(a-p, b-p)) < 0; }

/* 直线和圆的交点 */
int getLineCircleIntersection (Point p, Point q, Circle O, vector<Point>& sol) {
    double t1, t2;
    Vector v = q - p;
	sol.clear();
	Point tmp;
    double a = v.x, b = p.x - O.o.x, c = v.y, d = p.y - O.o.y;
    double e = a*a+c*c, f = 2*(a*b+c*d), g = b*b+d*d-O.r*O.r;
    double delta = f*f - 4*e*g;
    if (dcmp(delta) < 0) return 0;
    if (dcmp(delta) == 0) {
        t1 = t2 = -f / (2 * e);
		if(dcmp((p+v*t1).y-O.o.y)<=0)
			sol.push_back(p + v * t1);
        return 1;
    }
    t1 = (-f - sqrt(delta)) / (2 * e); if(dcmp((p+v*t1).y-O.o.y)<=0)sol.push_back(p + v * t1);
    t2 = (-f + sqrt(delta)) / (2 * e); if(dcmp((p+v*t2).y-O.o.y)<=0)sol.push_back(p + v * t2);
    return 2;
}

void getTriangleIntersection(Point s, Point e, Point a, Point b, Point c, vector<Point>& sol) {
    Point t;
    if (haveIntersection(s, e, a, b)) {
        getIntersection (s, e-s, a, b-a, t);
        sol.push_back(t);
    }

    if (haveIntersection(s, e, b, c)) {
        getIntersection (s, e-s, b, c-b, t);
        sol.push_back(t);
    }

    if (haveIntersection(s, e, c, a)) {
        getIntersection (s, e-s, c, a-c, t);
        sol.push_back(t);
    }
}
typedef double db;
db  v1,v2,v,t,x;
Point s,e;
int n;
struct node{
	db x,y,r,h;
	Point a,b,c;
	Circle O;
	void read(){
		scanf("%lf%lf%lf%lf",&x,&y,&r,&h);
		a.x = x,a.y = y+h;
		b.x = x-r,b.y = y;
		c.x = x+r,c.y = y;
		O.o.x = x,O.o.y = y;
		O.r = r;
	}
}a[mx];
struct section{
	db l,r;
	bool ok;
}b[mx];
db c[mx];
int vis[mx];
db L,R;
void init(){
	scanf("%lf%lf%lf%lf%lf",&v1,&v2,&v,&t,&x);
	scanf("%d",&n);
	for(int i = 1; i <= n; i++)
		a[i].read();
	memset(vis,0,sizeof(vis));
	s.x = x; s.y = 0;
	db T = v1*t/(v2-v1)+t;
	e.x = x-T*v1,e.y = v*T;	
	R = x;
	L = e.x;
}
void solve(){
	vector<Point> sol;
	int m = 0;
	for(int i = 1; i <= n; i++){
		b[i].ok = 0;
		getLineCircleIntersection(s,e,a[i].O,sol);
		getTriangleIntersection(s,e,a[i].a,a[i].b,a[i].c,sol);
		for(auto p: sol){
			p.x = max(L,p.x);
			p.x = min(p.x,R);
			if(b[i].ok)
				b[i].l = min(b[i].l,p.x),b[i].r = max(b[i].r,p.x);
			else
				b[i].l = b[i].r = p.x;
			b[i].ok = 1;
		}
		if(b[i].ok)
			c[++m] = b[i].l,c[++m] = b[i].r;
	}
	sort(c+1,c+m+1);
	int len = 1;
	for(int i = 1; i <= m; i++)
		if(c[i]!=c[len])
			c[++len] = c[i];
	db ans = 0;
	for(int i = 1; i <= n; i++)
		if(b[i].ok){
			int l = lower_bound(c+1,c+len+1,b[i].l)-c;
			int r = lower_bound(c+1,c+len+1,b[i].r)-c;
			vis[l]++;
			vis[r]--;
		}
	for(int i = 1; i < len; i++){
		vis[i] += vis[i-1];
		if(vis[i])
			ans += c[i+1]-c[i];
	}
	printf("%.4lf\n",(ans/v1)+eps);
}
int main(){
	int T,ca = 1;
	scanf("%d",&T);
	while(T--){
		init();
		printf("Case %d: ",ca++);
		solve();
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值