首页 > 代码库 > URAL - 1963(几何)

URAL - 1963(几何)

不知道是不是几何题,反正就是找对称,值得注意的是,对称轴不一定过点,还可能在边上

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
using namespace std;
const double mm=1e-7;
double x[10],y[10];
bool deng(double a,double b)
{
    return (a-b<mm)&&(b-a<mm);//精度处理
}
bool chuizhi(double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4)//判断1,3组成的直线是否和2,4垂直
{
    return (y3-y1)*(y2-y4)==(x1-x3)*(x2-x4);
}
bool juli(double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4)//判断和点4和点2到由点1和点3组成的直线的距离是否相等
{
    double a=(y1-y3);
    double b=(x3-x1);
    double c=(y1*b+x1*a)*(-1);
    double ding=a*x2+b*y2+c;
    double hehe=fabs(ding);//注意不要除以sqrt(a*a+b*b),因为有可能是0
      ding=a*x4+b*y4+c;
    double haha=fabs(ding);
    return deng(hehe,haha);

}
int main()
{
     while(~scanf("%lf%lf",&x[1],&y[1]))
     {
         scanf("%lf%lf%lf%lf%lf%lf",&x[2],&y[2],&x[3],&y[3],&x[4],&y[4]);
         int ans=0;
         if(juli(x[1],y[1],x[2],y[2],x[3],y[3],x[4],y[4])&&chuizhi(x[1],y[1],x[2],y[2],x[3],y[3],x[4],y[4]))
            ans+=2;
         if(juli(x[2],y[2],x[3],y[3],x[4],y[4],x[1],y[1])&&chuizhi(x[1],y[1],x[2],y[2],x[3],y[3],x[4],y[4]))
            ans+=2;
          double mx1,my1,mx2,my2;
         mx1=(x[1]+x[2])/2.0;
         my1=(y[1]+y[2])/2.0;
         mx2=(x[3]+x[4])/2.0;
         my2=(y[3]+y[4])/2.0;
         if(chuizhi(x[1],y[1],mx1,my1,x[2],y[2],mx2,my2)&&chuizhi(x[4],y[4],mx2,my2,x[3],y[3],mx1,my1))
               ans+=2;
         mx1=(x[1]+x[4])/2.0;
         my1=(y[1]+y[4])/2.0;
         mx2=(x[3]+x[2])/2.0;
         my2=(y[3]+y[2])/2.0;
         if(chuizhi(x[4],y[4],mx1,my1,x[1],y[1],mx2,my2)&&chuizhi(x[2],y[2],mx2,my2,x[3],y[3],mx1,my1))
            ans+=2;
        printf("%d\n",ans);

     }
    return 0;
}

 

URAL - 1963(几何)