#! /usr/bin/python3
# Last edited on 2025-03-27 22:28:49 by stolfi

import sys
from math import inf, sqrt, hypot


def main():
  L = read_data()
  G = group_data(L, 30, 6.0)
  write_groups(G)
  return 0
  # ----------------------------------------------------------------------
  
def group_data(L, vMax, rMax):
  # Takes a list {L} of triples. Each triple is {(x,y,v)} where
  # {x,y} are coords of a pixel and {v} is a pixel value.
  # Groups all pixels with {v} greater than a threshold {vMax}
  # into compact groups up with radius up to {rMax}.
  # Returns a list of group centers.
  
  G = [] # Group centers.
  R = [] # Group radii.
  P = [] # {P[ig]} is a list of indices of elements of {L} in group {ig}.
  for k in range(len(L)):
    x, y, v = L[k]
    if v <= vMax:
      ig = find_group(x, y, G, rMax);
      if ig < 0:
        # New group: 
        G.append((x,y))
        R.append(0)
        P.append([k,])
      else:
        # Old group:
        P[ig].append(k)
        G[ig], R[ig] = recompute_group(P[ig], L)
  return G
  # ----------------------------------------------------------------------
  
def find_group(x, y, G, rMax):
  # Given lists {G,R} of centers and radii of groups, 
  # tries to find the group that contains {(x,y)}.
  # It should be the group with center closest to {(x,y)},
  # provided that the distance is at most {rMax}. 
  # If not found, returns {-1}. 
  
  dMin = +inf
  igMin = -1;
  for ig in range(len(G)):
    xc, yc = G[ig]
    d = hypot(x - xc, y - yc);
    if d <= rMax and d < dMin:
      dMin = d; igMin = ig
  return igMin
  # ----------------------------------------------------------------------
  
def recompute_group(Pi, L):
  # Given a list {PK} of indices of elements of {L} that comprise some
  # group, recomputes the barycenter {Gi} and radius {Ri} of the group.
  # Returns {G,R}.
  
  n = len(Pi)
  xBar = 0; yBar = 0;
  for k in Pi:
    x, y, v = L[k];
    xBar += x; yBar += y
  xBar /= n; yBar /= n
  rad = 0;
  for k in Pi:
    x, y, v = L[k];
    d = hypot(x - xBar, y - yBar)
    if d > rad:
      rad = d
  return (xBar, yBar), rad
  # ----------------------------------------------------------------------
  
def read_data():
  rd = open("sites-mask.txt", "r")
  lines = rd.readlines()
  xtable = [ x.split(' ') for x in lines ]
  table = []
  for row in xtable:
    table.append([ float(x) for x in row ])
  return table
  # ----------------------------------------------------------------------
  
def write_groups(G):
  for x, y in G:
    sys.stdout.write(f"{x:8.2f} {y:8.2f}\n")
  sys.stdout.flush()
  return
  # ----------------------------------------------------------------------
  
main()
