又是判断正方形,这题竟然写了我两个晚上。。大部分时间全是在纸上画几何坐标系求已知两点的另外两点组成正方形的点。。我的高中数学知识啊。。太悲伤了,这种简单的几何题竟然想了这么久。。想当年切平面几何切菜一样的。。唉,还好最后还是把公式推出来了,剩下的代码就简单了
#include<stdio.h>
#include<stdlib.h>
struct Point
{
int x;
int y;
};
static int cmp_point(const void *p1, const void *p2)
{
struct Point *pp1 = (struct Point*) p1;
struct Point *pp2 = (struct Point*) p2;
if (pp1->x != pp2->x)
return pp1->x - pp2->x;
else
return pp1->y - pp2->y;
}
int main()
{
int N, n;
scanf("%d", &N);
struct Point *points = malloc(1000 * sizeof(struct Point));
struct Point *p1 = malloc(sizeof(struct Point));
struct Point *p2 = malloc(sizeof(struct Point));
while (N--)
{
while (scanf("%d", &n), n)
{
int i, j;
for (i = 0; i < n; i++)
scanf("%d %d", &(points[i].x), &(points[i].y));
qsort(points, n, sizeof(struct Point), cmp_point);
int x1, x2, y1, y2, squares = 0;
for (i = 0; i < n; i++)
for (j = i + 1; j < n; j++)
{
x1 = points[i].x;
y1 = points[i].y;
x2 = points[j].x;
y2 = points[j].y;
p1->x = y2 - y1 + x1;
p1->y = x1 - x2 + y1;
p2->x = y2 - y1 + x2;
p2->y = y2 + x1 - x2;
if (bsearch(p1, points, n, sizeof(struct Point),
cmp_point) != NULL&&bsearch(p2, points, n, sizeof(struct Point),
cmp_point) != NULL)
squares++;
p1->x = y1 - y2 + x1;
p1->y = x2 - x1 + y1;
p2->x = x2 + y1 - y2;
p2->y = x2 - x1 + y2;
if (bsearch(p1, points, n, sizeof(struct Point),
cmp_point) != NULL&&bsearch(p2, points, n, sizeof(struct Point),
cmp_point) != NULL)
squares++;
}
printf("%d\n", squares / 4);
}
if (N)
putchar('\n');
}
free(p1);
free(p2);
free(points);
return 0;
}