//实现微信摇一摇,找寻附近的10米的位置坐标。
#include "tool.h"
typedef struct {
// char name[100];
int x, y;
} POINT;
void input(POINT* p) { scanf("%d%d", &p->x, &p->y); }
void print(POINT* p) { printf("(%d,%d) ", p->x, p->y); }
double distance(POINT* p, POINT* q) {
int dx = p->x - q->x;
int dy = p->y - q->y;
return sqrt(dx * dx + dy * dy);
}
#define N 10
POINT me = {N / 2, N / 2};
int inc(const void* a, const void* b) {
POINT* pa = (POINT*)a;
POINT* pb = (POINT*)b;
double da = distance(pa, &me);
double db = distance(pb, &me);
double diff = da - db;
if (diff > 0) return 1;
if (diff < 0) return -1;
return 0;
}
int main() {
srand(time(NULL));
POINT p[N];
int i;
for (i = 0; i < N; i++) {
p[i].x = randInt(0, N);
p[i].y = randInt(0, N);
}
for (i = 0; i < N; i++) print(p + i);
qsort(p, N, sizeof(p[0]), inc);
puts("-------------After sorted-----------");
for (i = 0; i < N; i++) {
printf("%.3f ", distance(p + i, &me));
print(p + i);
}
}