import
java.io.*;
import
java.util.*;
class
Point {
double
x, y;
Point(
double
x,
double
y){
this
.x = x;
this
.y = y;
}
};
public
class
GFG {
public
static
Point[] test_point = {
new
Point(-
1
,
0
),
new
Point(
0
,
1
),
new
Point(
1
,
0
),
new
Point(
0
, -
1
)
};
public
static
double
lower_limit =
0.01
;
static
double
distSum(Point p, Point[] arr,
int
n)
{
double
sum =
0
;
for
(
int
i =
0
; i < n; i++) {
double
distx = Math.abs(arr[i].x - p.x);
double
disty = Math.abs(arr[i].y - p.y);
sum += Math.sqrt((distx * distx) + (disty * disty));
}
return
sum;
}
static
void
geometricMedian(Point[] arr,
int
n)
{
Point current_point =
new
Point(
0
,
0
);
for
(
int
i =
0
; i < n; i++) {
current_point.x += arr[i].x;
current_point.y += arr[i].y;
}
current_point.x /= n;
current_point.y /= n;
double
minimum_distance = distSum(current_point, arr, n);
int
k =
0
;
while
(k < n) {
for
(
int
i =
0
; i < n && i != k; i++) {
Point newpoint =
new
Point(
0
,
0
);
newpoint.x = arr[i].x;
newpoint.y = arr[i].y;
double
newd = distSum(newpoint, arr, n);
if
(newd < minimum_distance) {
minimum_distance = newd;
current_point.x = newpoint.x;
current_point.y = newpoint.y;
}
}
k++;
}
double
test_distance =
1000
;
int
flag =
0
;
while
(test_distance > lower_limit) {
flag =
0
;
for
(
int
i =
0
; i <
4
; i++) {
Point newpoint =
new
Point(
0
,
0
);
newpoint.x = current_point.x + (
double
)test_distance * test_point[i].x;
newpoint.y = current_point.y + (
double
)test_distance * test_point[i].y;
double
newd = distSum(newpoint, arr, n);
if
(newd < minimum_distance) {
minimum_distance = newd;
current_point.x = newpoint.x;
current_point.y = newpoint.y;
flag =
1
;
break
;
}
}
if
(flag ==
0
)
test_distance /=
2
;
}
System.out.println(
"Geometric Median = ("
+ (
int
)current_point.x +
", "
+ (
int
)current_point.y +
")"
);
System.out.println(
" with minimum distance = "
+ String.format(
"%.5f"
, minimum_distance));
}
public
static
void
main(String[] args) {
int
n =
2
;
Point[] arr =
new
Point[n];
arr[
0
] =
new
Point(
1
,
1
);
arr[
1
] =
new
Point(
3
,
3
);
geometricMedian(arr, n);
}
}