判断四个点是否能形成正方形

先贴上模板代码

struct node{
    double x,y;
};
double getdis(node a,node b){
    return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
bool isequal(double x,double y){
    if(x-y>=-1e-8&&x-y<=1e-8)
        return true;
    return false;
}
bool isSquare(node a,node b,node c,node d){
    double len[6];
    len[0]=getdis(a,b);
    len[1]=getdis(a,c);
    len[2]=getdis(a,d);
    len[3]=getdis(b,c);
    len[4]=getdis(b,d);
    len[5]=getdis(c,d);
    double len1=len[0];
    double len2=len[0];
    for(int i=1;i<6;++i){
        if(!isequal(len[i],len2)&&!isequal(len[i],len1))
            len2=len[i];
    }
    for(int i=0;i<6;++i){
        if(isequal(len[i],len2)||isequal(len[i],len1));
        else return false;
    }
    if(len2-len1<-1e-8)
        swap(len1,len2);
    if(!isequal(2*len1*len1,len2*len2))
        return false;
    return true;
}

来源:http://blog.csdn.net/mengxingyuanlove/article/details/47373261

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值