Hello,
Given two distinct points Start =[xS,yS] and End = [xE,yE] in 2d, my aim is to find a simple way to generate N evenly-spaced points along the shortest path between Start and End.
That is, I want a generalization of something like LinRange(xS, xE, N)
Pseudocode
Input: Start = [xS, yS]
End = [xE, yE]
N
Output: Path = [xS,yS; ... ; xE,yE] with len(Path) = N
# vertical line
if xE-xS == 0 && |yE-yS| > 0
yPts = LinRange(yS, yE, N)
for i in 1:N
Path = [xS, yPts[i]]
end
# horizontal line
elseif |xE-xS| > 0 && yE-yS == 0
xPts = LinRange(xS, xE, N)
for i in 1:N
Path = [xPts[i], yS]
end
# sloped line: y[i] = m x[i] + b
elseif |xE-xS| > 0 && |yE-yS| > 0
slope = (yE-yS)/(xE-xS)
b = yS - slope*xS
xPts = LinRange(xS, xE, N)
for i in 1:N
Path = [xPts[i], slope*xPts[i]+b ]
end
elseif
println("Start and end point are the same")
end
Attempt
function find_2dPath(Start::Matrix{Float64}, End::Matrix{Float64}, N::Int)
xS = Start[1]
yS = Start[2]
xE = End[1]
yE = End[2]
Path = Matrix{Float64}(undef,N,2)
#@show Path
#println("Type of 'Path' = ", typeof(Path))
if xE-xS == 0 && abs(yE-yS) > 0 #vertical line
yPts = LinRange(yS, yE, N)
for i in 1:N
newPt = convert(Matrix{Float64,1}, [xS, yPts[i]] )
Path = push!(Path, newPt)
end
elseif abs(xE-xS) > 0 && yE-yS == 0 #horizontal line
xPts = LinRange(xS, xE, N)
for i in 1:N
newPt = convert(Matrix{Float64,1}, [xPts[i], yS] )
Path = push!(Path, newPt)
end
elseif abs(xE-xS) > 0 && abs(yE-yS) > 0 #sloped line
slope = (yE-yS)/(xE-xS)
b = yS - slope*xS
xPts = LinRange(xS, xE, N)
for i in 1:N
ypt = slope*xPts[i] + b #y = mx+b
# newPt = convert(Array{Float64,1}, [xPts[i], ypt] )
# newPt = reshape([xPts[i], ypt], 2, 1)
newPt = reshape( reduce(vcat,transpose.([xPts[i], ypt])) , 2, 1)
Path = push!(Path, newPt )
end
end
return Path
end
let
## Example 1: Path = 10 points between (0,0) -> (1,1)
Start1 = [0.0, 0.0]
End1 = [1.0, 1.0]
N1 = 10
path1 = find_2dPath(Start1,End1, N1)
@show path1
end
Generic error message
ERROR: LoadError: MethodError: no method matching find_2dPath(::Vector{Float64}, ::Vector{Float64}, ::Int64)
Closest candidates are:
find_2dPath(::Matrix{Float64}, ::Matrix{Float64}, ::Int64) at
...
Any help you can give me in understanding or resolving this error message is greatly appreciated!