# [cairo] Calculating Control Points For Bsplines

Bill Spitzak spitzak at gmail.com
Mon Dec 14 10:41:04 PST 2015

```You can usually avoid working with angles entirely, by using vector math.

For instance it is very common to make the handle slope for P1 be equal to
the slope between P0 and P2. This means the distance from P1 to the handle
ends is some multiple of (P2-P0), all done with xy pairs and no angles or
trig needed.

Making the two handles for a point equal-length and parallel is a
requirement if you want the second derivative of the curve to be
continuous. Though it is not clear whether this results in the most
attractive curve.

On Mon, Dec 14, 2015 at 1:37 AM, Bernhard Fischer <bf at abenteuerland.at>
wrote:

> 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:
>
>
>
> Best regards,
> Bernhard
>
> --
> cairo mailing list
> cairo at cairographics.org
> http://lists.cairographics.org/mailman/listinfo/cairo
>
-------------- next part --------------
An HTML attachment was scrubbed...