Better Programming

Advice for programmers.

Follow publication

Member-only story

Using De Casteljau’s Algorithm/Bezier Curves in SwiftU

Mark Lucking
Better Programming
Published in
4 min readNov 18, 2020

--

Animation with Bezier curves
A collection of text boxes animated across three Bezier curves with foobar controls

I wrote an article the other day about using Bezier curves, the construction of which is built into Swift primitives. It’s super useful and works really well. Within that article, I mentioned in passing that the math behind it was mind-boggling. I was wrong. The math is simple. It’s the code that mind-boggling. Let’s dig deeper.

But wait, just before we jump in, why would you want to go down this rabbit hole? Here are three possible scenarios:

  • The client wants you to plot some text on a Bezier curve.
  • You want to use a set of Bezier coordinates for a game.
  • You’re concerned with making the app run faster, and calculating fewer points for a Bezier curve would make sense.

But if you wanted to do any and/or all of these, you’d be stuck because the Apple primitives don’t return coordinates. They return a path. You need to dig deeper. Let’s try to do the math and the code.

As I mentioned last time, the man of the moment is De Casteljau, who wrote the algorithm to do what mathematicians call linear interpolation between two values. The algorithm calculates the coordinates needed to draw…

--

--

Mark Lucking
Mark Lucking

Written by Mark Lucking

Coding for 35+ years, enjoying using and learning Swift/iOS development. Writer @ Better Programming, @The StartUp, @Mac O’Clock, Level Up Coding & More

Write a response