import math


def haversine(phi1, lambda1, phi2, lambda2, radius):
	""" Calculates distance between two points on sphere of given radius.
	Points are specified by their angles:
	- phi: vertical position (latitude);
	- lambda: horizontal position (longitude).
	"""
	phi1, lambda1, phi2, lambda2 = map(math.radians, (phi1, lambda1, phi2, lambda2))
	d_phi = phi2 - phi1
	d_lambda = lambda2 - lambda1
	a = math.sin(d_phi/2.) * math.sin(d_phi/2.) + math.cos(phi1) * math.cos(phi2) * math.sin(d_lambda/2.) * math.sin(d_lambda/2.)
	c = 2 * math.atan2(math.sqrt(a), math.sqrt(1 - a))
	d = radius * c
	return d