SGU 120 Archipelago(计算几何)

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 222
struct 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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值