Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

vectorize the function point_to_line() for time speed up #3

Merged
merged 1 commit into from
Aug 1, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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