判断四个点是否构成正方形。。没写过几何题目,代码写的非常丑。。先将就着AC了。。有空参考下大牛的代码
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
struct Point
{
double x;
double y;
};
int square(struct Point *p1, struct Point *p2, struct Point *p3,
struct Point *p4)
{
if ((p1->y - p2->y) * (p3->x - p4->x) == (p3->y - p4->y) * (p1->x - p2->x))
{
if ((p1->y - p3->y) * (p2->x - p4->x)
== (p2->y - p4->y) * (p1->x - p3->x))
{
return (p2->x - p1->x) * (p3->x - p1->x)
+ (p2->y - p1->y) * (p3->y - p1->y) == 0
&& (p2->x - p1->x) * (p2->x - p1->x)
+ (p2->y - p1->y) * (p2->y - p1->y)
== (p3->x - p1->x) * (p3->x - p1->x)
+ (p3->y - p1->y) * (p3->y - p1->y);
}
else if ((p1->y - p4->y) * (p2->x - p3->x)
== (p2->y - p3->y) * (p1->x - p4->x))
{
return (p2->x - p1->x) * (p4->x - p1->x)
+ (p2->y - p1->y) * (p4->y - p1->y) == 0
&& (p2->x - p1->x) * (p2->x - p1->x)
+ (p2->y - p1->y) * (p2->y - p1->y)
== (p4->x - p1->x) * (p4->x - p1->x)
+ (p4->y - p1->y) * (p4->y - p1->y);
}
else
return 0;
}
else
return 0;
}
int main()
{
int i, j, t;
scanf("%d", &t);
struct Point *array = malloc(4 * sizeof(struct Point));
for (i = 1; i <= t; i++)
{
for (j = 0; j < 4; j++)
scanf("%lf %lf", &(array[j].x), &(array[j].y));
int a = square(&array[0], &array[1], &array[2], &array[3]);
int b = square(&array[0], &array[2], &array[1], &array[3]);
int c = square(&array[0], &array[3], &array[1], &array[2]);
if (i != 1)
putchar('\n');
printf("Case %d:\n", i);
puts(a || b || c ? "Yes" : "No");
}
free(array);
return 0;
}