From c9a20027ca673bd7e13a1f240b7b66a836201eef Mon Sep 17 00:00:00 2001 From: Thomas Lin Pedersen Date: Thu, 11 Jan 2024 10:56:16 +0100 Subject: [PATCH] Make sure node_angle results in legible text --- R/utils.R | 13 ++++++++++--- man/node_angle.Rd | 7 +++++-- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/R/utils.R b/R/utils.R index 9309049e..28ad8da4 100644 --- a/R/utils.R +++ b/R/utils.R @@ -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 @@ -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 { @@ -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) } diff --git a/man/node_angle.Rd b/man/node_angle.Rd index a6b5c00f..4b25f68c 100644 --- a/man/node_angle.Rd +++ b/man/node_angle.Rd @@ -5,9 +5,9 @@ \alias{edge_angle} \title{Get the angle of nodes and edges} \usage{ -node_angle(x, y, degrees = TRUE) +node_angle(x, y, degrees = TRUE, avoid_flip = TRUE) -edge_angle(x, y, xend, yend, degrees = TRUE) +edge_angle(x, y, xend, yend, degrees = TRUE, avoid_flip = TRUE) } \arguments{ \item{x, y}{A vector of positions} @@ -15,6 +15,9 @@ edge_angle(x, y, xend, yend, degrees = TRUE) \item{degrees}{Logical. Should the angle be returned in degree (\code{TRUE}) or radians (\code{FALSE}). Defaults to \code{TRUE}.} +\item{avoid_flip}{Logical. Should the angle be adjusted so that text is +always upside-down} + \item{xend, yend}{The end position of the edge} } \value{