//dev-a_square
#include <cstdlib>
#include <iostream>
#include <math.h> using namespace std; struct ve// square.vertex
{
int x;
int y;
};
ve array[41]={ //以左下角为原点,构造矩阵
{0,0},{2,0},{4,0},{6,0},{8,0},
{0,2},{2,2},{4,2},{6,2},{8,2},
{0,4},{2,4},{4,4},{6,4},{8,4},
{0,6},{2,6},{4,6},{6,6},{8,6},
{0,8},{2,8},{4,8},{6,8},{8,8},
{3,1},{3,2},{3,3},{3,5},{3,6},{3,7},
{4,1},{4,3},{4,5},{4,7},
{5,1},{5,2},{5,3},{5,5},{5,6},{5,7}
};
int n=0,n_end=0; bool a_square(ve a,ve b,ve c,ve d)
{
ve e;
//寻找a点的对角点,只要bcd中有一点与a不在同一直线上即可
if((a.x!=b.x )&&(a.y!=b.y))
{
// cout<<"b may be a diagonal"<<endl;
//为了方便后续比较另外两个顶点与与这两个顶点的关系,
//将对角与b交换位置,此处正好是b所以无需交换
}
else
{
if((a.x!=c.x )&&(a.y!=c.y))
{
//cout<<"c may be a diagonal"<<endl;
e=c;//c与b的位置交换
c=b;
b=e;
}
else
{
if((a.x!=d.x )&&(a.y!=d.y))
{
// cout<<"d may be a diagonal"<<endl;
e=d;
d=b;
b=e;
}
else
{
// cout<<"abcd不可能组成正方形";
return 0;
}
}
}
//下面来判断cd和ab能否组成正方形。
if( ((c.x==a.x) && (c.y==b.y) //c和d的其中一个点的x为a的x,y为b的y
&&(d.x==b.x) && (d.y==a.y)) //另一个点的x为b的x,y为a的y
|| ((d.x==a.x) && (d.y==b.y)
&&(c.x==b.x) && (c.y==a.y)) )
{
//cout<<"abcd为矩形"<<endl;
}
if( //这一句是最关键的,貌似就错在这里了,哪位大侠帮忙看看...
(abs(a.x-b.x))==(abs(d.y-c.y))//相邻的两条边长度相等 的矩形是正方形
||(abs(a.y-b.y))==(abs(c.x-d.x))
)
{
/*******
cout<<"("<<a.x<<","<<a.y<<")"<<" and "<<
"("<< b.x<<","<<b.y<<")"<<" and "<<
"("<<c.x<<","<<c.y<<")"<<" and "<<
"("<< d.x<<","<<d.y<<")"<<" 四点是图中的正方形之一 "<< endl;
*********/
return 1;
}
else
{
// cout<<"abcd不可能组成正方形";
return 0;
}
}
int main(int argc, char *argv[])
{
int t=41;//t=sizeof(array)/sizeof(ve)
for(int a=0;a<t;a++)
{
for (int b=0;b<t;b++)
{
for(int c=0;c<t;c++)
{
for(int d=0;d<t;d++)
{
if(a_square(array[a],array,array[c],array[d]))
{
n++;
}
}
}
}
}
n_end=(n/24)-1;//扣掉 (3,3),(3,5),(5,3),(5,5)这个没有连通的.
cout<<"这个图中有"<<n_end<<"个正方形"<<endl;
system("PAUSE");
return EXIT_SUCCESS;
}