HDU 5572 An Easy Physics Problem (物理、计算几何)

题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=5572


题意:

一个质点球,一个固定的刚性圆柱体 
给定圆柱体圆心坐标,半径 
小球起点坐标,起始运动方向(向量) 
终点坐标 
问能否到达终点,小球运动中如果碰到圆柱体会反射(基本物理知识)

另外小球的起点和终点不在圆柱体中。


这题卡了我很久。。一直以为是精度问题,没想到最后是策略问题。网上找了一些代码看,发现有些代码连向量共线都没有判断居然都可以过。。。为什么就偏偏卡我啊。。

花了好几个小时,也不知道收获了什么。。。有没有意义....

正确的策略应该是先判断线段ab是否于圆香蕉,不交就判方向,相交就找法线,求对称点,在判断方向,具体看代码吧。

现在明确了,判断向量同向只要在共线的基础上判断点积就行了。也熟练了以下求对称点,以及一些圆的操作。。。


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <cmath>
using namespace std;
const double eps = 1e-8;
const double PI = acos(-1.0);
int sgn(double x) {
    if(fabs(x) < eps)return 0;
    if(x < 0)return -1;  
    else return 1; 
}
struct Point {  
    double x,y;
    Point(){}  
    Point(double _x,double _y) {   
        x = _x;y = _y;  
    }
    Point operator +(const Point &b)const {
       return Point(x + b.x,y + b.y);  
    } 
    Point operator -(const Point &b)const {
       return Point(x - b.x,y - b.y);  
    }  
    double operator ^(const Point &b)const {
       return x*b.y - y*b.x;  
    }    
    double operator *(const Point &b)const {   
        return x*b.x + y*b.y;  
    }
    Point operator *(const double &k) const{return Point(x*k, y*k);}
    Point operator /(const double &k) const{return Point(x/k, y/k);}
    void transXY(double B) {   
        double tx = x,ty = y;   
        x = tx*cos(B) - ty*sin(B);   
        y = tx*sin(B) + ty*cos(B);  
    }
    double len() { return hypot(x, y);}
    double len2() {return x*x + y*y;}
    double distance(Point p) { return hypot(x-p.x, y-p.y);};
    Point trunc(double r) {
        double l = len();
        if(!sgn(l)) return *this;
        r /= l;
        return Point (x*r, y*r);
    }
};


struct Line {  
    Point s,e; 
    Line(){}  
    Line(Point _s,Point _e)  {
       s = _s;e = _e;  
    } 
    double length(){ return s.distance(e);}
    pair<int,Point> operator &(const Line &b)const {
        Point res = s;
        if(sgn((s-e)^(b.s-b.e)) == 0) {
            if(sgn((s-b.e)^(b.s-b.e)) == 0) return make_pair(0,res);             
            else return make_pair(1,res);         
        } 
        double t = ((s-b.s)^(b.s-b.e))/((s-e)^(b.s-b.e));
        res.x += (e.x-s.x)*t;         
        res.y += (e.y-s.y)*t;         
        return make_pair(2,res);     
    }
    int relation(Point p) {
        int c = sgn((p - s) ^ (e - s));
        if(c < 0) return 1;
        else if(c > 0) return 2;
        else return 3;
    }
    double dispointtoline(Point p) { return fabs((p-s)^(e-s))/length();}
    double dispointtoseg(Point p) {
        if(sgn((p - s) * (e - s)) < 0 || sgn((p - e) * (s - e)) < 0)
        return min(p.distance(s), p.distance(e));
        return dispointtoline(p);
    }
    Point lineprog(Point p) { return s+(((e-s)*((e-s)*(p-s)))/((e-s).len2()));}
    Point symmetrypoint(Point p) {
        Point q = lineprog(p);
        return Point(2*q.x-p.x, 2*q.y-p.y);
    }
};

struct Circle {
    Point p;
    double r;
    int relationline(Line v) {
        double dst = v.dispointtoline(p);
        if(sgn(dst-r) < 0) return 2;
        else if(sgn(dst-r) == 0) return 1;
        return 0;
    }
    int pointcrossline(Line v, Point &p1, Point &p2) {
        if(!(*this).relationline(v)) return 0;
        Point a = v.lineprog(p); double d = v.dispointtoline(p);
        d = sqrt(r*r-d*d);
        if(sgn(d) == 0) {p1 = a; p2 = a; return 1;}
        p1 = a + (v.e - v.s).trunc(d);
        p2 = a - (v.e - v.s).trunc(d);
        return 2;
    }
    int relationseg(Line v) {
         double dst = v.dispointtoseg(p);
        if(sgn(dst - r) < 0) return 2;
        else if(sgn(dst - r) == 0) return 1;
        else return 0;
    }
};

int main() {
    int t;
    scanf("%d", &t);
    Circle c;
    Point a, v, b;
    for(int o = 1; o <= t; o++) {
        printf("Case #%d: ", o);
        scanf("%lf %lf %lf", &c.p.x, &c.p.y, &c.r);
        scanf("%lf %lf %lf %lf", &a.x, &a.y, &v.x, &v.y);
        scanf("%lf %lf", &b.x, &b.y);
        Line ab = Line(a, b);
        Point av = a + v;
        Line l = Line(a, av);
        int flag = 0;
        if(l.relation(b) == 3 && sgn((b-a)*(av-a)) > 0 && c.relationseg(ab) <= 1) flag = 1;
        else {
            Point p1, p2;
            if(c.pointcrossline(l, p1, p2) == 2) {
                Point m = sgn(a.distance(p1) - a.distance(p2)) < 0 ? p1 : p2;
                Line f(c.p, m);
                Point q = f.symmetrypoint(a);
                Line tmp(m, q);
                if(tmp.relation(b) == 3 && sgn(((b-m)*(q-m)))>0) flag = 1;
            }    
        }
        puts(flag?"Yes":"No");
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值