curve_intersect: Intersection of two curves

Description Usage Arguments Details Value Author(s) Examples

View source: R/intersect.R

Description

Calculate where two lines or curves intersect. Curves are defined as data frames with x and y columns providing cartesian coordinates for the lines. This function works on both linear and nonlinear curves.

Usage

1
curve_intersect(curve1, curve2, empirical = TRUE, domain = NULL)

Arguments

curve1

Either a data.frame with columns named x and y or a function.

curve2

Either data.frame with columns named x and y or a function.

empirical

If true (default) indicates that the curves are data frames of empirical data. If false, indicates that the curves are actual functions.

domain

Two-value numeric vector indicating the bounds along the x-axis where the intersection should be found when empirical is false

Details

For now, curve_intersect will only find one intersection.

If you define curves with empirical data frames (i.e. provide actual values for x and y), ensure that empirical = TRUE.

If you define curves with functions (i.e. curve1 <- x^2), ensure that empirical = FALSE and provide a range of x-axis values to search for an intersection using domain.

Value

A list with x and y values.

Author(s)

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
# Straight lines (empirical)
line1 <- data.frame(x = c(1, 9), y = c(1, 9))
line2 <- data.frame(x = c(9, 1), y = c(1, 9))

curve_intersect(line1, line2)

# Curved lines (empirical)
curve1 <- data.frame(Hmisc::bezier(c(1, 8, 9), c(1, 5, 9)))
curve2 <- data.frame(Hmisc::bezier(c(1, 3, 9), c(9, 3, 1)))

curve_intersect(curve1, curve2)

# Curved lines (functional)
curve1 <- function(q) (q - 10)^2
curve2 <- function(q) q^2 + 2*q + 8

curve_intersect(curve1, curve2, empirical = FALSE, domain = c(0, 5))

R-CoderDotCom/econocharts documentation built on Oct. 16, 2021, 12:42 p.m.