MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / direction

Function direction

geometry/segment_intersection.py:30–48  ·  view source on GitHub ↗

Return the cross product of vectors (pivot->query) and (pivot->target). The sign of the result encodes the orientation of the ordered triple (pivot, target, query): - Negative -> counter-clockwise (left turn) - Positive -> clockwise (right turn) - Zero -> colline

(pivot: Point, target: Point, query: Point)

Source from the content-addressed store, hash-verified

28
29
30def direction(pivot: Point, target: Point, query: Point) -> float:
31 """Return the cross product of vectors (pivot->query) and (pivot->target).
32
33 The sign of the result encodes the orientation of the ordered triple
34 (pivot, target, query):
35 - Negative -> counter-clockwise (left turn)
36 - Positive -> clockwise (right turn)
37 - Zero -> collinear
38
39 >>> direction(Point(0, 0), Point(1, 0), Point(0, 1))
40 -1
41 >>> direction(Point(0, 0), Point(0, 1), Point(1, 0))
42 1
43 >>> direction(Point(0, 0), Point(1, 1), Point(2, 2))
44 0
45 """
46 return (query.x - pivot.x) * (target.y - pivot.y) - (target.x - pivot.x) * (
47 query.y - pivot.y
48 )
49
50
51def on_segment(seg_start: Point, seg_end: Point, point: Point) -> bool:

Callers 1

segments_intersectFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected