#!/usr/bin/env python

import os, math, sys

def _getline(fp, required = False):
    while True:
        line = fp.readline()
        if not line:
            assert not required
            return ''
        line = line.strip()
        if line and line[0] != '#':
            return line

x1 = x2 = y1 = y2 = 0.0

fp = os.popen('mds 2 ' + sys.argv[1], 'r')
vectors = []
n = int(_getline(fp, True))
assert n == 2
while True:
    lbl = _getline(fp)
    if not lbl:
        break
    x = float(_getline(fp, True))
    y = float(_getline(fp, True))
    vectors.append((x, y, lbl))
    if x < x1: x1 = x
    if x > x2: x2 = x
    if y < y1: y1 = y
    if y > y2: y2 = y

fp.close()

PI2 = 2.0 * math.pi

dR = PI2 / math.pow(0.5 + 0.5 * math.sqrt(5), 2.0)

grid = []
gridsize = len(vectors)

r = 0.0
for n in range(gridsize):
    v = math.sqrt(n + .3)
    x = v * math.sin(r)
    y = v * math.cos(r)
    grid.append((x, y))
    r += dR

p = math.sqrt(gridsize)
dx = x2 - x1
dy = y2 - y1
for n in range(gridsize):
    x, y, lbl = vectors[n]
    x = (2.0 * (x - x1) / dx - 1.0) * p
    y = (2.0 * (y - y1) / dy - 1.0) * p
    vectors[n] = (x, y, lbl)

busy = True
while busy:
    busy = False
    for i in range(gridsize):
        for j in range(i + 1, gridsize):
            d1 = math.sqrt(math.pow(vectors[i][0] - grid[i][0], 2) + math.pow(vectors[i][1] - grid[i][1], 2))
            d2 = math.sqrt(math.pow(vectors[j][0] - grid[j][0], 2) + math.pow(vectors[j][1] - grid[j][1], 2))
            d3 = math.sqrt(math.pow(vectors[i][0] - grid[j][0], 2) + math.pow(vectors[i][1] - grid[j][1], 2))
            d4 = math.sqrt(math.pow(vectors[j][0] - grid[i][0], 2) + math.pow(vectors[j][1] - grid[i][1], 2))
            if d1 + d2 > d3 + d4:
                busy = True
                g = grid[i]
                grid[i] = grid[j]
                grid[j] = g

for i in range(gridsize):
    sys.stdout.write("%g %g 1 0 %s\n" % (grid[i][0], grid[i][1], vectors[i][2]))
