I have two 3D points:
t0: [5,1,8], t200: [100,20,10]
I have to find the line between t0 and t200 and all 3D points (x,y,z) included between t0 and t200. Finally, plot it and create a dataframe like this:
x y z
t0 5 1 9
t1
...
t200 100 20 10
Any tip?
best regards
CodePudding user response:
Using mapply
and fast seq.int
.
mapply(\(x, y) seq.int(x, y, length.out=201), t0, t200)
And the data frame:
res <- mapply(\(x, y) seq.int(x, y, length.out=201), t0, t200) |>
`dimnames<-`(list(paste0('t', 0:200), letters[24:26])) |>
as.data.frame()
head(res)
# x y z
# t0 5.000 1.000 0.00
# t1 5.475 1.095 0.05
# t2 5.950 1.190 0.10
# t3 6.425 1.285 0.15
# t4 6.900 1.380 0.20
# t5 7.375 1.475 0.25
tail(res)
# x y z
# t195 97.625 19.525 9.75
# t196 98.100 19.620 9.80
# t197 98.575 19.715 9.85
# t198 99.050 19.810 9.90
# t199 99.525 19.905 9.95
# t200 100.000 20.000 10.00
Note: R >= 4.1
Data:
t0 <- c(5, 1, 0)
t200 <- c(100, 20, 10)
CodePudding user response:
Assuming you want a vector between t0 and t200, you could just use seq
with the same n
start <- c(5, 1, 9)
end <- c(100, 20, 10)
n <- 201
sapply(1:3, \(i) seq(start[i], end[i], length = n))
A faster method would be to invert the normalization formula x_norm = (x - x_min) / (x_max - x_min) <=> x = x_norm * (x_max - x_min) x_min
x_i <- seq(0, 1, length = n)
sapply(1:3, \(i)x_i * (end[i] - start[i]) start[i])
all.equal(sapply(1:3, \(i) seq(start[i], end[i], length = n)),
sapply(1:3, \(i)x_i * (end[i] - start[i]) start[i]))
[1] TRUE