Skip to content

Commit

Permalink
Refactor angle2(): instead of calling edge_distance, do the
Browse files Browse the repository at this point in the history
calculations directly. This saves 1 square root operation and
3 squared operations. This also reduces the floating point
representation error, and no longer need to round to 5.

Might be fringe cases where a tolerance is needed, I am not
sure.

See also: #19
  • Loading branch information
TaipanRex committed Jul 4, 2017
1 parent 4e288ea commit e9eeeaa
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions pyvisgraph/visible_vertices.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,11 +274,16 @@ def angle(center, point):


def angle2(point_a, point_b, point_c):
a = edge_distance(point_b, point_c)
b = edge_distance(point_a, point_c)
c = edge_distance(point_a, point_b)
x = (a**2 + c**2 - b**2) / (2 * a * c)
return acos(round(x, 5))
"""Return angle B (radian) between point_b and point_c.
c
/ \
/ B\
a-------b
"""
a = (point_c.x - point_b.x)**2 + (point_c.y - point_b.y)**2
b = (point_c.x - point_a.x)**2 + (point_c.y - point_a.y)**2
c = (point_b.x - point_a.x)**2 + (point_b.y - point_a.y)**2
return acos((a + c - b) / (2 * sqrt(a) * sqrt(c)))


def ccw(A, B, C):
Expand Down

0 comments on commit e9eeeaa

Please sign in to comment.