Changed angle methods

This commit is contained in:
Grant Sanderson 2020-02-11 19:48:50 -08:00
parent 699b886d68
commit 052aa8afe2

View file

@ -123,21 +123,10 @@ def z_to_vector(vector):
return np.dot(rotation_about_z(theta), phi_down) return np.dot(rotation_about_z(theta), phi_down)
# TODO, this is redundant with below
def angle_between(v1, v2):
return np.arccos(np.dot(
v1 / get_norm(v1),
v2 / get_norm(v2)
))
def angle_of_vector(vector): def angle_of_vector(vector):
""" """
Returns polar coordinate theta when vector is project on xy plane Returns polar coordinate theta when vector is project on xy plane
""" """
z = complex(*vector[:2])
if z == 0:
return 0
return np.angle(complex(*vector[:2])) return np.angle(complex(*vector[:2]))
@ -146,10 +135,8 @@ def angle_between_vectors(v1, v2):
Returns the angle between two 3D vectors. Returns the angle between two 3D vectors.
This angle will always be btw 0 and pi This angle will always be btw 0 and pi
""" """
return np.arccos(fdiv( diff = (angle_of_vector(v2) - angle_of_vector(v1)) % TAU
np.dot(v1, v2), return min(diff, TAU - diff)
get_norm(v1) * get_norm(v2)
))
def project_along_vector(point, vector): def project_along_vector(point, vector):
@ -304,6 +291,7 @@ def norm_squared(v):
return sum(v * v) return sum(v * v)
# TODO, fails for polygons drawn over themselves
def earclip_triangulation(verts, rings): def earclip_triangulation(verts, rings):
n = len(verts) n = len(verts)
# Establish where loop indices should be connected # Establish where loop indices should be connected
@ -336,9 +324,8 @@ def earclip_triangulation(verts, rings):
# Find an ordering of indices walking around the polygon # Find an ordering of indices walking around the polygon
indices = [] indices = []
i = 0 i = 0
starting = True for x in range(n + len(rings) - 1):
while (i != 0 or starting): # starting = False
starting = False
if i in loop_connections: if i in loop_connections:
j = loop_connections[i] j = loop_connections[i]
indices.extend([i, j]) indices.extend([i, j])
@ -346,6 +333,8 @@ def earclip_triangulation(verts, rings):
else: else:
indices.append(i) indices.append(i)
i = after[i] i = after[i]
if i == 0:
break
meta_indices = earcut(verts[indices, :2], [len(indices)]) meta_indices = earcut(verts[indices, :2], [len(indices)])
return [indices[mi] for mi in meta_indices] return [indices[mi] for mi in meta_indices]