MCPcopy Index your code
hub / github.com/TheAlgorithms/Python / check_direction

Function check_direction

other/graham_scan.py:51–90  ·  view source on GitHub ↗

Return the direction toward to the line from via to target from starting :param starting: The starting point via: The via point target: The target point :return: the Direction Examples: >>> check_direction((1,1), (2,2), (3,3)) Direction.straight >>> c

(
    starting: tuple[int, int], via: tuple[int, int], target: tuple[int, int]
)

Source from the content-addressed store, hash-verified

49
50
51def check_direction(
52 starting: tuple[int, int], via: tuple[int, int], target: tuple[int, int]
53) -> Direction:
54 """Return the direction toward to the line from via to target from starting
55
56 :param starting: The starting point
57 via: The via point
58 target: The target point
59 :return: the Direction
60
61 Examples:
62 >>> check_direction((1,1), (2,2), (3,3))
63 Direction.straight
64
65 >>> check_direction((60,1), (-50,199), (30,2))
66 Direction.left
67
68 >>> check_direction((0,0), (5,5), (10,0))
69 Direction.right
70 """
71 x0, y0 = starting
72 x1, y1 = via
73 x2, y2 = target
74 via_angle = degrees(atan2(y1 - y0, x1 - x0))
75 via_angle %= 360
76 target_angle = degrees(atan2(y2 - y0, x2 - x0))
77 target_angle %= 360
78 # t-
79 # \ \
80 # \ v
81 # \|
82 # s
83 # via_angle is always lower than target_angle, if direction is left.
84 # If they are same, it means they are on a same line of convex hull.
85 if target_angle > via_angle:
86 return Direction.left
87 elif target_angle == via_angle:
88 return Direction.straight
89 else:
90 return Direction.right
91
92
93def graham_scan(points: list[tuple[int, int]]) -> list[tuple[int, int]]:

Callers 1

graham_scanFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected