Skip to content

Commit

Permalink
Workaround precision errors
Browse files Browse the repository at this point in the history
Lack of precision in represenation of numbers can cause
numbers in the arc calculation to slip into undefined
spaces (arcos(-1-eps)).

This commit fixes this by rounding up to -1 if needed.
  • Loading branch information
Torvaney committed Apr 3, 2024
1 parent 3fe317f commit b991405
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
3 changes: 2 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# ggsoccer 0.1.8 (In progress)

- Add IMPECT pitch dimensions (`pitch_impect`)
- Add IMPECT pitch dimensions (`pitch_impect`)
- Fixed precision bug in arc drawing, which could cause the pitch to fail to render

# ggsoccer 0.1.7

Expand Down
10 changes: 9 additions & 1 deletion R/annotate_pitch.R
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,15 @@ annotate_intersection_arc <- function(xintercept, x0, y0, r, direction, ...) {
# `annotate_intersection_arc` with `xintercept = spec$penalty_spot_distance`
# on top of a pitch_international pitch and comparing to the drawn arc)
# but it is close enough.
angle <- acos((r^2 + r^2 - abs(pos_y - neg_y)^2)/(2*r^2))

# However, it's possible to get NaNs here!
# This is because of the inaccuracies in double-precision numbers.
# Consequently, if you have a perfect semi-circle (which should have an
# angle of `acos(-1) = pi`), you might try to calculate `acos(-(1 + epsilon))`
# where epsilon is some tiny value. The arccos of anything greater than -1 is
# undefined.
# To get around this, we cap the inner calculation to be -1 at the least
angle <- acos(pmax(-1, (r^2 + r^2 - abs(pos_y - neg_y)^2)/(2*r^2)))
arc_proportion <- angle/(2*pi)
curvature <- -arc_proportion/(arc_proportion-1)

Expand Down

0 comments on commit b991405

Please sign in to comment.