mirror of
https://github.com/3b1b/manim.git
synced 2025-08-21 05:44:04 +00:00
Added find_intersection
This commit is contained in:
parent
8638f7303a
commit
13b69a14d8
1 changed files with 29 additions and 0 deletions
|
@ -231,6 +231,35 @@ def line_intersection(line1, line2):
|
||||||
return np.array([x, y, 0])
|
return np.array([x, y, 0])
|
||||||
|
|
||||||
|
|
||||||
|
def find_intersection(p0, v0, p1, v1, threshold=1e-5):
|
||||||
|
"""
|
||||||
|
Return the intersection of a line passing through p0 in direction v0
|
||||||
|
with one passing through p1 in direction v1. (Or array of intersections
|
||||||
|
from arrays of such points/directions).
|
||||||
|
For 3d values, it returns the point on the ray p0 + v0 * t closest to the
|
||||||
|
ray p1 + v1 * t
|
||||||
|
"""
|
||||||
|
p0 = np.array(p0, ndmin=2)
|
||||||
|
v0 = np.array(v0, ndmin=2)
|
||||||
|
p1 = np.array(p1, ndmin=2)
|
||||||
|
v1 = np.array(v1, ndmin=2)
|
||||||
|
m, n = np.shape(p0)
|
||||||
|
assert(n in [2, 3])
|
||||||
|
|
||||||
|
numer = np.cross(v1, p1 - p0)
|
||||||
|
denom = np.cross(v1, v0)
|
||||||
|
if n == 3:
|
||||||
|
d = len(np.shape(numer))
|
||||||
|
new_numer = np.multiply(numer, numer).sum(d - 1)
|
||||||
|
new_denom = np.multiply(denom, numer).sum(d - 1)
|
||||||
|
numer, denom = new_numer, new_denom
|
||||||
|
|
||||||
|
denom[abs(denom) < threshold] = np.inf # So that ratio goes to 0 there
|
||||||
|
ratio = numer / denom
|
||||||
|
ratio = np.repeat(ratio, n).reshape((m, n))
|
||||||
|
return p0 + ratio * v0
|
||||||
|
|
||||||
|
|
||||||
def get_winding_number(points):
|
def get_winding_number(points):
|
||||||
total_angle = 0
|
total_angle = 0
|
||||||
for p1, p2 in adjacent_pairs(points):
|
for p1, p2 in adjacent_pairs(points):
|
||||||
|
|
Loading…
Add table
Reference in a new issue