Add missing points in a 2D line

have some 2D data in Julia representing a circular feature with some “holes” in it. I like to interpolate these holes by finding points fitting on a curve representing the shape of the feature.

Using Julia’s PlotlyJS library I was able to find a nice spline interpolation curve for the points visually. However, I am not able to “access” that interpolation to calculate the actual points required. Any (alternative?) idea on how to get that?

Here is a working example:

using PlotlyJS

someData = [
    3.47336 -0.471233;
    3.53109 0.335963;
    3.46748 1.10433;
    3.13369 1.87227;
    2.33268 2.51022;
    1.21804 3.07551;
    0.211065 3.3075;
    -0.768256 3.18599;
    -1.72856 2.87655;
    -2.55477 2.58726;
    -3.28657 1.99779;
    -3.63637 1.31502;
    -3.56652 -0.462201;
    -2.96175 -0.956073;
    -2.0519 -0.870708;
    -1.07193 -0.837913;
    -0.156219 -0.972855;
    0.594719 -1.4576;
    1.27607 -1.9387;
    2.08427 -2.17288;
    3.47336 -0.471233
]
  
  
plot([
    scatter(
        x=someData[:,1], y=someData[:,2], type="scatter", mode="markers", name="Some data",
        marker=attr(size=8, color="orange", opacity=1)
    ), 
    scatter(
        x=someData[:,1], y=someData[:,2], type="scatter", mode="lines", name="Plotly spline",
        line=attr(color="green", width=2, shape="spline")
    ),

    ], Layout(scene=attr(aspectmode="data"), showlegend=true)
)

You can use parametric splines. See some examples in this other thread.

1 Like

Thank you for the hint. Unfortunately I don’t fully understand how to adopt it. Could you give an example?

Copy and pasting here one of the examples and renaming your sample points as data:

using Dierckx, Plots
t = 1:size(data,1)
spl = ParametricSpline(t, data', bc="extrapolate", s=0.0)
tfine= range(1, size(data,1), 200)
Pfine = evaluate(spl, tfine)
t0, t1 = 12.5, 20.6
P0, P1 = evaluate.((spl,), [t0, t1])

plot(eachrow(Pfine)..., c=:blues)
scatter!(eachcol(data)...,legend=false )
scatter!([P0[1]], [P0[2]], ms=5, mc=:red)
scatter!([P1[1]], [P1[2]], ms=5, mc=:red)

Thank you for the example!

I have improved it further, parameterizing using integers, so that it is easier to count the points anti-clockwise and know where to place the new ones.

1 Like