圆+向量旋转——SCU1980

/*
通过c[i]和c[i+1]求出上一层圆的圆心 
*/
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
#define N 20

typedef double db;
const db eps=1e-6;
const db pi=acos(-1);
int sign(db k){
    if (k>eps) return 1; else if (k<-eps) return -1; return 0;
}
int cmp(db k1,db k2){return sign(k1-k2);}
int inmid(db k1,db k2,db k3){return sign(k1-k3)*sign(k2-k3)<=0;}// k3 在 [k1,k2] 内 
struct point{
    db x,y;
    point operator + (const point &k1) const{return (point){k1.x+x,k1.y+y};}
    point operator - (const point &k1) const{return (point){x-k1.x,y-k1.y};}
    point operator * (db k1) const{return (point){x*k1,y*k1};}
    point operator / (db k1) const{return (point){x/k1,y/k1};}
    db abs(){return sqrt(x*x+y*y);}
    db abs2(){return x*x+y*y;}
    db dis(point k1){return ((*this)-k1).abs();}
    point unit(){db w=abs(); return (point){x/w,y/w};}
    point turn90(){return (point){-y,x};}
};

struct circle{
    point o;
    db r;
};
circle c[N][N];
int n;

int comp(circle a,circle b){
    return a.o.x<b.o.x;
}

int main(){
    while(cin>>n && n){
        for(int i=1;i<=n;i++){
            cin>>c[1][i].o.x;
            c[1][i].o.y=1;
        }
        sort(c[1]+1,c[1]+1+n,comp);
        
        int last=n,now,high=1;
        for(int i=1;i<n;i++)if(last>1){
            high++;now=0;
            for(int j=1;j<last;j++){
                if(sign(c[i][j+1].o.x-c[i][j].o.x-4)>=0)continue;
                point k1=c[i][j].o,k2=c[i][j+1].o,k3=(k2-k1)*0.5;
                point mid=(k1+k2)/2;
                db h=sqrt(4-k3.abs2());
                point v=k3.turn90().unit()*h;
                now++;
                c[i+1][now].o=mid+v;
                c[i+1][now].r=1;
            }
            last=now;
        }

        printf("%.4lf %.4lf
",c[high][1].o.x,c[high][1].o.y);
    }
} 
原文地址:https://www.cnblogs.com/zsben991126/p/12349980.html