R/intersect_intervals.R

Defines functions intersect_intervals are_disjoint_intervals is_contained

Documented in are_disjoint_intervals intersect_intervals is_contained

#' Return the Intersection of Two Intervals
#'
#' @param int_1 double(2) \cr
#' An interval to intersect with [int_2]
#'
#' @param int_2 double(2) \cr
#' An interval to intersect with [int_1]
#'
intersect_intervals <- function(int_1, int_2) {

  if (are_disjoint_intervals(int_1, int_2)) return(NULL)
  if (is_contained(this = int_1, in_this = int_2)) return(int_1)
  if (is_contained(this = int_2, in_this = int_1)) return(int_2)

  if (min(int_1) < min(int_2)) return(c(min(int_2), max(int_1)))
  if (min(int_2) < min(int_1)) return(c(min(int_1), max(int_2)))

  rlang::abort("intersect_intervals: unanticipated condition")
}

#' Check if Two Intervals are Disjoint
#'
#' @param int_1 double(2) \cr
#' An interval to compare with [int_2]
#'
#' @param int_2 double(2) \cr
#' An interval to compare with [int_1]
#'
#' @inheritParams intersect_intervals
are_disjoint_intervals <- function(int_1, int_2) {
  max(int_1) < min(int_2) | max(int_2) < min(int_1)
}

#' Check if One Interval is Contained in Another
#'
#' @param this double(2) \cr
#' An interval that may fall inside the interval [in_this]
#'
#' @param in_this double(2) \cr
#' An interval that may contain the interval [this]
#'
is_contained <- function(this, in_this) {
  min(this) >= min(in_this) &
  max(this) <= max(in_this)
}
adviksh/winfer documentation built on Dec. 24, 2019, 7:05 p.m.