Learning from my Mistakes

Design Notes Diary

So my article last week about converting angles into the SwiftUI unit points had a bug in it. I was doing a linear interpolation between the corners rather than a trigonometric one which meant that my result would slightly fall off the true intersection point.

Here is the animation that Rob Mayoff made to illustrate this error:

Several readers wrote in with a correction to my work observing this deviation. This presented a really interesting opportunity to see how different minds find the solution to the same problem.

As a brief aside I wanted to commend my readers about how friendly, kind and respectful you all are. All the corrections I received were very friendly and not at all condescending. I very much appreciate that. You all rock!

So here is the fun part, let’s dive into the diversity in solutions.


DrewFitz posted a solution that looks like this:

The standard cos and sin computation to find the x and y component along the circle inside the square, then scaled outwards to meet the square.


robb posted a solution that looks like this:

Very similar in concept but rather than building a common scale factor and applying that to the output regardless, they instead make the different quadrants explicit with an if/then statement.

Rob Mayoff

Rob Mayoff posted a solution that looks like this:

Rob’s whole solution is really worth a read, there is some super clever SwiftUI stuff used to create the “proof” demonstration.

From using a Swift Task to inside of a stride loop, to a very elegant use of the Grid control for the layout. Seriously, load it up in Xcode, and I’m confident you’ll learn something from it. I know I did.

Anyway, onto his solution. I find Rob’s solution a bit harder to follow, but helpfully it includes two links to explanations. It is doing the generally same thing as the other solutions but in a more concise syntax. If I’m being honest this is the kind of code I get very intimidated by when I read it. It looks like deep magic; powerful, but less accessible.

I really like how it extends UnitPoint to make this a “built-in” SwiftUI option. Honestly, having seen that I really hope that iOS 17 adds this as an option (FB12011738).


As funny as it might sound I am really happy that I made this mistake. It created a really helpful learning opportunity for me to see how different minds would solve the same problem. I have learned a ton from reading through these varied solutions to a problem I was wrestling with.

Code isn’t precious, it can always be improved.

The reality of being an indie developer is that I very rarely get the benefit of other people’s approaches to problems I’m working on. This is certainly one of the biggest drawbacks of my situation. So when I do get the opportunity to learn from other developers I jump at it.

Thank you all for your feedback!

Bonus Learning

SSteve posted this comment on the way that I was normalizing the angle in my original computation.

My approach is “correct” in this case but SSTeve’s is certainly more elegant and concise. I’d never really thought about how the modulus operator would work for negative numbers. But their explanation makes a ton of sense.

Yet more learning!

David Smith