From 21e9fca9db31dbc5dd529b1e3afc73b5c4c122a2 Mon Sep 17 00:00:00 2001 From: sbrockhaus Date: Mon, 25 Jul 2016 16:59:38 +0200 Subject: [PATCH] vectorize the function point_to_line() for time speed up --- R/utils.R | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/R/utils.R b/R/utils.R index 535dedd..e788c7a 100644 --- a/R/utils.R +++ b/R/utils.R @@ -29,17 +29,20 @@ extract_data <- function(data,use){ # that forms a line with P0 so that it is orthogonal to P1-P2. # For details regarding the formula, see # http://paulbourke.net/geometry/pointlineplane/ +# expects P0 to be a matrix point_to_line <- function(P0, P1, P2){ - u <- ( (P0[1]-P1[1]) * (P2[1]-P1[1]) + - (P0[2]-P1[2]) * (P2[2]-P1[2]) ) / + u <- ( (P0[1,]-P1[1]) * (P2[1]-P1[1]) + + (P0[2,]-P1[2]) * (P2[2]-P1[2]) ) / ( (P2[1]-P1[1])^2 + (P2[2]-P1[2])^2 ) - P <- c( + P <- matrix(c( P1[1] + u * (P2[1] - P1[1]), - P1[2] + u * (P2[2] - P1[2]) + P1[2] + u * (P2[2] - P1[2])), nrow = 2, byrow = TRUE ) + rownames(P) <- names(P1) + return(P) } @@ -69,11 +72,8 @@ points_on_ideal <- function(points, start=NULL, end=NULL){ result[1,] <- start[1] result[2,] <- start[2] } else { - result <- apply( - points, MARGIN=2, - FUN=point_to_line, - P1=start, P2=end - ) + # compute the projection onto the idealized straight line for all points + result <- point_to_line(P0 = points, P1 = start, P2 = end) } return(result)