Skip to content

Commit

Permalink
Merge pull request #3 from sbrockhaus/master
Browse files Browse the repository at this point in the history
vectorize the function point_to_line() for time speed up
  • Loading branch information
PascalKieslich authored Aug 1, 2016
2 parents 3955468 + 21e9fca commit 0b7aa02
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 0b7aa02

Please sign in to comment.