# Control points from a B-Spline

**URL:** https://discourse.julialang.org/t/control-points-from-a-b-spline/29674
**Category:** Numerics
**Tags:** question, splines
**Created:** [October 9, 2019, 7:01am UTC](https://discourse.julialang.org/t/control-points-from-a-b-spline/29674 "2019-10-09T07:01:44Z")
**Posts on this page:** 15
**Page:** 1

<div class="post-metadata">

### Author: ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)
#### Post date: [October 9, 2019, 7:01am UTC](https://discourse.julialang.org/t/control-points-from-a-b-spline/29674/1 "2019-10-09T07:01:44Z")

</div>

Is there any way I can retrieve the control points/polygon from a B-Spline (calculated from `Dierckx.jl` or `Interpolations.jl`)?

Control points as described [here](https://en.wikipedia.org/wiki/B-spline) and shown as the black polygon here:  
 ![image](https://global.discourse-cdn.com/julialang/original/2X/9/93d77efccc13877875062ee11f586173af7cd1c6.png)

For instance, in the following MWE, how would I get the control polygon of the following spline:

```julia
using Dierckx, Makie

x = range(0, stop=1, length=5)
y = [0.82, 0.23, 0.82, 0.46, 0.22]
sc = scatter(x, y)
spl = Spline1D(x, y)
xl = range(0, stop=1, length=100)
yl = spl.(xl)
lines!(sc, xl, yl)

```

 ![plot](https://global.discourse-cdn.com/julialang/original/2X/7/75a4524d28268f644de6ea9f8764390c5be63e18.png)

Thanks!

---

<div class="post-metadata">

### Author: ![MatFi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/matfi/32/10002_2.png) [@MatFi](https://discourse.julialang.org/u/MatFi)
#### Post date: [October 9, 2019, 8:21am UTC](https://discourse.julialang.org/t/control-points-from-a-b-spline/29674/2 "2019-10-09T08:21:32Z")

</div>

Just to make sure that I understood you correctly:  
you want to have a function  
`f(xl,yl) -> (x,y) `  
I would guess that this is not unambiguously feasible unless you specify at least the `x` values for which you want the `y`. This is because multiple sets of `(x,y)` can lead to the same spline function. Using only the values of the spline function at different points `xl` does not make it any easier.

---

<div class="post-metadata">

### Author: ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)
#### Post date: [October 9, 2019, 8:28am UTC](https://discourse.julialang.org/t/control-points-from-a-b-spline/29674/3 "2019-10-09T08:28:01Z")

</div>

I assumed that given a set of discrete `xy` points and their corresponding spline (the result from, say, `Spline1D(x, y)`), I could get a unique control polygon. Ultimately I’d like to get a measure of how much the spline needed to “turn” to fit said points. I had hoped to get such a measure from the angles between the straight lines composing the polygon…

---

<div class="post-metadata">

### Author: ![MatFi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/matfi/32/10002_2.png) [@MatFi](https://discourse.julialang.org/u/MatFi)
#### Post date: [October 9, 2019, 8:39am UTC](https://discourse.julialang.org/t/control-points-from-a-b-spline/29674/4 "2019-10-09T08:39:48Z")

</div>

OK probably I’m turned completely wrong somewhere. But IMHO the (x,y) points are your control polygon already, why you don’t just connect them?

---

<div class="post-metadata">

### Author: ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)
#### Post date: [October 9, 2019, 8:42am UTC](https://discourse.julialang.org/t/control-points-from-a-b-spline/29674/5 "2019-10-09T08:42:54Z")

</div>

I think the (x,y) coordinates are the **knots** of the spline, which is different from the control points. I suspect that the control points are a artificial construct, and that these help visualize how the spline is generated, but do not have anything to do with the spline’s properties…

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [October 9, 2019, 8:51am UTC](https://discourse.julialang.org/t/control-points-from-a-b-spline/29674/6 "2019-10-09T08:51:23Z")

</div>

I am not sure what you are looking for here. B-splines are defined by a set of knots, are evaluated at some values x, and fit using a linear solution or least squares, to values or various derivatives. The x may or may not coincide with the knots — they don’t even have the same length, depending on how you set up the problem and the order of the splines

---

<div class="post-metadata">

### Author: ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)
#### Post date: [October 9, 2019, 9:00am UTC](https://discourse.julialang.org/t/control-points-from-a-b-spline/29674/7 "2019-10-09T09:00:39Z")

</div>

> [@Tamas\_Papp](#):
>
> I am not sure what you are looking for here.

Here are some definitions:

1. xy coordinates: the data the spline is fitted to
2. spline knots: xy coordinates the fitted spline passes through
3. spline degree: the degree of the function describing the spline
4. control points/polygon: a bunch of straight lines that have some relation to the resulting spline.

I’m wondering if there is any way I can get the control points/polygon.  
I just found this blog: [Bézier curves in Julia with animations](https://opensourc.es/blog/bezier-curve) about these control points. In any case, there is a nice illustration of these there, here it is:

 ![bezier_d2](https://global.discourse-cdn.com/julialang/original/3X/8/a/8acef2b9282eddaf245b82c14cd9a90072a94975.gif)

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [October 9, 2019, 9:06am UTC](https://discourse.julialang.org/t/control-points-from-a-b-spline/29674/8 "2019-10-09T09:06:37Z")

</div>

> [@yakir12](#):
>
> control points/polygon: a bunch of straight lines that have some relation to the resulting spline.

I think you are confusing B-splines with Bezier curves, they are different things.

Also, generally for B-splines, no “polygon” drawn, say, tangentially at various points would determine the spline except for some very special cases.

You may find reading an intro text about B-splines helpful.

---

<div class="post-metadata">

### Author: ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)
#### Post date: [October 9, 2019, 9:11am UTC](https://discourse.julialang.org/t/control-points-from-a-b-spline/29674/9 "2019-10-09T09:11:42Z")

</div>

> [@Tamas\_Papp](#):
>
> I think you are confusing B-splines with Bezier curves, they are different things.

I think you are correct. OK, so I’ll drop the idea of these control points/polygon when it comes to B-splines.

---

<div class="post-metadata">

### Author: ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)
#### Post date: [October 9, 2019, 9:19am UTC](https://discourse.julialang.org/t/control-points-from-a-b-spline/29674/10 "2019-10-09T09:19:35Z")

</div>

I still need to read that book you mentioned, but check this continuation post @Wikunia wrote:

> **[B-splines](https://opensourc.es/blog/b-spline/)**
>
> Explanation of B-splines and their comparison to Bézier splines as well as animations using Julia

It would be good to hear what Olle has to say too.

---

<div class="post-metadata">

### Author: ![Wikunia](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wikunia/32/2180_2.png) [@Wikunia](https://discourse.julialang.org/u/Wikunia)
#### Post date: [October 9, 2019, 9:54am UTC](https://discourse.julialang.org/t/control-points-from-a-b-spline/29674/11 "2019-10-09T09:54:22Z")

</div>

Thanks for reaching out. I think you have a misunderstanding of knots here as it really is just a list of number and not of coordinates.  
As mentioned I think the control polygon is not well defined. I normally construct it the other way around. It’s interesting though. Maybe one can dive deeper into the Julia package to find it out.  
I think I’m not quite sure what you’re interested in exactly. Maybe just curvature? Or what do you mean by “turn”?

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [October 9, 2019, 10:25am UTC](https://discourse.julialang.org/t/control-points-from-a-b-spline/29674/12 "2019-10-09T10:25:29Z")

</div>

My understanding of that blog post is that it describes how to approximate a polygon with B-splines. Reconstruction of such a polygon is an interesting problem, but I am not sure it has a unique solution.

---

<div class="post-metadata">

### Author: ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)
#### Post date: [October 9, 2019, 10:50am UTC](https://discourse.julialang.org/t/control-points-from-a-b-spline/29674/13 "2019-10-09T10:50:00Z")

</div>

Thanks to you both!

What I’m truly after is some reliable measure of how curvy a section of a trajectory is.

I have a trajectory, a set of xy coordinates + time. These are noisy and not entirely reliable. I want to smooth the trajectory using splines. These trajectories are easily divided into two parts:

1. a more or less straight part where there are hardly any turns larger than 45 degrees.
2. a more curvy part where there is a mix of turns, some of which are 189 degrees.

I naively thought that it would be useful to describe the curviness of the trajectory as a function of the angle between each subsequent segments of the control polygon. _This_ is why I’ve been asking about these control points/polygon…

At the end of the day I’m trying to do three things in one shot: smooth the data, allow for interpolation, detect where the straight part ends and the curvy part begins.

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [October 9, 2019, 11:11am UTC](https://discourse.julialang.org/t/control-points-from-a-b-spline/29674/14 "2019-10-09T11:11:06Z")

</div>

> [@yakir12](#):
>
> What I’m truly after is some reliable measure of how curvy a section of a trajectory is.

You could calculate the second derivative of the spline approximation at various points in that section. It is easy with B-splines (but make sure you use at least cubic).

---

<div class="post-metadata">

### Author: ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)
#### Post date: [October 9, 2019, 11:25am UTC](https://discourse.julialang.org/t/control-points-from-a-b-spline/29674/15 "2019-10-09T11:25:13Z")

</div>

I test this as well. Thanks!
