SGU 446. Rotation Estimation

57 篇文章 0 订阅
29 篇文章 0 订阅

Link To The Problem


Solution : 枚举旋转的角度、注意精度,开到1e-6就行了,太大了可能出错


Code:

// SGU 446 Rotation Estimation
// 枚举、凸包、旋转

#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 1e-6
#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 = 1e9;
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));
	}
};
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,v-p))==0 && dcmp(dot(p-u,p-v)) <= 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,v.a-v.b))==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(point p[],int n,vector<point> &q){
	//int n=p.size();
	int m=0;
//	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:

double ClockAngle(point o,point a,point p,point q){
	p = q-p;
	a = a-o;
	double ret = (a*p)/a.len()/p.len();
	ret = fabs(asin(ret));
	if(dcmp(a*p)>=0) {
		if(dcmp(a^p)<0) ret = pi-ret;
	}else {
		if(dcmp(a^p)<0) ret = pi + ret;
		else ret = pi*2-ret;
	}
	return ret;
}

int n;
point s1[nMax],s2[nMax];

int chk(point o,double s,point v) {
	vector<point> tmp;
	tmp.clear();
	for(int i=0;i<n;i++) tmp.pb(s2[i]+v-o);
	for(int i=0;i<n;i++) tmp[i] = tmp[i].rotate(-s);
	for(int i=0;i<n;i++) tmp[i] = tmp[i] + o;
	sort(tmp.begin(),tmp.end()) ;
	for(int i=0;i<n;i++) if(tmp[i]!=s1[i]) return 0;
	return 1;
}

vector<point> p1,p2;

int main() {
#ifndef ONLINE_JUDGE
	freopen("in.txt","r",stdin);
#endif

	while(~sf("%d",&n)){
		rep(i,n) s1[i].read();
		rep(i,n) s2[i].read();
		if(n<=1) {
			pf("0.00000000000\n");
			continue;
		}
		sort(s1,s1+n);
		sort(s2,s2+n);
		int m = ConvexHull(s1,n,p1);
		ConvexHull(s2,n,p2);
		p1.pb(p1[0]);
		p2.pb(p2[0]);
		double ans = inf, tmp;
		for(int i=0;i<m;i++) {
			if(dcmp((p2[i]-p2[i+1]).len()-(p1[0]-p1[1]).len())==0){
				tmp = ClockAngle(p1[0],p1[1],p2[i],p2[i+1]);
				//dbg(tmp) ;
				if(chk(p1[0],tmp,p1[0]-p2[i]))  ans = min(ans,min(tmp,2*pi-tmp));
				tmp = ClockAngle(p1[0],p1[1],p2[i+1],p2[i]);
				if(chk(p1[0],tmp,p1[0]-p2[i+1])) ans = min(ans,min(tmp,2*pi-tmp));
			}
		}
		pf("%.11lf\n",ans);
	}
	return 0;
}	


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值