Topic: Utilities

Topic Description:

Basic R utilities used in many packages and functions, such as date and string manipulations, statistical distributions, geometry of lines and distances. The R package date is required for the two data functions.



File: utilities/geometry.r

View File Source Download File No help file available

Function: intersection.of.lines

Function Description: intersection.of.lines

Finds the point where 2 lines intersect, given lines as 2 parameters each (intercept b then slope m). If the two lines are identical, it returns NAs. Note the check for both slopes being infinite (vertical lines). This is vectorized: either pair can be vector if other pair is atomic, or both can be same length vectors.

Function Arguments:

ArgumentDefault Value
b1
m1
b2
m2

Function Source:

intersection.of.lines=function(b1,m1,b2,m2)
{
len=IfElse(length(b2)>length(b1),length(b2),length(b1))

m1inf=(is.infinite(m1)&!is.infinite(m2))
m2inf=(is.infinite(m2)&!is.infinite(m1))
mfinite=(!m1inf&!m2inf)

exact=(m1==m2 & b1==b2) | (is.infinite(m1) & is.infinite(m2))

x=(b2-b1)/(m1-m2)
y=m1*x+b1

x[m1inf]=b1[m1inf]
y[m1inf]=(m2*b1+b2)[m1inf]

x[m2inf]=b2[m2inf]
y[m2inf]=(m1*b2+b1)[m2inf]

x[exact]=NA
y[exact]=NA

return(data.frame(x,y))
}