Using Swift techniques for Drawing

The long discussion on is Code an Art or a Science will continue for years to come because there will always be two sides to it and I am not attempting to join or fuel that discussion. However, I feel that in my lifetime I have been lucky to get exposure to a whole lot of technologies and development techniques over the past couple of decades. With that as a benchmark, I can say that Swift makes development a breeze and also in many ways turns your code into Art.
In this article, we are looking at using Swift for Drawing, not the one with oils and canvases, but drawing lines and curves. This can grow into a better library but for now it is about how code can look better and perform better too.

What are we trying to achieve

We are trying to create code to help us create a path which we can use as we would want. Generating the path is not difficult, but you will see that it makes it easy to use. Lets look at what it would look like,

let path = Bezier()
    .moveTo(x:10, y: 10)
    .lineTo(x:10, y: 110)
    .lineTo(x:60, y: 60)
    .lineTo(x:10, y: 10)

    .closePath()
    
    .circle(x:25, y: 60, radius: 120)

Note: The syntax shown above is what is required by Swift 3.0, where you need to specify the first parameter.

this will result in a path that creates a triangle and a circle outside of it. which looks something like this.
Bezier Path

In Closing

While everyone enjoys a particular style of coding, I prefer the easy to read and simplistic approach to code. Something that can be easily modified or managed further by developers than create a complicated library that developers use but avoid touching for the fear of not understanding magic numbers and macros, etc.

It is just a matter of time when Swift will be everywhere and if you want to get future generations onto Swift, then it is on you to write simplistic code.

Views All Time
Views All Time
1492
Views Today
Views Today
1
Posted in Basics, Tip and tagged , , , , , .