3

I am trying to get a closed curve which have to go exactly through all the points (given below) and trying to get sample 100 equidistant points from the graph using R. The first and last points can be joined to get a closed curve.

Here is a set points

X            Y
-0.04494395  -0.051481617
-0.01102178  -0.032550217
0.01193315   -0.072318417
0.04585535   -0.053448067
0.07975850   -0.034595817
0.05682070   0.005183833
0.03377230   0.045034983
0.01057130   0.085048283
-0.02335468  0.066181283
-0.05729975  0.047296183
-0.03407970  0.007294283
-0.06801145  -0.011644717

I want to use spline fitting (not regression). Could anyone help me in this regard? Here is what tried so far

d=splinefun(x, y,method = c("natural"))

I stuck here: how to get 100 equidistant points from this function?

Janak
  • 653
  • 7
  • 25
  • This problem is solved using the answer given here: http://stackoverflow.com/questions/34606181/drawing-equidistant-points-from-the-sides-of-a-polygon> – Janak Jan 05 '16 at 09:22

2 Answers2

3

Perhaps this gets you started. The xsplinefunction allows for creating a closed spline - although I am unsure how to control the number of interpolated points. This answer demonstrates the use of the xspline function.

df <- read.table(text="
   x         y
                 -0.04494395 -0.051481617
                 -0.01102178 -0.032550217
                 0.01193315 -0.072318417
                 0.04585535 -0.053448067
                 0.07975850 -0.034595817
                 0.05682070  0.005183833
                 0.03377230  0.045034983
                 0.01057130  0.085048283
                 -0.02335468  0.066181283
                 -0.05729975  0.047296183
                 -0.03407970  0.007294283
                 -0.06801145 -0.011644717
                 ",
                 header=TRUE
)


plot(y ~ x, df)
spl <- xspline(df$x, df$y, open=FALSE, shape = -0.5, draw=FALSE) 
lines(spl)

enter image description here

Community
  • 1
  • 1
Marc in the box
  • 11,769
  • 4
  • 47
  • 97
  • Thanks Marc for the answer. This is not a closed curve. I need a non- self intersecting closed curve. The given co-ordinate in the question are in that order. – Janak Jan 04 '16 at 08:23
  • Sorry about that misunderstanding - above is an example but, unfortunately, I don't know how to set the number of points to 100 – Marc in the box Jan 04 '16 at 08:48
  • No problem. Thanks a lot for the above example. – Janak Jan 04 '16 at 10:39
0

More than 100 points will be formed, but I think this meets your goal:

    df <- read.table(text="
   x         y
                 -0.04494395 -0.051481617
                 -0.01102178 -0.032550217
                 0.01193315 -0.072318417
                 0.04585535 -0.053448067
                 0.07975850 -0.034595817
                 0.05682070  0.005183833
                 0.03377230  0.045034983
                 0.01057130  0.085048283
                 -0.02335468  0.066181283
                 -0.05729975  0.047296183
                 -0.03407970  0.007294283
                 -0.06801145 -0.011644717
                 ",
                 header=TRUE
)


xy<-xspline(df$x,df$y,draw = F,shape = -0.5,open = F)
xy

plot(xy$x,xy$y)

xspline(df$x,df$y,open = F,shape = -0.5)

Here is the graph: