zoukankan      html  css  js  c++  java
  • 和矩形相关的操作

    #include <cstdio>
    #include <cstring>
    #include <vector>
    #include <cmath>
    #define PI 3.141592654
    #define eps 1e-8
    using namespace std;
    /*********************************************************************/
    struct point
    {
        int x,y;
        point() {}
        point(int _x,int _y):x(_x),y(_y) {}
    } p[40];
    double getdis(int a,int b)///两点之间的距离
    {
        return sqrt((double)(p[b].y-p[a].y)*(p[b].y-p[a].y)+(p[b].x-p[a].x)*(p[b].x-p[a].x));
    }
    struct rectangle
    {
        int a,b,c,d;
        double h,w,l;
        rectangle(int _a,int _b,int _c,int _d):a(_a),b(_b),c(_c),d(_d)
        {
            h=getdis(_a,_b);
            w=getdis(_a,_c);
            l=getdis(_a,_d);
            if(h>w) swap(h,w);
            if(w>l) swap(w,l);
            if(h>w) swap(h,w);
        }
    };
    bool isRightAngle(int a,int b,int c) ///推断ab与ac是否构成直角
    {
        if((p[b].y-p[a].y)*(p[c].y-p[a].y)==-1*(p[b].x-p[a].x)*(p[c].x-p[a].x))
            return true;
        return false;
    }
    bool isRectangle(int a,int b,int c,int d) ///推断a,b,c,d是否构成矩形
    {
        int rightAngle=0;
        if(isRightAngle(a,b,c)||isRightAngle(a,b,d)||isRightAngle(a,c,d))
            rightAngle++;
        if(isRightAngle(b,a,c)||isRightAngle(b,a,d)||isRightAngle(b,c,d))
            rightAngle++;
        if(isRightAngle(c,a,b)||isRightAngle(c,a,d)||isRightAngle(c,b,d))
            rightAngle++;
        if(isRightAngle(d,a,b)||isRightAngle(d,a,c)||isRightAngle(d,b,c))
            rightAngle++;
        if(rightAngle==4)
            return true;
        else
            return false;
    }
    double getAngle(int a,int b,int c)///得到ab与ac的夹角
    {
        double x=getdis(a,b);
        double y=getdis(a,c);
        double z=getdis(b,c);
        double l=(x+y+z)/2;
        double area=sqrt(l*(l-x)*(l-y)*(l-z));
        if(x*x+y*y-z*z<-eps)
            return (asin(2*area/(x*y))+PI/2);
        else
            return asin(2*area/(x*y));
    }
    bool isInside(rectangle t,int p)///推断p是否在矩形t内部
    {
        double ans=0;
        if(getdis(t.a,t.b)<t.l-eps)
            ans+=getAngle(p,t.a,t.b);
        if(getdis(t.a,t.c)<t.l-eps)
            ans+=getAngle(p,t.a,t.c);
        if(getdis(t.a,t.d)<t.l-eps)
            ans+=getAngle(p,t.a,t.d);
        if(getdis(t.b,t.c)<t.l-eps)
            ans+=getAngle(p,t.b,t.c);
        if(getdis(t.b,t.d)<t.l-eps)
            ans+=getAngle(p,t.b,t.d);
        if(getdis(t.c,t.d)<t.l-eps)
            ans+=getAngle(p,t.c,t.d);
        if(ans-2*PI>-eps&&ans-2*PI<eps)
            return true;
        else return false;
    }
    bool onSement(int a,int b,int c)///推断c是否在ab线段上
    {
        if((p[c].x-p[a].x)*(p[b].y-p[a].y)==(p[b].x-p[a].x)*(p[c].y-p[a].y)&&min(p[a].x,p[b].x)<=p[c].x&&p[c].x<=max(p[a].x,p[b].x)&&
                min(p[a].y,p[b].y)<=p[c].y&&p[c].y<=max(p[a].y,p[b].y))
            return true;
        else
            return false;
    }
    bool isInEdge(rectangle t,int q)///推断q是否在矩形边框上
    {
        if(getdis(t.a,t.b)<t.l-eps&&onSement(t.a,t.b,q))
            return true;
        if(getdis(t.a,t.c)<t.l-eps&&onSement(t.a,t.c,q))
            return true;
        if(getdis(t.a,t.d)<t.l-eps&&onSement(t.a,t.d,q))
            return true;
        if(getdis(t.b,t.c)<t.l-eps&&onSement(t.b,t.c,q))
            return true;
        if(getdis(t.b,t.d)<t.l-eps&&onSement(t.b,t.d,q))
            return true;
        if(getdis(t.c,t.d)<t.l-eps&&onSement(t.c,t.d,q))
            return true;
        return false;
    }
    double getArea(rectangle t)///得到矩形的面积
    {
        return t.h*t.w;
    }
    /************************************************************************/
    vector<rectangle> vc;
    int n;
    int main()
    {
        while(scanf("%d",&n)!=EOF&&n!=0)
        {
            for(int i=0; i<n; ++i)
                scanf("%d%d",&p[i].x,&p[i].y);
            vc.clear();
            for(int i=0; i<n; ++i)
                for(int j=i+1; j<n; ++j)
                    for(int u=j+1; u<n; ++u)
                        for(int v=u+1; v<n; ++v)
                            if(isRectangle(i,j,u,v))
                                vc.push_back(rectangle(i,j,u,v));
            double ans=0;
            int tsize=vc.size();
            for(int i=0; i<tsize; ++i)
                for(int j=i+1; j<tsize; ++j)
                {
                    if(isInEdge(vc[i],vc[j].a)||isInEdge(vc[i],vc[j].b)||isInEdge(vc[i],vc[j].c)||isInEdge(vc[i],vc[j].d))
                        continue;
                    if(isInEdge(vc[j],vc[i].a)||isInEdge(vc[j],vc[i].b)||isInEdge(vc[j],vc[i].c)||isInEdge(vc[j],vc[i].d))
                        continue;
                    if(isInside(vc[i],vc[j].a)&&isInside(vc[i],vc[j].b)&&isInside(vc[i],vc[j].c)&&isInside(vc[i],vc[j].d)){
                        ans=max(ans,getArea(vc[i]));
                        continue;
                    }
                    if(isInside(vc[j],vc[i].a)&&isInside(vc[j],vc[i].b)&&isInside(vc[j],vc[i].c)&&isInside(vc[j],vc[i].d)){
                        ans=max(ans,getArea(vc[j]));
                        continue;
                    }
                    if(isInside(vc[i],vc[j].a)||isInside(vc[i],vc[j].b)||isInside(vc[i],vc[j].c)||isInside(vc[i],vc[j].d))
                        continue;
                    if(isInside(vc[j],vc[i].a)||isInside(vc[j],vc[i].b)||isInside(vc[j],vc[i].c)||isInside(vc[j],vc[i].d))
                        continue;
                    ans=max(ans,getArea(vc[i])+getArea(vc[j]));
                }
            if(ans>=-eps&&ans<=eps)
                printf("imp
    ");
            else
                printf("%d
    ",(int)(ans+0.5));
        }
        return 0;
    }
    

  • 相关阅读:
    hibernate&查询
    hibernate&一对多&多对多
    hibernate&三种状态的对象&一级缓存&快照机制
    Hibernate5.0安装&基本配置&基本使用
    python获取命令行输入的参数
    node.js + express搭建服务流程
    xpath获取两个标签之间的所有标签
    flask快速入门
    03.调用js执行代码
    02.5 js中的语法知识补充
  • 原文地址:https://www.cnblogs.com/yjbjingcha/p/8370957.html
Copyright © 2011-2022 走看看