Hello everyone. I have created a 100*100 matrix in R. Let's now say that I have a line that starts from (2,3) point and ends to the (62,34) point. In other words this line starts at cell (2,3) and ends at cell (62,34). Is it possible to get by some R function all the matrix's cells that this line transverses? I would like to thank you for your feedback. Best Regards Alex [[alternative HTML version deleted]]
Hello Alex, Here is one way to do it. It works but it's not pretty :) interp <- approx(c(2, 62), c(3, 34), method="linear", xout=2:62) m <- matrix(c(interp$x, round(interp$y)), ncol=2) tie <- m[,2] == c(-Inf, m[-nrow(m),2]) m <- m[ !tie, ] You might want to examine the result like this... plot(m) # plots points lines(c(2,26), c(3, 34)) # overlay line for comparison Michael