POJ-2600 模拟退火

Description
There is a polygon A1A2…An (the vertices Ai are numbered in the clockwise order). On each side AiAi+1 an isosceles triangle AiMiAi+1 is built on the outer side of the polygon, and angle AiMiAi+1 = αi. Here An+1 = A1.
The set of angles ai satisfies a condition that the sum of angles in any of its nonempty subsets is not aliquot to 360 degrees.
You are given n <= 50, co-ordinates of vertices Mi and angles αi (measured in degrees). Write a program which restores co-ordinates of the polygon vertices.
Input
The first line of an input contains an integer n. The next n lines contain pairs of real numbers which are co-ordinates of points Mi. And the last n lines of the file consist of degree values of angles αi.
Output
The output file should contain n lines of pairs of coordinates of the points
Sample Input

3
0 2
3 3
2 0
90
90
90

Sample Output
1 1
1 3
3 1

只要多边形的一个点就可以求出所有的点
随便初始一个点P,在这个点周围找一个点A为可能的点
依次旋转算出P[1],P[2]…P[n]
若P[n]==A找到
否则若distance(A,P[n])<distance(P,P[n]),令P=A

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
using namespace std;
const double pi=acos(-1.0);
const double eps=1e-6;
int sgn(double x){return fabs(x)<eps?0:x<0?-1:1;}
double hypot(double x,double y){return sqrt(x*x+y*y);}
int n;
double ang[64];
struct Point{
	double x,y;
	Point(double x=0,double y=0):x(x),y(y){}
	Point operator -(const Point &t){return Point(x-t.x,y-t.y);}
	Point rotate(double rad){//rad为弧度 逆时针旋转
		Point A=(*this);
		double c=cos(rad),s=sin(rad);
		return Point(A.x*c-A.y*s,A.x*s+A.y*c);
	}
	Point rotate(Point p,double angle){//绕p逆时针旋转后点
		Point v=((*this)-p).rotate(angle);
		return Point(p.x+v.x,p.y+v.y);
	}
	double distance(Point b){return hypot(x-b.x,y-b.y);}
}T[105],A,P;
int f[8][2]={1,0,1,1,1,-1,0,1,0,-1,-1,0,-1,1,-1,-1};
int Trans(){//模拟退火
	for(int i=0;i<8;++i){
		A=Point(P.x+f[i][0],P.y+f[i][1]);
		Point tp=A;
		for(int k=0;k<n;++k)tp=tp.rotate(T[k],ang[k]);
		if(!sgn(A.distance(tp)))return P=A,1;
		if(sgn(A.distance(tp)-P.distance(tp))<0)P=A;
	}
	return 0;
}
int main(){
	scanf("%d",&n);
	for(int i=0;i<n;++i)scanf("%lf%lf",&T[i].x,&T[i].y);
	for(int i=0;i<n;++i)scanf("%lf",&ang[i]),ang[i]*=pi/180.0;
	P=Point(0,0);
	while(!Trans());
	printf("%.lf %.lf\n",P.x,P.y);
	for(int i=0;i+1<n;++i){
		P=P.rotate(T[i],ang[i]);
		printf("%.lf %.lf\n",P.x,P.y);
	}
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值