A point (x,y), on the cartesian plane, makes an angle theta with the positive direction of the x-axis. Theta varies in the interval  [0 ,2PI) radians, i.e, greater than or equal to zero; but less than 2*PI radians.  

For example, the polar angle of the point (1,2) as marked in this plane below, is (approximately) 63.4 degrees (multiply by PI/180 to convert to radians)

Ref http://eldar.mathstat.uoguelph.ca/dashlock/Outreach/Articles/images/PRfig1.jpg  

The Task  

Given a list of points in the 2D plane, sort them in ascending order of their polar angle. In case multiple points share exactly the same polar angle, the one with lesser distance from the origin (0,0) should occur earlier in the sorted list.  

Input Format 

The first line contains an integer N. 

This is followed by N lines containing pairs of space separated integers, x and y which represent the coordinates of the points in the cartesian plane.  

Constraints 

    1 <= N <= 1000 

    -100 <= x,y <= 100 

    The point (0,0) will not be present in the list of points.  

Output Format 

The output should contain N lines. 
Each line should contain two integers x and y, corresponding to a point in the original list. Display the points in ascending order of their polar angle.

Sample Input 

4  
1 0  
0 -1  
-1 0  
0 1  

Sample Output    

1 0    
0 1    
-1 0    
0 -1    

Explanation  

The point (0,1) has a polar angle of 90 degrees. The point (1,0) has a polar angle of 0 degrees. (-1,0) has a polar angle of 180 degrees and (0,-1) has a polar angle of 270 degrees.

Difficulty: MEDIUM
Tags: Geometry

Starter code:

Solutions:
import math
N = int(input())
points = []
for _ in range(N):
	(x, y) = list(map(int, input().split()))
	mag = math.sqrt(x ** 2 + y ** 2)
	deg = math.degrees(math.atan2(y, x))
	if deg < 0:
		deg = 360 + deg
	points.append((deg, mag, x, y))
points.sort()
for (d, m, x, y) in points:
	print(x, y)


import sys
import math
n = int(input())
points = [{'point': tuple(map(int, line.rstrip().split(' ')))} for line in sys.stdin]
for p in points:
	angle = math.atan2(*reversed(p['point']))
	if angle < 0:
		angle += 2 * math.pi
	p['angle'] = angle
	p['distance'] = p['point'][0] ** 2 + p['point'][1] ** 2
sorted_points = sorted(points, key=lambda p: (p['angle'], p['distance']))
for p in sorted_points:
	print('{} {}'.format(p['point'][0], p['point'][1]))


import math

def main():
	n = int(input().strip())
	pts = []
	for i in range(n):
		(x, y) = input().strip().split(' ')
		pts.append((int(x), int(y)))

	def key(pt):
		(x, y) = pt
		a = math.atan2(y, x)
		if a < 0:
			a += 2 * math.pi
		d = math.sqrt(x * x + y * y)
		return (a, d)
	pts.sort(key=key)
	for pt in pts:
		print(*pt)
main()


import sys
import math

def readPoint(r):
	s = r.readline()
	a = s.split()
	return [int(v) for v in a]
r = sys.stdin
numPoints = int(r.readline())
points = []
thetas = []
while numPoints > 0:
	(x, y) = readPoint(r)
	points.append([x, y])
	numPoints -= 1
points = sorted(points, key=lambda x: math.sqrt(x[0] ** 2 + x[1] ** 2))
for (x, y) in points:
	if x >= 0 and y >= 0:
		thetas.append([math.atan2(y, x), x, y])
	elif x < 0 and y >= 0:
		thetas.append([math.atan2(y, x), x, y])
	elif x < 0 and y < 0:
		thetas.append([2 * math.pi + math.atan2(y, x), x, y])
	else:
		thetas.append([2 * math.pi + math.atan2(y, x), x, y])
thetas2 = sorted(thetas, key=lambda x: x[0])
for x in thetas2:
	sys.stdout.write(str(x[1]) + ' ' + str(x[2]) + '\n')


import sys
from math import sqrt, atan2, pi

def dist(x, y):
	return sqrt(x ** 2 + y ** 2)

def to_polar(x, y):
	radians = atan2(y, x)
	return radians if radians >= 0 else 2 * pi + radians

def by_polar_then_dist(point):
	(x, y) = map(int, point.split())
	return (to_polar(x, y), dist(x, y))
items = list(filter(None, sys.stdin.read().split('\n')))
n = int(items.pop(0))
print('\n'.join(sorted(items[:n], key=by_polar_then_dist)))


from operator import itemgetter
import math
l = list()
N = input()
N = int(N)

def getMag(e):
	y = e.pop()
	x = e.pop()
	if x == 0:
		if y == 0:
			return 0
		elif y > 0:
			return 90
		else:
			return 270
	elif y == 0:
		if x > 0:
			return 0
		else:
			return 180
	else:
		return math.degrees(math.atan(y / x))

def convertDegs(a, b, degs):
	if a < 0 and b < 0:
		return degs + 180
	elif a > 0 and b < 0:
		return degs + 360
	elif a < 0 and b > 0:
		return degs + 180
	else:
		return degs

def sortList(l):
	l = sorted(l, key=lambda x: (x[2], x[3]))
	return l
for i in range(0, N):
	tmp = input()
	[a, b] = tmp.split()
	a = int(a)
	b = int(b)
	mag = getMag([a, b])
	tmp = [a, b, mag, math.sqrt(a * a + b * b)]
	convertedDegs = convertDegs(tmp[0], tmp[1], tmp[2])
	l.append([a, b, convertedDegs, math.sqrt(a * a + b * b)])
sortedList = sortList(l)
outList = list()
for i in range(0, N):
	tmp = sortedList.pop()
	[a, b, c, d] = tmp
	tmp.pop()
	tmp.pop()
	outList.append(tmp)
for i in range(0, N):
	[a, b] = outList.pop()
	print(str(a) + ' ' + str(b))


import math

def main():
	x = 0
	line = input()
	line2 = line.split(' ')
	a = int(line2[0])
	cords = []
	for xx in range(a):
		line = input()
		line2 = line.split(' ')
		b = int(line2[0])
		c = int(line2[1])
		angle = math.atan2(c, b)
		if angle < 0:
			angle += 2 * math.pi
		dist = math.pow(b * b + c * c, 0.5)
		cords.append((b, c, angle, dist))
	key1 = lambda cord: (cord[2], cord[3])
	cords.sort(key=key1)
	for cord in cords:
		print(cord[0], cord[1])
main()


import sys
import math

def read(r, w):
	n = r.readline()
	points = int(n)
	ptlist = []
	for i in range(points):
		point = r.readline()
		z = point.split()
		(x, y) = z
		degree = math.atan2(float(y), float(x))
		if int(y) < 0:
			degree = 2 * math.pi + degree
		ptlist.append((degree, int(x), int(y)))
	for i in range(points):
		for j in range(i + 1, points):
			if float(ptlist[i][0]) > float(ptlist[j][0]):
				temp = ptlist[i]
				ptlist[i] = ptlist[j]
				ptlist[j] = temp
			elif float(ptlist[i][0]) == float(ptlist[j][0]):
				dis = float(ptlist[i][1] ** 2 + ptlist[i][2] ** 2)
				dis2 = float(ptlist[j][1] ** 2 + ptlist[j][2] ** 2)
				if dis > dis2:
					temp = ptlist[i]
					ptlist[i] = ptlist[j]
					ptlist[j] = temp
	for i in range(points):
		w.write(str(ptlist[i][1]) + ' ' + str(ptlist[i][2]) + '\n')
read(sys.stdin, sys.stdout)


from math import pi as PI
from math import atan as ATAN
from math import sqrt as SQRT
from pprint import pprint

def getAngle(point):
	if point[0] == 0 and point[1] > 0:
		return 90
	if point[0] == 0 and point[1] < 0:
		return 270
	if point[1] == 0 and point[0] > 0:
		return 0
	if point[1] == 0 and point[0] < 0:
		return 180
	angle = ATAN(float(point[1]) / float(point[0])) * 180 / PI
	if angle < 0:
		angle += 90
	if point[0] > 0:
		if point[1] < 0:
			angle += 270
	elif point[0] < 0:
		if point[1] > 0:
			angle += 90
		elif point[1] < 0:
			angle += 180
	return angle

def getAngles(data):
	return list(map(getAngle, data))

def getDistance(point):
	return SQRT(point[0] ** 2 + point[1] ** 2)
N = int(input())
data = [[int(x) for x in input().split()] for _ in range(N)]
angles = getAngles(data)
origindist = list(map(getDistance, data))
data = [(data[i], angles[i], origindist[i]) for i in range(len(data))]
data = sorted(data, key=lambda x: x[1])
for i in range(len(data)):
	for j in range(i, len(data)):
		if data[i][1] == data[j][1]:
			if data[i][2] > data[j][2]:
				(data[i], data[j]) = (data[j], data[i])
for elem in data:
	print('%s %s' % (elem[0][0], elem[0][1]))


import math
n = int(input())
ptsList = []
while n > 0:
	pt = input().split()
	ptsList.append((int(pt[0]), int(pt[1])))
	n -= 1

def sortAngle(t):
	value = math.atan2(t[1], t[0])
	if value < 0:
		return math.pi * 2 + value
	return math.atan2(t[1], t[0])
ptsList.sort(key=lambda t: (t[0] ** 2 + t[1] ** 2) ** 0.5)
ptsList.sort(key=sortAngle)
for pt in ptsList:
	print(pt[0], pt[1])


def polarFromString(cartString):
	(x, y) = tuple(map(int, cartString.split()))
	if x != 0:
		tan = float(y) / x
	else:
		tan = float('-inf')
	if x > 0 and y >= 0:
		quad = 1
	elif x > 0:
		quad = 4
	elif y > 0:
		quad = 2
	else:
		quad = 3
		if x == 0 and y < 0:
			quad = 4
	return (quad, tan, x * x + y * y)
n = int(input().strip())
assert n >= 1 and n <= 1000
pointStrings = []
for i in range(n):
	pointStrings.append(input().strip())
pointStrings = sorted(pointStrings, key=lambda pString: polarFromString(pString))
for i in range(n):
	print(pointStrings[i])


import math
n = int(input())
points = []

def getAngle(coord):
	return (coord[0], coord[1])
for i in range(0, n):
	row = input().split()
	x = int(row[0])
	y = int(row[1])
	angle = math.atan2(y, x)
	if angle < 0:
		angle = math.pi * 2 + angle
	points += [(angle, math.hypot(x, y), x, y)]
spoints = sorted(points, key=getAngle)
for p in spoints:
	print(str(p[2]) + ' ' + str(p[3]))


from math import atan2, pi

def myatan(xy):
	val = atan2(int(xy[1]), int(xy[0]))
	if val < 0:
		return 2 * pi + val
	return val

def mydist(xy):
	return (int(xy[0]) ** 2 + int(xy[1]) ** 2) ** (1 / 2)
mylist = []
n = int(input())
for i in range(n):
	temp = input()
	temp2 = [temp, myatan(temp.split()), mydist(temp.split())]
	mylist += [temp2]
mylist.sort(key=lambda x: x[2])
mylist.sort(key=lambda x: x[1])
for x in mylist:
	print(x[0])


import sys, math
from operator import itemgetter, attrgetter
sys.stdin.readline()
list0 = sys.stdin.readlines()
gt0List = []
ls0List = []
for line0 in list0:
	line = line0.strip()
	cords = line.split()
	x1 = int(cords[0])
	y1 = int(cords[1])
	distance = math.sqrt(x1 ** 2 + y1 ** 2)
	theta = math.atan2(y1, x1)
	if theta >= 0:
		gt0List.append([line, theta, distance])
	else:
		ls0List.append([line, theta, distance])
gt0List.sort(key=itemgetter(1, 2))
ls0List.sort(key=itemgetter(1, 2))
for i in gt0List:
	print(i[0])
for i in ls0List:
	print(i[0])


import cmath
import math
INPUT = input()
LIST = []
LIST2 = []
for i in range(0, int(INPUT)):
	(A, B) = input().split()
	A = float(A)
	B = float(B)
	X = cmath.phase(complex(A, B))
	if X >= 0:
		LIST2.append([A, B, X])
	else:
		LIST.append([A, B, X])
while len(LIST2) != 0:
	LOWEST = 5000
	PRINTA = (0, 0)
	for KEY in LIST2:
		if LOWEST > KEY[2]:
			LOWEST = KEY[2]
			PRINTA = (KEY[0], KEY[1])
		elif LOWEST == KEY[2]:
			if math.hypot(KEY[0], KEY[1]) < math.hypot(PRINTA[0], PRINTA[1]):
				LOWEST = KEY[2]
				PRINTA = (KEY[0], KEY[1])
	LIST2.remove([PRINTA[0], PRINTA[1], LOWEST])
	print(int(PRINTA[0]), int(PRINTA[1]))
while len(LIST) != 0:
	LOWEST = 0
	PRINTA = (0, 0)
	for KEY in LIST:
		if LOWEST > KEY[2]:
			LOWEST = KEY[2]
			PRINTA = (KEY[0], KEY[1])
		elif LOWEST == KEY[2]:
			if math.hypot(KEY[0], KEY[1]) < math.hypot(PRINTA[0], PRINTA[1]):
				LOWEST = KEY[2]
				PRINTA = (KEY[0], KEY[1])
	LIST.remove([PRINTA[0], PRINTA[1], LOWEST])
	print(int(PRINTA[0]), int(PRINTA[1]))


import math

def cmp_to_key(mycmp):

	class K:

		def __init__(self, obj, *args):
			self.obj = obj

		def __lt__(self, other):
			return mycmp(self.obj, other.obj) < 0

		def __gt__(self, other):
			return mycmp(self.obj, other.obj) > 0

		def __eq__(self, other):
			return mycmp(self.obj, other.obj) == 0

		def __le__(self, other):
			return mycmp(self.obj, other.obj) <= 0

		def __ge__(self, other):
			return mycmp(self.obj, other.obj) >= 0

		def __ne__(self, other):
			return mycmp(self.obj, other.obj) != 0
	return K

def compare(a, b):
	if a[3] < b[3]:
		return -1
	if a[3] > b[3]:
		return 1
	if a[2] < b[2]:
		return -1
	if a[2] > b[2]:
		return 1
	return 0
n = int(input().strip())
points = []
while n > 0:
	(x, y) = tuple((int(num) for num in input().strip().split(' ')))
	if x == 0 and y > 0:
		angle = math.pi / 2
	elif x == 0 and y < 0:
		angle = 3 * math.pi / 2
	else:
		angle = math.atan(y / x)
	if x > 0 and y < 0:
		angle += 2 * math.pi
	elif x < 0:
		angle += math.pi
	points.append((x, y, math.sqrt(x ** 2 + y ** 2), angle))
	n -= 1
points.sort(key=cmp_to_key(compare))
for point in points:
	print(point[0], point[1])


import math
points = []
for _ in range(int(input())):
	(a, b) = input().split()
	points.append(((int(a), int(b)), -1))

def rads(points):
	(x, y) = points
	if not x:
		return math.pi / 2 if y > 0 else math.pi * 3 / 2
	if x < 0:
		return math.atan(y / x) + math.pi
	if y < 0:
		return math.atan(y / x) + 2 * math.pi
	return math.atan(y / x)
for i in range(len(points)):
	points[i] = (points[i][0], rads(points[i][0]))
points.sort(key=lambda x: x[0][1] ** 2 + x[0][0] ** 2)
points.sort(key=lambda x: x[1])
for i in points:
	print(i[0][0], i[0][1])


import math
N = int(input())
points = []
for i in range(0, N):
	pointA = input().split(' ')
	pointB = (int(pointA[0]), int(pointA[1]))
	points.append(pointB)

def pivot(pointB):
	(x, y) = pointB
	degree = math.atan2(y, x)
	if degree < 0:
		degree = degree + 2 * math.pi
	distance = math.sqrt(x * x + y * y)
	return (degree, distance)
points.sort(key=pivot)
for pointB in points:
	print(*pointB)


import cmath

def mergesort(list, comparator):
	if len(list) == 1:
		return list
	half = len(list) // 2
	left = list[:half]
	right = list[half:]
	return merge(mergesort(left, comparator), mergesort(right, comparator), comparator)

def merge(array1, array2, comparator):
	i = 0
	j = 0
	output = []
	while i < len(array1) and j < len(array2):
		if comparator(array1[i], array2[j]):
			output.append(array1[i])
			i += 1
		else:
			output.append(array2[j])
			j += 1
	if i < len(array1):
		while i < len(array1):
			output.append(array1[i])
			i += 1
	else:
		while j < len(array2):
			output.append(array2[j])
			j += 1
	return output

def compareLength(a, b):
	(x1, y1) = a
	(x2, y2) = b
	return y1 * y1 + x1 * x1 <= y2 * y2 + x2 * x2

def compareCoord(a, b):
	(x1, y1) = a
	(x2, y2) = b
	phasea = cmath.phase(complex(-x1, y1))
	phaseb = cmath.phase(complex(-x2, y2))
	return phasea >= phaseb

def length(item):
	(x, y) = item
	return x * x + y * y

def printPolars(list):
	for (x, y) in list:
		print(str(x) + ' ' + str(y))
numOfInputs = int(input())
polars = []
for i in range(numOfInputs):
	theInput = input().split()
	polars.append((int(theInput[0]), int(theInput[1])))
sortedPolars = mergesort(polars, compareLength)
sortedPolars = mergesort(sortedPolars, compareCoord)
printPolars(sortedPolars)


import math
n = int(input())
point_angle = []
for i in range(n):
	d = input().split()
	x = int(d[0])
	y = int(d[1])
	theta = math.atan2(y, x) * (180 / math.pi)
	theta = (360 + theta) % 360
	r = math.sqrt(x ** 2 + y ** 2)
	point_angle.append([theta, r, x, y])
point_angle.sort()
for p in point_angle:
	print('{} {}'.format(p[2], p[3]))


from math import *
n = int(input())
A = []
Theta = []
Dist = []
ls = []
for i in range(n):
	(p, q) = map(int, input().split())
	A.append([p, q])
for i in range(n):
	dist = sqrt(pow(A[i][0], 2) + pow(A[i][1], 2))
	angleinDegrees = atan2(A[i][1], A[i][0])
	if angleinDegrees < 0:
		angleinDegrees = 2 * pi + angleinDegrees
	ls.append([angleinDegrees, dist, A[i][0], A[i][1]])
ls.sort(key=lambda tup: tup[1])
ls.sort(key=lambda tup: tup[0])
for i in range(n):
	print(str(ls[i][2]) + ' ' + str(ls[i][3]))


import math

def swap(i, j, a):
	x = a[i]
	a[i] = a[j]
	a[j] = x
n = int(input())
input_ar = []
angle_ar = []
for j in range(n):
	(x, y) = [int(i) for i in input().strip().split()]
	input_ar.append((x, y))
	if x >= 0 and y >= 0:
		if x == 0:
			angle_ar.append((math.pi / 2, j))
		else:
			angle_ar.append((math.atan(y / x), j))
	if x < 0 and y >= 0:
		angle_ar.append((math.pi - math.atan(y / -x), j))
	if x < 0 and y < 0:
		angle_ar.append((math.pi + math.atan(-y / -x), j))
	if x >= 0 and y < 0:
		if x == 0:
			angle_ar.append((3 * math.pi / 2, j))
		else:
			angle_ar.append((2 * math.pi - math.atan(-y / x), j))
for i in range(n):
	for j in range(n - i - 1):
		if angle_ar[j][0] > angle_ar[j + 1][0]:
			swap(j, j + 1, angle_ar)
		elif angle_ar[j][0] == angle_ar[j + 1][0]:
			k1 = angle_ar[j][1]
			k2 = angle_ar[j + 1][1]
			if math.fabs(input_ar[k1][0]) > math.fabs(input_ar[k2][0]):
				swap(j, j + 1, angle_ar)
			elif input_ar[k1][0] == input_ar[k2][0]:
				if math.fabs(input_ar[k1][1]) > math.fabs(input_ar[k2][1]):
					swap(j, j + 1, angle_ar)
for (i, j) in angle_ar:
	print(*input_ar[j])


from math import acos, sqrt, pi
from fractions import *

def length(point):
	l = 0
	for x in point:
		l += x * x
	return sqrt(l)
n = int(input())
pointsx0yp = []
pointsx0ym = []
pointsxpy0 = []
pointsxmy0 = []
points1 = []
points2 = []
points3 = []
points4 = []
for i in range(n):
	point = [int(i) for i in input().split()]
	x = point[0]
	y = point[1]
	if x > 0 and y > 0:
		points1.append(point)
	elif x < 0 and y > 0:
		points2.append(point)
	elif x < 0 and y < 0:
		points3.append(point)
	elif x > 0 and y < 0:
		points4.append(point)
	elif x == 0 and y > 0:
		pointsx0yp.append(point)
	elif x == 0 and y < 0:
		pointsx0ym.append(point)
	elif x > 0 and y == 0:
		pointsxpy0.append(point)
	elif x < 0 and y == 0:
		pointsxmy0.append(point)
for point in sorted(pointsxpy0, key=lambda point: length(point)):
	print(*point)
dct = {}
f = lambda point: Fraction(point[1], point[0]) if point[0] != 0 else float('inf')
for point in points1:
	value = f(point)
	if value in dct:
		dct[value].append(point)
	else:
		dct[value] = [point]
for value in sorted(dct.keys()):
	for point in sorted(dct[value], key=lambda point: length(point)):
		print(*point)
for point in sorted(pointsx0yp, key=lambda point: length(point)):
	print(*point)
dct.clear()
for point in points2:
	value = f(point)
	if value in dct:
		dct[value].append(point)
	else:
		dct[value] = [point]
for value in sorted(dct.keys()):
	for point in sorted(dct[value], key=lambda point: length(point)):
		print(*point)
for point in sorted(pointsxmy0, key=lambda point: length(point)):
	print(*point)
dct.clear()
for point in points3:
	value = f(point)
	if value in dct:
		dct[value].append(point)
	else:
		dct[value] = [point]
for value in sorted(dct.keys()):
	for point in sorted(dct[value], key=lambda point: length(point)):
		print(*point)
for point in sorted(pointsx0ym, key=lambda point: length(point)):
	print(*point)
dct.clear()
for point in points4:
	value = f(point)
	if value in dct:
		dct[value].append(point)
	else:
		dct[value] = [point]
for value in sorted(dct.keys()):
	for point in sorted(dct[value], key=lambda point: length(point)):
		print(*point)


import cmath
import math
from math import *

def compare(point):
	(x, y) = point
	(r, phi) = cmath.polar(complex(x, y))
	if phi < 0:
		phi = 2 * 3.1415926535 + phi
	return (phi, r)
T = int(input())
points = list()
for i in range(T):
	(x, y) = [int(s) for s in input().split()]
	points.append((x, y))
computed = sorted(points, key=compare)
for (x, y) in computed:
	print(str(x) + ' ' + str(y))


import math

def polar(c):
	(x, y) = c
	a = abs(x + y * 1j)
	p = math.acos(x / a)
	if y < 0:
		p = 2 * math.pi - p
	p = int(p * 32768) / 32768
	return (p, a)
N = int(input())
A = []
for _ in range(N):
	(a, b) = (int(_) for _ in input().split())
	A.append((a, b))
A.sort(key=polar)
for a in A:
	print('%d %d' % a)


import math

class Point(object):

	def __init__(self, ptstr):
		self.x = int(ptstr.split()[0])
		self.y = int(ptstr.split()[1])
		self.angle = self.polar_angle()
		self.distance = self.distance_from_origin()

	def polar_angle(self):
		if self.x == 0 and self.y > 0:
			return math.pi / 2
		if self.x == 0 and self.y < 0:
			return 3 * math.pi / 2
		if self.y == 0 and self.x > 0:
			return 0
		if self.y == 0 and self.x < 0:
			return math.pi
		val = math.atan2(self.y, self.x)
		return val if val > 0 else val + 2 * math.pi

	def distance_from_origin(self):
		return math.sqrt(self.x ** 2 + self.y ** 2)

	def string(self):
		return '%d %d' % (self.x, self.y)

def main():
	points = [Point(str(input())) for i in range(int(input()))]
	points.sort(key=lambda p: (p.angle, p.distance))
	print('\n'.join([p.string() for p in points]))

def deg(rad):
	return rad * 180 / math.pi
main()


from math import atan2
from math import pi

def value(t):
	angle = atan2(t[1], t[0])
	if angle < 0:
		angle += 2 * pi
	dist = t[0] ** 2 + t[1] ** 2
	return (angle, dist)
atlas = []
for _ in range(int(input())):
	t = tuple(map(int, input().split()))
	atlas.append(t)
atlas = sorted(atlas, key=value)
for t in atlas:
	print(t[0], t[1])


import math

def compare(p1, p2):
	(a1, a2) = (0, 0)
	if p1[1] != 0:
		if p1[0] != 0:
			a1 = math.atan(p1[1] / p1[0])
			if a1 < 0:
				a1 = math.pi - a1 * -1
		else:
			a1 = math.pi / 2
	elif p1[0] > 0:
		a1 = 0
	else:
		a1 = math.pi
	if p1[1] < 0:
		a1 += math.pi
	if p2[1] != 0:
		if p2[0] != 0:
			a2 = math.atan(p2[1] / p2[0])
			if a2 < 0:
				a2 = math.pi - a2 * -1
		else:
			a2 = math.pi / 2
	elif p2[0] > 0:
		a2 = 0
	else:
		a2 = math.pi
	if p2[1] < 0:
		a2 += math.pi
	if a1 > a2:
		return 1
	elif a1 < a2:
		return 0
	elif p2[0] * p2[0] + p2[1] * p2[1] - p1[0] * p1[0] - p1[1] * p1[1] > 0:
		return 0
	else:
		return 1

def qsort(L):
	if len(L) > 0:
		l1 = [i for i in L[1:] if compare(i, L[0]) == 0]
		l2 = [i for i in L[1:] if compare(i, L[0]) == 1]
		return qsort(l1) + [L[0]] + qsort(l2)
	else:
		return []
n = int(input())
l = []
for i in range(n):
	l.append([int(i) for i in input().split()])
res = qsort(l)
for i in range(n):
	print(' '.join(map(str, res[i])))


from math import atan2, pi
A = []
N = int(input())
for _ in range(N):
	[x, y] = [int(s) for s in input().split()]
	angle = atan2(y, x)
	if angle < 0:
		angle += 2 * pi
	mag = x ** 2 + y ** 2
	A.append([x, y, angle, mag])
A.sort(key=lambda k: k[3])
A.sort(key=lambda k: k[2])
for [x, y, a, m] in A:
	print(str(x) + ' ' + str(y))


from math import atan2, pi
n = int(input())
ps = []
for _ in range(n):
	(x, y) = map(int, input().split())
	a = atan2(y, x)
	if a < 0:
		a += 2 * pi
	rsq = x * x + y * y
	ps.append((a, rsq, x, y))
ps.sort()
for (a, rsq, x, y) in ps:
	print(x, y)


import math

def get_angle(x, y):
	if x == 0:
		angle = 90.0 * math.pi / 180.0
		if y < 0:
			angle *= -1
	else:
		angle = math.atan2(y, x)
	if angle < 0:
		angle = 2 * math.pi + angle
	return angle

def get_distance(x, y):
	return math.sqrt(x ** 2 + y ** 2)

class Point:

	def __init__(self, x, y):
		self.x = x
		self.y = y
		self.angle = get_angle(x, y)
		self.dist = get_distance(x, y)

	def print_point(self):
		print(self.x, self.y)
_N = int(input())
points = []
for i in range(_N):
	(x, y) = input().split()
	points.append(Point(int(x), int(y)))
sorted_points = sorted(points, key=lambda point: (point.angle, point.dist))
for point in sorted_points:
	point.print_point()


import operator

def polar(x, y, deg=1):
	from math import hypot, atan2, pi
	if deg:
		return (hypot(x, y), 180.0 * atan2(y, x) / pi)
	else:
		return (hypot(x, y), atan2(y, x))
N = int(input())
points = []
for _ in range(N):
	(x, y) = [int(ns) for ns in str(input()).split()]
	(w, r) = polar(x, y)
	if r < 0:
		r = r + 360
	points.append([x, y, r, w])
points.sort(key=operator.itemgetter(2, 3))
for p in points:
	print(str(p[0]) + ' ' + str(p[1]))


import math

def getAngle(x, y):
	if x is 0:
		if y > 0:
			return math.pi / 2
		else:
			return 1.5 * math.pi
	result = math.atan(y / x)
	if x < 0 and y >= 0:
		result += math.pi
	elif x < 0 and y < 0:
		result += math.pi
	elif x > 0 and y < 0:
		result += 2 * math.pi
	return result

def getKey(item):
	return item[2]
angles = {}
for i in range(int(input())):
	(x, y) = [int(x) for x in input().strip().split()]
	angle = getAngle(x, y)
	r = math.sqrt(x * x + y * y)
	if str(angle) not in angles:
		angles[str(angle)] = [(x, y, r)]
	else:
		angles[str(angle)].append((x, y, r))
for k in sorted(angles):
	for (x, y, r) in sorted(angles[k], key=getKey):
		print(''.join([str(x), ' ', str(y)]))


import math, sys
N = int(sys.stdin.readline())
P = []
for _ in range(N):
	(x, y) = list(map(int, sys.stdin.readline().split()))
	P.append(((math.atan2(y, x) + 2 * math.pi) % (2 * math.pi), x ** 2 + y ** 2, x, y))
P.sort()
for (theta, dist, x, y) in P:
	print(x, y)


import math, sys
N = int(sys.stdin.readline())
P = []
for _ in range(N):
	(x, y) = list(map(int, sys.stdin.readline().split()))
	P.append(((math.atan2(y, x) + 2 * math.pi) % (2 * math.pi), x ** 2 + y ** 2, x, y))
P.sort()
for (theta, dist, x, y) in P:
	print(x, y)


from math import asin, acos, atan
N = int(input())
points = []
for point in range(N):
	points.append(tuple([int(i) for i in input().split()]))

def quad(p):
	(x, y) = p
	if y < 0:
		if x > 0:
			return 7
		elif x == 0:
			return 6
		elif x < 0:
			return 5
	elif y == 0:
		if x > 0:
			return 0
		elif x < 0:
			return 4
	elif y > 0:
		if x > 0:
			return 1
		elif x == 0:
			return 2
		elif x < 0:
			return 3

def f(point):
	if point[0] != 0:
		return atan(point[1] / point[0])
	else:
		return 1.5696852161410326 if point[1] > 0 else 4.71127786614103

def dist(a, b):
	return sum([(a[i] - b[i]) ** 2 for i in range(len(a))]) ** 0.5
points.sort(key=lambda point: (quad(point), f(point), dist(point, (0, 0))))
for point in points:
	print(point[0], point[1], end='')
	print()


import math

def angle(x, y):
	if x > 0:
		if y >= 0:
			return math.atan(y / float(x))
		else:
			return math.atan(y / float(x)) + 2 * math.pi
	elif x < 0:
		return math.atan(y / float(x)) + math.pi
	elif y > 0:
		return math.pi / float(2)
	else:
		return 3 * math.pi / float(2)

def main():
	n = int(input())
	points = []
	for x in range(n):
		(x, y) = map(int, input().split())
		theta = angle(x, y)
		r = math.sqrt(x ** 2 + y ** 2)
		points.append([x, y, r, theta])
	sorted_li = sorted(points, key=lambda x: (x[-1], x[-2]))
	for x in sorted_li:
		print(x[0], x[1])
main()


from math import atan2, degrees, sqrt
T = int(input())
tests = []
for i in range(T):
	N = input()
	tests.append(list(map(int, N.split())))
tests.sort(key=lambda point: (degrees(atan2(point[1], point[0])) % 360, sqrt(point[0] ** 2 + point[1] ** 2)))
for i in tests:
	print('{0} {1}'.format(i[0], i[1]))


import math, sys
N = int(sys.stdin.readline())
P = []
for _ in range(N):
	(x, y) = list(map(int, sys.stdin.readline().split()))
	P.append(((math.atan2(y, x) + 2 * math.pi) % (2 * math.pi), x ** 2 + y ** 2, x, y))
P.sort()
for (theta, dist, x, y) in P:
	print(x, y)


import math, sys
N = int(sys.stdin.readline())
P = []
for _ in range(N):
	(x, y) = list(map(int, sys.stdin.readline().split()))
	P.append(((math.atan2(y, x) + 2 * math.pi) % (2 * math.pi), x ** 2 + y ** 2, x, y))
P.sort()
for (theta, dist, x, y) in P:
	print(x, y)


import math, sys
N = int(sys.stdin.readline())
P = []
for _ in range(N):
	(x, y) = list(map(int, sys.stdin.readline().split()))
	P.append(((math.atan2(y, x) + 2 * math.pi) % (2 * math.pi), x ** 2 + y ** 2, x, y))
P.sort()
for (theta, dist, x, y) in P:
	print(x, y)


from math import hypot, atan2, pi

def polar(x, y):
	k = 180.0 * atan2(y, x) / pi
	if k < 0:
		k += 360
	return (k, hypot(x, y))
n = int(input())
ans1 = []
ans = []
for i in range(n):
	(x, y) = map(int, input().split())
	ans.append([x, y])
	ans1.append(polar(x, y))
x = sorted(ans1)
ind = []
for j in x:
	ind.append(ans1.index(j))
for i in ind:
	print(ans[i][0], ans[i][1])


from math import atan2, pi
n = int(input())
array = [()] * n
for i in range(n):
	(x, y) = map(int, input().split())
	array[i] = (atan2(y, x) % (2 * pi), x ** 2 + y ** 2, x, y)
for point in sorted(array):
	print(point[2], point[3])


import math
N = int(input())
points = []
for n in range(N):
	points.append([int(x) for x in input().split()])

def sorfun(p):
	angle = math.atan2(p[1], p[0])
	if angle < 0:
		angle += 2 * math.pi
	radius = math.sqrt(p[0] * p[0] + p[1] * p[1])
	return (angle, radius)
sortedpoints = sorted(points, key=sorfun)
for p in sortedpoints:
	print(p[0], p[1])


from math import atan2, pi
points = []
for _ in range(int(input())):
	(x, y) = [int(i) for i in input().split()]
	angle = atan2(y, x)
	if angle < 0:
		angle += 2 * pi
	point = (angle, x ** 2 + y ** 2, x, y)
	points.append(point)
for (_, _, x, y) in sorted(points):
	print('%d %d' % (x, y))


N = int(input().strip())
nums = [input().strip().split() for _ in range(N)]
import math, functools, operator, cmath

def to_polar(anum):
	cpl = complex(float(anum[0]), float(anum[1]))
	angle = cmath.phase(cpl)
	dist = abs(cpl)
	while angle < 0:
		angle += 2 * math.pi
	return (angle, dist, anum)

def sort_vals(nums):

	def sort_order(x, y):
		if x[0] == y[0]:
			return x[1] - y[1]
		return x[0] - y[0]
	return sorted(nums, key=functools.cmp_to_key(sort_order))
for n in map(operator.itemgetter(2), sort_vals(map(to_polar, nums))):
	print('{} {}'.format(n[0], n[1]))


import math
import cmath
N = int(input())
inpts = [input().split() for i in range(N)]
points = [(int(x), int(y)) for (x, y) in inpts]
angles = [((cmath.phase(complex(p[0], p[1])) % (2 * math.pi), math.sqrt(p[0] ** 2 + p[1] ** 2)), p) for p in points]
angles.sort()
for a in angles:
	print(str(a[1][0]) + ' ' + str(a[1][1]))


import math
from math import pi
num = int(input())
pts = []
for i in range(num):
	pts.append(input())
polar = []
for pt in pts:
	plist = [int(s) for s in pt.split()]
	hyp = math.hypot(plist[0], plist[1])
	ang = math.atan2(plist[1], plist[0])
	if ang < 0:
		ang = ang + 2 * pi
	polar.append([ang, hyp, plist[0], plist[1]])
polar.sort()
for p in polar:
	print(p[2], p[3])


import math
nn = int(input())
pnts = []
for i0 in range(nn):
	(x, y) = list(map(int, input().split()))
	pnts.append([x, y, (x * x + y * y) ** 0.5, 0])
pi2 = 2 * math.pi
for i0 in range(nn):
	theta = math.atan2(pnts[i0][1], pnts[i0][0])
	if theta < 0:
		theta = pi2 + theta
	pnts[i0][3] = theta
pnts = sorted(pnts, key=lambda x1: x1[2])
pnts = sorted(pnts, key=lambda x1: x1[3])
for i0 in range(nn):
	print(pnts[i0][0], pnts[i0][1])


import functools
import sys

def compare(a, b):
	if a[1] >= 0 and b[1] < 0:
		return -1
	elif b[1] >= 0 and a[1] < 0:
		return +1
	elif a[1] == 0 and b[1] == 0:
		if a[0] >= 0 and b[0] < 0:
			return -1
		elif b[0] >= 0 and a[0] < 0:
			return +1
		else:
			return 0
	else:
		cp = a[0] * b[1] - a[1] * b[0]
		if cp < 0:
			return +1
		elif cp > 0:
			return -1
		else:
			return 0
instream = sys.stdin
N = int(instream.readline())
lst = []
for i in range(N):
	l = instream.readline().strip()
	(p, q) = [int(i) for i in l.split()]
	lst.append((p, q))
lst.sort(key=lambda t: t[0] * t[0] + t[1] * t[1])
lst.sort(key=functools.cmp_to_key(compare))
for t in lst:
	print('%d %d' % t)


a = []
import cmath

def f(c):
	p = cmath.phase(c)
	if p < 0:
		p += 2 * cmath.pi
	return (p, abs(c))
for _ in range(int(input())):
	a.append(complex(*[int(i) for i in input().split()]))
for i in sorted(a, key=f):
	print('{} {}'.format(int(round(i.real)), int(round(i.imag))))


import sys
import math
t = int(input().strip())
lst = []
for o in range(t):
	n = input().strip().split(' ')
	a = int(n[0])
	b = int(n[1])
	r = (a * a + b * b) ** 0.5
	l = []
	if a == 0:
		if b > 0:
			l.append(90)
		elif b < 0:
			l.append(270)
	elif b == 0:
		if a > 0:
			l.append(0)
		elif a < 0:
			l.append(180)
	else:
		x = math.degrees(math.atan(b / a))
		if x < 0:
			x += 180
		if b < 0:
			x += 180
		l.append(x)
	l.append(r)
	l.append(str(a) + ' ' + str(b))
	lst.append(l)
lst.sort()
for o in range(t):
	print(lst[o][2])


import math

def calcRadian(x, y):
	if x > 0:
		if y > 0:
			phi = math.atan(y / x)
		elif y == 0:
			phi = 0.0
		else:
			phi = math.atan(y / x) + 2 * math.pi
	elif x < 0:
		if y > 0:
			phi = math.atan(y / x) + math.pi
		elif y == 0:
			phi = math.pi
		else:
			phi = math.atan(y / x) + math.pi
	elif x == 0:
		if y > 0:
			phi = math.pi / 2
		else:
			phi = 1.5 * math.pi
	return phi

def insertionSort(l, tup):
	for i in range(0, len(l)):
		if tup[2] < l[i][2]:
			l.insert(i, tup)
			return
		if tup[2] == l[i][2]:
			if tup[3] <= l[i][3]:
				l.insert(i, tup)
				return
	l.append(tup)
	return
times = int(input())
l = []
for i in range(0, times):
	s = input().split()
	x = float(s[0])
	y = float(s[1])
	phi = calcRadian(x, y)
	s.append(phi)
	s.append(math.hypot(float(s[0]), float(s[1])))
	insertionSort(l, s)
for i in l:
	print(i[0], i[1])


import fractions

def read_ints():
	return tuple([int(x) for x in input().split()])

def make_key(p):
	d = p[0] * p[0] + p[1] * p[1]
	f = fractions.Fraction(p[0] * abs(p[0]), d)
	if p[1] >= 0:
		return (0, -f, d)
	else:
		return (1, f, d)
T = int(input())
points = []
for case in range(T):
	points.append(read_ints())
points.sort(key=make_key)
for p in points:
	print(p[0], p[1])


N = int(input())
(P1, P2, P3, P4) = ([], [], [], [])
for j in range(N):
	(x, y) = map(int, input().split())
	r = x ** 2 + y ** 2
	if y == 0:
		if x > 0:
			P1.append((0, r, x, 0))
		else:
			P2.append((1, r, x, 0))
	elif y > 0:
		if x >= 0:
			P1.append((y ** 2 / r, r, x, y))
		else:
			P2.append((x ** 2 / r, r, x, y))
	elif x <= 0:
		P3.append((y ** 2 / r, r, x, y))
	else:
		P4.append((x ** 2 / r, r, x, y))
	P1 = sorted(P1)
	P2 = sorted(P2)
	P3 = sorted(P3)
	P4 = sorted(P4)
P1.extend(P2)
P1.extend(P3)
P1.extend(P4)
for p in P1:
	print(p[2], p[3])


import sys
import math

def polar(x, y):
	r = pow(x * x + y * y, 0.5)
	theta = math.atan2(y, x)
	if theta < 0:
		theta += 2 * math.pi
	return (r, theta)
t = int(input())
pp = []
for _ in range(t):
	(x, y) = map(int, input().split())
	pp.append((x, y))
pp.sort(key=lambda p: polar(*p)[::-1])
for (r, theta) in pp:
	print(r, theta)


from math import pi, atan2
print('\n'.join(map('{} {}'.format, *zip(*sorted((list(map(int, input().split())) for _ in range(int(input()))), key=lambda x: (atan2(x[1], x[0]) % (2 * pi), x[0] ** 2 + x[1] ** 2))))))


lst = []
t = int(input())
import math
while t > 0:
	(x, y) = [int(x) for x in input().split()]
	dist = x ** 2 + y ** 2
	if x == 0:
		if y > 0:
			lst.append((90, str(x) + ' ' + str(y), dist))
		else:
			lst.append((270, str(x) + ' ' + str(y), dist))
	elif y == 0:
		if x > 0:
			lst.append((0, str(x) + ' ' + str(y), dist))
		else:
			lst.append((180, str(x) + ' ' + str(y), dist))
	elif x > 0 and y > 0:
		lst.append((math.atan(y / x) * 180 / 3.14, str(x) + ' ' + str(y), dist))
	elif x < 0 and y > 0:
		lst.append((180 - math.atan(y / abs(x)) * 180 / 3.14, str(x) + ' ' + str(y), dist))
	elif x < 0 and y < 0:
		lst.append((180 + math.atan(abs(y / x)) * 180 / 3.14, str(x) + ' ' + str(y), dist))
	elif x > 0 and y < 0:
		lst.append((360 - math.atan(abs(y / x)) * 180 / 3.14, str(x) + ' ' + str(y), dist))
	t -= 1
for ele in sorted(lst, key=lambda x: (x[0], x[2])):
	print(ele[1])


from math import pi, atan2
print('\n'.join(map('{} {}'.format, *zip(*sorted((list(map(int, input().split())) for _ in range(int(input()))), key=lambda x: (atan2(x[1], x[0]) % (2 * pi), x[0] ** 2 + x[1] ** 2))))))


from math import pi, atan2
print('\n'.join(map('{} {}'.format, *zip(*sorted((list(map(int, input().split())) for _ in range(int(input()))), key=lambda x: (atan2(x[1], x[0]) % (2 * pi), x[0] ** 2 + x[1] ** 2))))))


import math
N = int(input().strip())
Pts = []
for dummyN in range(N):
	(x, y) = [int(x) for x in input().strip().split()]
	rho = math.atan2(y, x)
	if rho < 0:
		rho += 2 * math.pi
	Pts.append((rho, x * x + y * y, x, y))
Pts.sort()
for Pt in Pts:
	print(Pt[2], Pt[3])


from math import atan2, pi, hypot
n = int(input().strip())
a_i = 0
a_idx = []
sorted_h = {}
for a_i in range(n):
	z_pos = input().strip().split(' ')
	x = float(z_pos[0])
	y = float(z_pos[1])
	at = atan2(y, x)
	if at < 0:
		at = 2 * pi + at
	a_idx.append({'r': at, 'x': x, 'y': y, 'h': hypot(x, y)})
yy = sorted(a_idx, key=lambda z_ang: z_ang['h'])
xx = sorted(yy, key=lambda z_ang: z_ang['r'])
for a_i in xx:
	print('{0} {1}'.format(int(a_i['x']), int(a_i['y'])))


import math

class Point:

	def __init__(self, x, y):
		self.x = x
		self.y = y
		self.radius = (x * x + y * y) ** 0.5
		self.angle = math.atan2(y, x)
		if self.angle < 0:
			self.angle += 2 * math.pi

	def __str__(self):
		return str(self.x) + ' ' + str(self.y)
pts = []
for _ in range(int(input())):
	pts.append(Point(*[int(a) for a in input().split()]))
print(*sorted(pts, key=lambda p: (p.angle, p.radius)), sep='\n')


from math import pi, atan2
print('\n'.join(map('{} {}'.format, *zip(*sorted((list(map(int, input().split())) for _ in range(int(input()))), key=lambda x: (atan2(x[1], x[0]) % (2 * pi), x[0] ** 2 + x[1] ** 2))))))


import math
N = int(input())
L = []
for i in range(N):
	tokens = input().split()
	(x, y) = (int(tokens[0]), int(tokens[1]))
	r = x ** 2 + y ** 2
	theta = math.atan2(y, x)
	if theta < 0:
		theta += 2 * math.pi
	L.append((x, y, r, theta))
for t in sorted(L, key=lambda t: (t[3], t[2])):
	print(t[0], t[1])


import math

def to_polar_angle(x, y):
	if x == 0:
		if y > 0:
			return math.pi / 2
		elif y < 0:
			return math.pi * 3 / 2
		else:
			raise ValueError
	angle = math.atan(y / x)
	if x < 0:
		return angle + math.pi
	if y < 0:
		return angle + 2 * math.pi
	return angle

def to_radius(x, y):
	return math.sqrt(x * x + y * y)
N = int(input().strip())
L = []
for i in range(N):
	(x, y) = map(int, input().strip().split())
	L.append((to_polar_angle(x, y), to_radius(x, y), x, y))
L.sort()
for i in L:
	print(i[2], i[3])


import math

def angle(x, y):
	if x == 0:
		if y > 0:
			k = 90
		else:
			k = 270
	elif x > 0 and y >= 0:
		k = math.atan(y / x)
	elif x < 0 and y >= 0:
		k = 180 + math.atan(y / x)
	elif x < 0 and y <= 0:
		k = 180 + math.atan(y / x)
	else:
		k = 360 + math.atan(y / x)
	l = []
	l.append(k)
	l.append(math.sqrt(x ** 2 + y ** 2))
	return l
X = []
Y = []
Q = []
L = []
t = int(input())
for i in range(t):
	a = input().split()
	x = int(a[0])
	y = int(a[1])
	X.append(x)
	Y.append(y)
	Q.append(angle(x, y))
L = sorted(Q)
for i in range(len(Q)):
	print(X[Q.index(L[i])], end=' ')
	print(Y[Q.index(L[i])])


import cmath
t = int(input())
i = 0
li1 = []
li2 = []
while i < t:
	li = []
	(a, b) = map(int, input().split())
	x = cmath.phase(complex(a, b))
	y = abs(complex(a, b))
	if x >= 0:
		li.append(a)
		li.append(b)
		li.append(x)
		li.append(y)
		li1.append(li)
	else:
		li.append(a)
		li.append(b)
		li.append(x)
		li.append(y)
		li2.append(li)
	i = i + 1
li1 = sorted(li1, key=lambda row: (row[2], row[3]))
li2 = sorted(li2, key=lambda row: (row[2], row[3]))
for c in li1:
	c.pop(2)
	c.pop(2)
	print(' '.join(map(str, c)))
for c in li2:
	c.pop(2)
	c.pop(2)
	print(' '.join(map(str, c)))


import math
n = int(input())
l = []
for _ in range(n):
	(x, y) = map(int, input().split())
	(a, d) = (math.atan2(y, x), math.hypot(y, x))
	if a < 0:
		a += math.pi * 2
	l.append((a, d, x, y))
l.sort()
for (a, d, x, y) in l:
	print(x, y)


import math
n = int(input())
angles = {}
for _ in range(n):
	(x, y) = input().split()
	(x, y) = (int(x), int(y))
	tempangle = math.atan2(y, x)
	if tempangle < 0:
		tempangle += 2 * math.pi
	if tempangle in angles:
		angles[tempangle].append(x)
		angles[tempangle].append(y)
	else:
		angles[tempangle] = [x, y]
for angle in sorted(angles):
	if len(angles[angle]) > 2:
		idx = [angles[angle][2 * i] ** 2 + angles[angle][2 * i + 1] ** 2 for i in range(len(angles[angle]) // 2)]
		tempx = sorted(range(len(idx)), key=lambda k: idx[k])
		for j in tempx:
			print(angles[angle][2 * j], angles[angle][2 * j + 1])
		pass
	else:
		print(angles[angle][0], angles[angle][1])


from math import atan2, hypot, pi
polar = lambda x: [atan2(x[1], x[0]) % (2 * pi), hypot(x[0], x[1])]
(a, pts) = (int(input()), [])
for _ in range(a):
	pts.append(list(map(int, input().split())))
for i in sorted(pts, key=lambda x: polar(x)):
	print(' '.join(map(str, i)))


from math import pi, atan2
print('\n'.join(map('{} {}'.format, *zip(*sorted((list(map(int, input().split())) for _ in range(int(input()))), key=lambda x: (atan2(x[1], x[0]) % (2 * pi), x[0] ** 2 + x[1] ** 2))))))


from math import pi, atan2
print('\n'.join(map('{} {}'.format, *zip(*sorted((list(map(int, input().split())) for _ in range(int(input()))), key=lambda x: (atan2(x[1], x[0]) % (2 * pi), x[0] ** 2 + x[1] ** 2))))))


from math import atan2, pi
N = int(input())
points = []
for i in range(N):
	points.append(tuple(map(int, input().split())))
points.sort(key=lambda p: (atan2(p[1], p[0]) % (2 * pi), p[0] ** 2 + p[1] ** 2))
for p in points:
	print(p[0], p[1])


def dist(a):
	(x, y) = a
	return x ** 2 + y ** 2

def angle(a):
	(x, y) = a
	if x > 0 and y >= 0:
		return y / x
	elif x < 0 and y >= 0:
		return 1000 - y / abs(x)
	elif x < 0:
		return 2000 + y / x
	elif x > 0:
		return 3000 - abs(y) / x
	elif x == 0 and y > 0:
		return 500
	return 2500
n = int(input())
a = []
for i in range(n):
	a.append(list(map(int, input().split())))
a.sort(key=dist)
a.sort(key=angle)
for i in a:
	print(*i)


from math import atan
from math import pi

def f(x, y):
	if (x > 0) & (y >= 0):
		theta = y / x
	elif (x == 0) & (y > 0):
		theta = 101
	elif (x < 0) & (y >= 0):
		theta = 202 + y / x
	elif (x < 0) & (y < 0):
		theta = 202 + y / x
	elif (x == 0) & (y < 0):
		theta = 303
	else:
		theta = 404 + y / x
	return (theta, x ** 2 + y ** 2)
N = int(input())
A = {}
for n0 in range(N):
	(x, y) = (int(a) for a in input().split())
	(theta, dist) = f(x, y)
	if (theta, dist) in A:
		A[theta, dist + 1] = (x, y)
	A[theta, dist] = (x, y)
for a in sorted(A):
	res = str(A[a][0]) + ' ' + str(A[a][1])
	print(res)


from math import *

def fun(p1, p2):
	(a1, x1, y1) = p1
	(a2, x2, y2) = p2
	if a1 != a2:
		return a1 < a2
	return x1 * x1 + y1 * y1 < x2 * x2 + y2 * y2
p = []
for _ in range(int(input())):
	(x, y) = map(int, input().split())
	if x == 0:
		if y < 0:
			a = 3 * pi / 2
		else:
			a = pi / 2
	else:
		a = atan2(y, x)
		if a < 0:
			a += 2 * pi
	p.append((a, x * x + y * y, x, y))
p.sort()
for (_, _, x, y) in p:
	print(x, y)


import math
pi2 = 2 * math.pi
points = []
for _ in range(int(input())):
	(x, y) = map(int, input().split())
	points.append((x, y, (pi2 + math.atan2(y, x)) % pi2, x ** 2 + y ** 2))
points.sort(key=lambda x: (x[2], x[3]))
for (x, y, a, d) in points:
	print(x, y)


import math

def func(x, y):
	if y == 0:
		if x >= 0:
			return 0
		else:
			return 180
	elif y >= 0:
		return 90
	else:
		return 270

def slope(x, y):
	bias = 0
	ans = 0
	if x >= 0 and y >= 0:
		bias += 0
	elif x < 0 and y >= 0:
		bias += 90
	elif x < 0 and y < 0:
		bias += 180
	else:
		bias += 270
	if y == 0 or x == 0:
		ans = func(x, y)
		return ans
	else:
		ans = math.degrees(math.atan(y / x))
		if ans < 0:
			ans = 90 + ans
		return ans + bias
	return 0

def dis(x, y):
	return (x ** 2 + y ** 2) ** 0.5
t = int(input())
s = []
for _ in range(t):
	(x, y) = map(int, input().split())
	s.append([slope(x, y), dis(x, y), x, y])
s = sorted(s, key=lambda x: (x[0], x[1]))
for i in s:
	print(i[2], i[3])


import math

def angle(x, y):
	if x > 0 and y == 0:
		return 0
	if x == 0 and y > 0:
		return math.pi / 2.0
	if x < 0 and y == 0:
		return math.pi
	if x == 0 and y < 0:
		return 3.0 * math.pi / 2.0
	if x > 0 and y > 0:
		return math.atan(y / x)
	return math.pi / 2.0 + angle(y, -x)
n = int(input())
A = []
for _ in range(n):
	(x, y) = (int(t) for t in input().split())
	A.append((angle(x, y), x * x + y * y, x, y))
A = sorted(A)
for t in A:
	print(t[2], t[3])


import math, operator
n = int(input())
arr = []
for _ in range(n):
	(x, y) = [int(val) for val in input().split()]
	angle = math.atan2(y, x)
	angle = 2 * math.pi + angle if angle < 0 else angle
	arr.append((x, y, math.hypot(x, y), angle))
for (x, y, _, _) in sorted(arr, key=operator.itemgetter(3, 2)):
	print(x, y)


from math import pi, atan2
print('\n'.join(map('{} {}'.format, *zip(*sorted((list(map(int, input().split())) for _ in range(int(input()))), key=lambda x: (atan2(x[1], x[0]) % (2 * pi), x[0] ** 2 + x[1] ** 2))))))


from math import pi, atan2
print('\n'.join(map('{} {}'.format, *zip(*sorted((list(map(int, input().split())) for _ in range(int(input()))), key=lambda x: (atan2(x[1], x[0]) % (2 * pi), x[0] ** 2 + x[1] ** 2))))))


import math as m

def main():
	p = []
	for i in range(int(input())):
		(x, y) = map(int, input().split())
		v = m.atan2(y, x)
		if v < 0:
			v += 2 * m.pi
		d = (x ** 2 + y ** 2) ** 0.5
		p.append((v, d))
	p.sort()
	for (t, r) in p:
		if t > m.pi:
			t -= 2 * m.pi
		print(round(r * m.cos(t)), round(r * m.sin(t)))
main()


import os
import sys
from math import *

def mag(x, y):
	return x ** 2 + y ** 2

def toPol(x, y):
	ans = atan2(y, x)
	ans = ans + 2 * pi if ans < 0 else ans
	return (ans, mag(x, y))

def solve(coordinates):
	return sorted(coordinates, key=lambda p: toPol(p[0], p[1]))
n = int(input())
coordinates = []
for _ in range(n):
	coordinates.append(list(map(int, input().rstrip().split())))
result = solve(coordinates)
print('\n'.join([' '.join(map(str, x)) for x in result]))


from functools import cmp_to_key
from math import atan2, pi, hypot
n = int(input())
coordinates = []
for _ in range(n):
	coordinates.append(list(map(int, input().rstrip().split())))

def custom(a, b):
	(ax, ay) = a
	(bx, by) = b
	a_angle = atan2(ay, ax)
	b_angle = atan2(by, bx)
	if by < 0:
		b_angle += 2 * pi
	if ay < 0:
		a_angle += 2 * pi
	if a_angle > b_angle:
		return 1
	elif a_angle < b_angle:
		return -1
	else:
		return hypot(ax, ay) - hypot(bx, by)
coordinates.sort(key=cmp_to_key(custom))
for (x, y) in coordinates:
	print(x, y)