1 .求不同的四个点组成最大正方形的总个数;
2.由(x1,y1),(x2,y2),可以求出另外两点的坐标;
即 x3=x1+(y1-y2);y3=y1-(x1-x2);
x4=x2+(y1-y2);y4=y2-(x1-x2);
或者
x3=x1-(y1-y2);y3=y1+(x1-x2);
x4=x2-(y1-y2);y4=y2+(x1-x2);
3.由求出的点的坐标可以hash查找是否存在,如果存在就加一;
4.最终答案为8倍的正方形个数;
5.源码;
#include<iostream>
#include<stdio.h>
using namespace std;
const int prime=99991;
struct Node
{
int x;
Node *next;
};
Node hash[100000];
int a[20000],b[20000];
int n;
void insert(int key,int i)
{
if(hash[key].x==0)
{
hash[key].x=i;
}
else{
Node *p=&hash[key],*pp;
while (p!=NULL)
{
pp=p;
p=p->next;
}
Node *p1 = new Node;
p1->x=i;
p1->next=NULL;
pp->next=p1;
}
}
void init()
{
int i,j;
for (i=0;i<=99991;i++) {hash[i].x=0;hash[i].next=NULL;}
for (i=1;i<=n;i++)
{
scanf("%d%d",&a[i],&b[i]);
int key=(a[i]*a[i]+b[i]*b[i])%prime;
insert(key,i);
}
}
int ans;
bool find(int key,int x,int y)
{
Node *p=&hash[key];
while (p!=NULL)
{
if(a[p->x]==x&&b[p->x]==y) return true;
p=p->next;
}
return false;
}
void make()
{
ans=0;
int key1,key2,x1,x2,y1,y2;
int i,j;
for (i=1;i<=n;i++)
for (j=1;j<=n;j++)
if(i!=j) {
x1=a[i]+b[i]-b[j];y1=b[i]-(a[i]-a[j]);
x2=a[j]+b[i]-b[j];y2=b[j]-(a[i]-a[j]);
key1=(x1*x1+y1*y1)%prime;
key2=(x2*x2+y2*y2)%prime;
if(find(key1,x1,y1)&&find(key2,x2,y2)) ans++;//cout<<x1<<" "<<y1<<endl;cout<<x2<<" "<<y2<<endl;}
x1=a[i]-(b[i]-b[j]);y1=b[i]+(a[i]-a[j]);
x2=a[j]-(b[i]-b[j]);y2=b[j]+(a[i]-a[j]);
key1=(x1*x1+y1*y1)%prime;
key2=(x2*x2+y2*y2)%prime;
if(find(key1,x1,y1)&&find(key2,x2,y2)) ans++;
}
printf("%d\n",ans/8);
}
int main()
{
while(scanf("%d",&n))
{
if(n==0) break;
init();
make();
}
return 0;
}