A utility function for drawing vector diagrams. Find position of an interpolated point along a line from x1
to x2
.
Arguments
- x1
A vector of length 2 or 3, representing the starting point of a line in 2D or 3D space
- x2
A vector of length 2 or 3, representing the ending point of a line in 2D or 3D space
- d
The distance along the line from
x1
tox2
of the point to be found.- absolute
logical; if
TRUE
,d
is taken as an absolute distance along the line; otherwise it is calculated as a relative distance, i.e., a fraction of the length of the line.
Details
The function takes a step of length d
along the line defined by the difference between the two points, x2 - x1
.
When absolute=FALSE
, this step is proportional to the difference,
while when absolute=TRUE
, the difference is first scaled to unit length so that the step is always of length d
.
Note that the physical length of a line in different directions in a graph depends on the aspect ratio of the plot axes,
and lines of the same length will only appear equal if the aspect ratio is one
(asp=1
in 2D, or aspect3d("iso")
in 3D).
Examples
x1 <- c(0, 0)
x2 <- c(1, 4)
pointOnLine(x1, x2, 0.5)
#> [1] 0.1212678 0.4850713
pointOnLine(x1, x2, 0.5, absolute=FALSE)
#> [1] 0.5 2.0
pointOnLine(x1, x2, 1.1)
#> [1] 0.2667892 1.0671568
y1 <- c(1, 2, 3)
y2 <- c(3, 2, 1)
pointOnLine(y1, y2, 0.5)
#> [1] 1.353553 2.000000 2.646447
pointOnLine(y1, y2, 0.5, absolute=FALSE)
#> [1] 2 2 2