Amritapuri 2009 (UValive 4676 ) - Geometry Problem

29 篇文章 0 订阅
15 篇文章 0 订阅

Link To The Problem


Solution : 由于x的范围是10e7,平方的话是10e14,double 的精度可能判不到小的eps。所以在做叉积或者点积做判断的时候注意取方向向量就好。。。否则。。。WA。。。


Code :

// Amritapuri 2009 Geometry Problem
// Solution : 模拟
//


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>

using namespace std;
#define FOR(i,a,b) for(int (i)=(a);(i)<=(b);(i)++)
#define DOR(i,a,b) for(int (i)=(a);(i)>=(b);(i)--)

#define oo 1e6
#define eps 1.0e-9
#define nMax 1010

//{ 
#define pb push_back
#define dbg(x) cerr << __LINE__ << ": " << #x << " = " << x << endl

#define F first
#define S second

#define bug puts("OOOOh.....");
#define zero(x) (((x)>0?(x):-(x))<eps)

#define LL long long
#define DB double 

#define sf scanf
#define pf printf
#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)

double const pi = acos(-1.0);
double const inf = 1e20;
double inline sqr(double x) { return x*x; }

int dcmp(double x){
    if(fabs(x)<eps) return 0;
    return x>0?1:-1;
}
//}

// Describe of the 2_K Geomtry
// First Part : Point and Line
// Second Part Cicle
// Third Part Polygan

// First Part:
// ****************************** Point and Line *******************************\\
//  {  

class point {
public:
    double x,y;
    point (double x=0,double y=0):x(x),y(y) {}
    void make(double _x,double _y) {x=_x;y=_y;}
    void read() { scanf("%lf%lf",&x,&y); }
    void out() { printf("%.3lf %.3lf\n",x,y);}
    double len() { return sqrt(x*x+y*y); }

    point friend operator - (point const& u,point const& v) { return point(u.x-v.x,u.y-v.y); }
    point friend operator + (point const& u,point const& v) { return point(u.x+v.x,u.y+v.y); }
    double friend operator * (point const& u,point const& v){ return u.x*v.y-u.y*v.x; }
    double friend operator ^ (point const& u,point const& v) { return u.x*v.x+u.y*v.y; }
    point friend operator * (point const& u,double const& k) { return point(u.x*k,u.y*k); }
	point friend operator / (point const& u,double const& k) { return point(u.x/k,u.y/k); }
	
	friend bool operator < (point const& u,point const& v){
		if(dcmp(v.x-u.x)==0) return dcmp(u.y-v.y)<0;
		return dcmp(u.x-v.x)<0;
	}
	friend bool operator != (point const& u,point const& v){
		return dcmp(u.x-v.x) || dcmp(u.y-v.y);
	}

	point rotate(double s) {
		return point(x*cos(s) - y*sin(s),\
					 x*sin(s) + y*cos(s));
	}
	point art() {
		point ret = *this;
		if(dcmp(ret.len())) ret = ret/ret.len();
		return ret;
	}
};
typedef point Vector;
typedef class line{
public:
    point a,b;
    line() {}
    line (point a,point b):a(a),b(b){}
    void make(point u,point v) {a=u;b=v;}
    void read() { a.read(),b.read(); }
}segment;

double det(point u,point v) {
	return u.x*v.y - u.y*v.x;
}
double dot(point u,point v) {
	return u.x*v.x + u.y*v.y;
}

// Weather P is On the Segment (uv) 
int dot_on_seg(point p,point u,point v){
	return dcmp(det((p-u).art(),(v-p).art()))==0 && dcmp(dot((p-u).art(),(p-v).art())) <= 0; // '>=' means P is u or v
}
// The distance from point p to line l
double PToLine(point p,line l) {
	return fabs((p-l.a)*(l.a-l.b))/(l.a-l.b).len();
}
// The ProJect Of Point(p) To Line(l)
point PointProjectLine(point p,line l) {
	double t = dot(l.b-l.a,p-l.a)/dot(l.b-l.a,l.b-l.a);
	return l.a + (l.b-l.a)*t;
}
// Weather line u parallel line v
int parallel(line u,line v) {
	return dcmp(det((u.a-u.b).art(),(v.a-v.b).art()))==0;
}
// The Intersection Point Of Line u and Line v
point intersection(line u,line v) {
	point ret = u.a;
	double t = det(u.a-v.a,v.a-v.b)/det(u.a-u.b,v.a-v.b);
	return ret + (u.b-u.a)*t;
}

//}
// ****************************** First Part end ********************************\\

// Second Part:
// ********************************* Circle *************************************\\
// {

struct circle {
	point O;
	double r;
	circle() {};
	circle(point O,double r):O(O),r(r){};
};


//}
// ****************************** Second Part End *******************************\\

// Third Part :
// ********************************* Polygan *************************************\\
// {


int ConvexHull(vector<point>& p){
	int n=p.size();
	int m=0;
	sort(p.begin(),p.end());
	vector<point> q;
	q.resize(2*n+5);
	rep(i,n) {
		while(m>1 && dcmp((q[m-1]-q[m-2])*(p[i]-q[m-2])) <= 0) m--;
		q[m++] = p[i];
	}
	int k = m;
	for(int i=n-2;i>=0;i--) {
		while(m>k && dcmp((q[m-1]-q[m-2])*(p[i]-q[m-2])) <= 0) m--;
		q[m++] = p[i];
	}
	q.resize(m) ;
	if(m>1) q.resize(m-1);
	// p = q;    // 是否修改原来的多边形
	return q.size();
}
// 三角形重心
point Center(point a,point b,point c){
	return (a+b+c)/3.0;
}
// Centroid of Polygan
point Centroid(vector<point> p){
	point O(0,0),ret(0,0);
	int n = p.size();
	p.pb(p[0]);
	double area = 0.0;
	rep(i,n) {
		ret = ret + Center(O,p[i],p[i+1])*dot(p[i]-O,p[i+1]-O);
		area += dot(p[i]-O,p[i+1]-O);
	}
	if(dcmp(area)==0) {
		pf("There maybe something wrong\n");
		return p[0];
	}
	return ret / area;
}


struct Polygan{
	vector<point> g;
	Polygan() {};
	Polygan(vector<point> g):g(g){};
	Polygan(point p[],int n) {g.clear();rep(i,n) g.pb(p[i]); };
	int convex() { return ConvexHull(g); }
	point center() { return Centroid(g);  } // 多边形的重心
};

//}
// ******************************* Third Part End ********************************\\






//Solution Part :


typedef point P;
typedef line L;

void chk(P p,P v,P a, P b, double& ans) {
	if(dcmp(v.len()) == 0) {
		return ;
	}

	double len = v.len();

	if(parallel(L(p,p+v),L(a,b))) {
		return ;
	}

	P tmp = intersection(L(p,p+v),L(a,b));
	if(dot_on_seg(tmp,a,b) == 0) return ;
	if(dcmp((tmp-p).art()^v) < 0) return ;
	ans = min(ans,(tmp-p).len() / len);
	return ;
}
		
P a[2],b[2],c[2],v[2];


int main() {
#ifndef ONLINE_JUDGE
	freopen("in.txt","r",stdin);
	freopen("out.txt","w",stdout);
#endif
	
	int T;
	cin >> T;
	while(T--){
		for(int i=0;i<2;i++) {
			a[i].read();
			b[i].read();
			c[i].read();
			v[i].read();
		}

		double ans = 1e20;
		for(int i=0;i<2;i++) {
			P V = v[i]-v[i^1];
			chk(a[i],V,a[i^1],b[i^1],ans);
			chk(a[i],V,a[i^1],c[i^1],ans);
			chk(a[i],V,b[i^1],c[i^1],ans);
			chk(b[i],V,a[i^1],b[i^1],ans);
			chk(b[i],V,a[i^1],c[i^1],ans);
			chk(b[i],V,b[i^1],c[i^1],ans);
			chk(c[i],V,a[i^1],b[i^1],ans);
			chk(c[i],V,a[i^1],c[i^1],ans);
			chk(c[i],V,b[i^1],c[i^1],ans);
		}

		if(dcmp(ans-inf) == 0) pf("NO COLLISION\n");
		else pf("%.12lf\n",ans);
	}
	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值