-
Notifications
You must be signed in to change notification settings - Fork 0
/
point-2d.c
97 lines (74 loc) · 1.68 KB
/
point-2d.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include<stdio.h>
#include<math.h>
int main() {
double a[20][3], x, y, x1, y1, temp;
int i, n, nearby, index, position, j;
printf("\nEnter the number of points: ");
scanf("%d", &n);
for(i=0; i<n; i++) {
printf("\nPoint - %d", i+1);
printf("\nEnter the x co-ordinate: ");
scanf("%lf", &a[i][0]);
printf("Enter the y co-ordinate: ");
scanf("%lf", &a[i][1]);
}
printf("\n\nThese are the entered co-ordinates");
for(i=0; i<n; i++) {
printf("\n%d. (%lf, %lf)",i+1, a[i][0], a[i][1]);
}
printf("\n\nEnter the index of desired point: ");
scanf("%d", &index);
index--;
x1 = a[index][0];
y1 = a[index][1];
printf("\nRequired number of nearby points: ");
scanf("%d", &nearby);
//Calculate the distance of points.
for(i=0; i<n; i++) {
x = pow(a[i][0] - x1, 2);
y = pow(a[i][1] - y1, 2);
a[i][2] = sqrt(x + y);
if(a[i][2] < a[i-1][2]) {
temp = a[i][0];
a[i][0] = a[i-1][0];
a[i-1][0] = temp;
temp = a[i][1];
a[i][1] = a[i-1][1];
a[i-1][1] = temp;
temp = a[i][2];
a[i][2] = a[i-1][2];
a[i-1][2] = temp;
}
}
//Sorting by distance.
for(i = 0; i < n; i++)
{
position = i;
for(j = i + 1; j <n ; j++)
{
if(a[position][2] > a[j][2])
position = j;
}
if(position != i)
{
temp = a[i][0];
a[i][0] = a[position][0];
a[position][0] = temp;
temp = a[i][1];
a[i][1] = a[position][1];
a[position][1] = temp;
temp = a[i][2];
a[i][2] = a[position][2];
a[position][2] = temp;
}
}
for(i=0; i<n; i++) {
printf("\n(%lf, %lf) - %lf", a[i][0], a[i][1], a[i][2]);
}
printf("\nNearby Points");
for(i=1; i<=nearby; i++) {
printf("\n(%lf, %lf) - %lf", a[i][0], a[i][1], a[i][2]);
}
printf("\n");
return 0;
}