# [cairo] Calculating Control Points For Bsplines

Bernhard Fischer bf at abenteuerland.at
Mon Dec 14 01:37:52 PST 2015

```On Saturday 12 December 2015 22:46:17 Lawrence D'Oliveiro wrote:
> On Sat, 12 Dec 2015 08:25:30 +0100, Bernhard Fischer wrote:
> > I wrote an article about calculating control points for Bsplines
> > (based on polygons) and a sample program.
>
> Always interested to see how people use geometrical calculations
> together with Cairo. Just some points:
>
> I had to deal with the same issue of calculating half the
> difference between two angles in this
> <https://github.com/ldo/qahirah_examples/blob/master/pattern_dash>
> piece of example code (output here
> <http://default-cube.deviantart.com/art/Pattern-Dash-576369003>),
> while avoiding discontinuities around 0°. The calculation I came up with
> was basically
>
>     ((angle2 - angle1 + π) mod 2π - π) ÷ 2

That's why I do not divide the angles like this (I did that in an earlier
variant). I calculate the end points of an intermediate line and then
determine the angle of this. It eliminates the need for handling of special
cases.

> Also, C, like most IEEE-754-compliant languages, has a built-in function
> for calculating Euclidean distance. Instead of
>
>     s = sqrt(pow(a, 2) + pow(b, 2))
>
> why not just write
>
>     s = hypot(a, b)

Thanks, I know that function but forgot about it. I'll change it.

> Another idea is, when averaging the angles at the corners, why not
> weight the contributions from the adjacent line segments according to
> their relative lengths? So shorter segments will produce sharper curves.

Well, there is no perfect solution. I implemented some variants and every
variant creates a specific appearance of the curve. I chose the one as
implemented in my demo program to fit best to my needs.
I use this variant in my chart renderer Smrender. The following example have
the coastline and the depth contour lines rendered with this curve style: