mirror of
https://github.com/3b1b/manim.git
synced 2025-08-20 05:14:12 +00:00
Add line_intersects_path
This commit is contained in:
parent
923066db2b
commit
de4a56849e
1 changed files with 23 additions and 0 deletions
|
@ -309,6 +309,29 @@ def find_intersection(
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
|
def line_intersects_path(
|
||||||
|
start: Vect2 | Vect3,
|
||||||
|
end: Vect2 | Vect3,
|
||||||
|
path: Vect2Array | Vect3Array,
|
||||||
|
) -> bool:
|
||||||
|
"""
|
||||||
|
Tests whether the line (start, end) intersects
|
||||||
|
a polygonal path defined by its vertices
|
||||||
|
"""
|
||||||
|
n = len(path)
|
||||||
|
p1 = np.tile(start[:2], n).reshape((n, 2))
|
||||||
|
q1 = np.tile(end[:2], n).reshape((n, 2))
|
||||||
|
p2 = path[:, :2]
|
||||||
|
q2 = np.vstack([path[1:, :2], path[:1, :2]])
|
||||||
|
|
||||||
|
v1 = q1 - p1
|
||||||
|
v2 = q2 - p2
|
||||||
|
|
||||||
|
mis1 = cross2d(v1, p2 - p1) * cross2d(v1, q2 - p1) < 0
|
||||||
|
mis2 = cross2d(v2, p1 - p2) * cross2d(v2, q1 - p2) < 0
|
||||||
|
return bool((mis1 * mis2).any())
|
||||||
|
|
||||||
|
|
||||||
def get_closest_point_on_line(a: VectN, b: VectN, p: VectN) -> VectN:
|
def get_closest_point_on_line(a: VectN, b: VectN, p: VectN) -> VectN:
|
||||||
"""
|
"""
|
||||||
It returns point x such that
|
It returns point x such that
|
||||||
|
|
Loading…
Add table
Reference in a new issue