Skip to content

Commit

Permalink
Make sure node_angle results in legible text
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasp85 committed Jan 11, 2024
1 parent e010f77 commit c9a2002
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
13 changes: 10 additions & 3 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
#' @param degrees Logical. Should the angle be returned in degree (`TRUE`)
#' or radians (`FALSE`). Defaults to `TRUE`.
#'
#' @param avoid_flip Logical. Should the angle be adjusted so that text is
#' always upside-down
#'
#' @return A vector with the angle of each node/edge
#'
#' @examples
Expand All @@ -27,9 +30,13 @@
#' expand_limits(x = c(-1.3, 1.3), y = c(-1.3, 1.3))
#' @export
#'
node_angle <- function(x, y, degrees = TRUE) {
node_angle <- function(x, y, degrees = TRUE, avoid_flip = TRUE) {
angles <- atan2(y, x)
angles[angles < 0] <- angles[angles < 0] + 2 * pi
if (avoid_flip) {
needs_flip <- angles > pi/2 & angles < 3*pi/2
angles[needs_flip] <- angles[needs_flip] + pi
}
if (degrees) {
angles * 360 / (2 * pi)
} else {
Expand All @@ -39,10 +46,10 @@ node_angle <- function(x, y, degrees = TRUE) {
#' @rdname node_angle
#'
#' @export
edge_angle <- function(x, y, xend, yend, degrees = TRUE) {
edge_angle <- function(x, y, xend, yend, degrees = TRUE, avoid_flip = TRUE) {
x <- xend - x
y <- yend - y
node_angle(x, y, degrees)
node_angle(x, y, degrees, avoid_flip = avoid_flip)
}


Expand Down
7 changes: 5 additions & 2 deletions man/node_angle.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit c9a2002

Please sign in to comment.