-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmath_tools.py
69 lines (59 loc) · 1.76 KB
/
math_tools.py
1
2
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#v0.1a
# math tools
import math
import pymel.core as pm
def get_line_circle_intersections(A, B, C, r):
"""Finds line and circle intersectinos
# Code source:
http://csharphelper.com/blog/2014/09/determine-where-a-line-intersects-a-circle-in-c/
# Args:
- A -point, start of line
- B -point, end of line
- C -point, circle's center
- r -float, circle's radius
# Returns, always two points:
- if not interseection(s) - points at origin
- if tangential - first point is result second at origin
# Usage:
A = pm.getAttr("A.t")
B = pm.getAttr("B.t")
C = pm.getAttr("C.t")
r = 10.0
P1, P2 = get_line_circle_intersections(A, B, C, r)
pm.move("P1", P1[0], P1[1], P1[2])
pm.move("P2", P2[0], P2[1], P2[2])
"""
Lx = B[0] - A[0]
Ly = B[1] - A[1]
Lz = B[2] - A[2]
# stranger things
D = Lx**2 + Ly**2
E = 2 * ( Lx * (A[0] - C[0]) + Ly * (A[1] - C[1]) )
F = (
(A[0] - C[0])**2
+ (A[1] - C[1])**2
- r**2
)
det = E**2 - 4 * D * F
# declare null vectors
P1 = [0, 0, 0]
P2 = [0, 0, 0]
t1 = t2 = None
eps = .00001
if ( not (D <= eps) or (det < 0) ):
if det == 0:
print "tangential intersection found",
t1 = t2 = -E / (2*D)
else:
print "pass-through intersection found",
t1 = ( (-E + math.sqrt(det)) / (2 * D) )
t2 = ( (-E - math.sqrt(det)) / (2 * D) )
P1[0] = A[0] + t1 * Lx
P1[1] = A[1] + t1 * Ly
P1[2] = A[2] + t1 * Lz
P2[0] = A[0] + t2 * Lx
P2[1] = A[1] + t2 * Ly
P2[2] = A[2] + t2 * Lz
else:
print "no intersections are available",
return P1, P2