SGU 120 – 2D Gemo Translation & Rotation

/* Gemo rotation and translation
 * Originally use equations to solve for center, but failed due to the precision
 * Rotation is easier
 */
#include <cstdio>
#include <algorithm>
#include <cmath>
using namespace std;
 
#define EPS 1e-6
#define feq(x,y) (fabs((x)-(y))<EPS)
#define fgt(x,y) ((x)>((y)+EPS))
#define fge(x,y) (((x)+EPS)>(y))
 
const double pi = acos(-1.0);
 
typedef struct Pt{
    double x,y;
    Pt(){}
    Pt(double x,double y):x(x),y(y){}
    Pt operator+(const Pt&p){ return Pt(x+p.x,y+p.y); }
    Pt operator-(const Pt&p){ return Pt(x-p.x,y-p.y); }
    Pt operator*(double k){ return Pt(x*k,y*k); }
    double operator*(const Pt&p){ return x*p.x+y*p.y; }
    double operator^(const Pt&p){ return x*p.y-y*p.x; }
    double len2(){ return x*x+y*y; }
    double len(){ return sqrt(len2()); }
    bool operator==(const Pt&p){ return feq(x,p.x)&&feq(y,p.y); }
    void eat(){ scanf("%lf%lf",&x,&y); }
	void out(){ printf("%.6lf %.6lf\n",feq(x,0)?0:x,feq(y,0)?0:y); }	
} Pt;
 
int N,N1,N2;
Pt p[200],Center,pN1,pN2,P;
double totalAngle, eachAngle, R;
 
Pt rotate(Pt P, double angle)
{
	double c=cos(angle);
	double s=sin(angle);
	return Pt(P.x*c+P.y*s, P.y*c-P.x*s);
}
 
int main()
{
	double halfAngle,a,b,A,B,C,D,x1,x2,y1,y2,t;
	int tmp;
	//Get Input
	scanf("%d %d %d",&N, &N1, &N2);
	if(N1 > N2) { 
		tmp=N1; N1=N2; N2=tmp; 
		pN2.eat(); pN1.eat();
	} else {
		pN1.eat(); pN2.eat();
	}
 
	//Get Radius
	totalAngle = 2*pi;
	eachAngle = totalAngle/N;	
	halfAngle = (N2-N1)*eachAngle/2;
	R = (pN1-pN2).len()/2/sin(halfAngle);
 
	//Rotate for center
	halfAngle = pi/2-halfAngle;
	P=rotate(pN2-pN1, halfAngle);
	Center = pN1 + P*(R/P.len());
 
	//printf("Get Radius = %lf\n",R);
	//printf("Get Center = "); Center.out();
 
	p[0]=pN1;
	P=pN1-Center;
	for(int i=1;i<N;i++)
	{
		//printf("before rotate P="); P.out();		
		P=rotate(P,eachAngle);
		//printf("after rotate P="); P.out();
		p[i]=Center+P;		
	}
 
	for(int i=1;i<=N;i++)
		p[(i-N1+N)%N].out();
	return 0;
}

Leave a Reply

Your email address will not be published.