SGU 120 Archipelago(计算几何)

来源:互联网 发布:约瑟夫环的数组实现 编辑:程序博客网 时间:2024/04/27 23:14

Description
给出一个正n边形的第n1和第n2个顶点坐标,顺时针输出这个n边形的n个顶点坐标
Input
第一行为三个整数n,n1,n2,之后两行每行两个数表示这个正n边形的第n1和第n2个顶点坐标(3<=n<=150,1<=n1,n2<=n,n1!=n2)
Output
顺次输出这个正n边形的n个顶点坐标
Sample Input
4 1 3
1.0000 0.0000
1.0000 2.0000
Sample Output
1.000000 0.000000
0.000000 1.000000
1.000000 2.000000
2.000000 1.000000
Solution
计算几何,首先根据两个顶点坐标求出n边形中心o,之后将向量oa(a为n1个顶点)每次顺时针旋转2PI/n即可得到各点坐标
Code

#include<cstdio>#include<iostream>#include<algorithm>#include<cmath>using namespace std;#define PI acos(-1.0)#define eps 1e-8#define maxn 222struct node{    double x,y;    node operator *(const double n)const    {        node a;        a.x=x*n,a.y=y*n;        return a;    }    node(){}    node(double _x,double _y)    {        x=_x,y=_y;    }    double len()    {        return sqrt(x*x+y*y);    }    void unit()    {        double l=len();        x/=l,y/=l;    }    node spin(double angle)    {        node a;        a.x=x*cos(angle)+y*sin(angle);        a.y=-x*sin(angle)+y*cos(angle);             return a;    }}p[maxn];double dis(node a,node b){    double x=a.x-b.x,y=a.y-b.y;    return sqrt(x*x+y*y);}int main(){    int n,n1,n2;    double x1,y1,x2,y2;    scanf("%d%d%d",&n,&n1,&n2);    scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);    if(n1>n2)swap(n1,n2),swap(x1,x2),swap(y1,y2);    node s(x2-x1,y2-y1);    double angle=(PI-2*PI*(n2-n1)/n)/2,d=dis(node(x1,y1),node(x2,y2))/2;    s=s.spin(angle);    s.unit();      s=s*(-d/cos(angle));    double x=x1-s.x,y=y1-s.y;    p[n1-1].x=x1-x,p[n1-1].y=y1-y;    for(int i=n1,j=1;j<n;i=(i+1)%n,j++)        p[i]=s.spin(2*j*PI/n);    for(int i=0;i<n;i++)        printf("%.6lf %.6lf\n",p[i].x+x+eps,p[i].y+y+eps);    return 0;}
0 0