Skip to content

face_detector

face_detector

sort_counterclockwise(points, centre=None)

This function is used to sort the points in a counterclockwise manner.

Parameters

points: the list of points to be sorted in the form of [(x,y), (x,y), ..., (x,y)] centre (optional): the center point of the list of points. If not provided, the function calculates the center of the points using the average of the x and y coordinates.

Returns

counterclockwise_points: the sorted points in counterclockwise order as a list of tuples

Source code in redesign_pipeline/face_detectors/face_detector.py
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
def sort_counterclockwise(points, centre=None):
    """
    This function is used to sort the points in a counterclockwise manner.

    Parameters
    ----------

    points: the list of points to be sorted in the form of [(x,y), (x,y), ..., (x,y)]
    centre (optional): the center point of the list of points. If not provided, the function
    calculates the center of the points using the average of the x and y coordinates. 

    Returns
    -------

    counterclockwise_points: the sorted points in counterclockwise order as a list of tuples

    """
    if centre:
        centre_x, centre_y = centre
    else:
        centre_x, centre_y = sum([x for x,_ in points])/len(points), sum([y for _,y in points])/len(points)
    angles = [math.atan2(y - centre_y, x - centre_x) for x,y in points]
    counterclockwise_indices = sorted(range(len(points)), key=lambda i:angles[i])
    counterclockwise_points = [points[i] for i in counterclockwise_indices]
    return counterclockwise_points