Example #1
0
def initialize_clusters(markers, distance_threshold, bearing_threshold):
    '''
	Create an initial set of clusters.

	Return a list of Cluster objects.

	markers is a list of PointWithID instances.

	We arbitrarily select a marker that has not yet been assigned to a cluster, and create
	a new cluster at the marker. Then, any markers that are within distance_threshold and
	bearing_threshold of the selected marker are added to the cluster.
	
	For example, suppose that we have created a cluster at (26, 0, 83), and the remaining
	seed points are at (0, 0, 90), (30, 0, 90), (50, 10, 0), and (50, 40, 0). Then,
	(30, 0, 90) is assigned to the existing cluster since the distance to the cluster is 4
	while the bearing difference is 7, which are both within the thresholds. The other seed
	points become new clusters.
	
	You might find Index (from util.py) useful to query points that are near a specified
	location.
	'''
    clusters = []
    markers = markers[:]
    index = Index(markers)
    while markers:
        random_marker = markers[random.randint(0, len(markers) - 1)]
        markers.remove(random_marker)

        nearest_markers = index.nearby(random_marker, distance_threshold)

        nearest_markers = [
            marker for marker in nearest_markers
            if marker.angle_to(random_marker) <= bearing_threshold
        ]

        cluster = Cluster(random_marker)
        cluster.add_member(random_marker)
        for marker in nearest_markers:
            cluster.add_member(marker)

        clusters.append(cluster)

        markers = list(set(markers) - set(nearest_markers))
    return clusters