Leonardo is a Swift Library that makes working with geometric ratios, such as the golden ratio, easy and intuitive. This can be useful in UI and numerics computation.
This library is inspired by another library I wrote a while back called leonardo-python, which provides similiar functionality with syntactic sugar for the python language.
To use the library, clone the project into your workspace. It is not yet available as its API cannot be considered stable. Once sufficiently stable, I will consider to package it for use with the Swift Package Manager from Apple, and possibly other package managers.
There are various ways to use Leonardo to generate aesthetic parameters for your UI. For example, with the ContinuedRatio
type and its segment()
number extension, you can split a quantity into segments such that subsequent segments relate to another following a particular ratio (such as the golden ratio).
import Leonardo
let ratio = ContinuedRatio<Double>(.gold, numberOfTerms: 3)
let segments = 100.segmented(by: ratio)
// segments == [50.0, 30.901699437494745, 19.09830056250526]
You find this example on the playground page Continued Ratios
.
Defining your own ratios is easy!
let doubling: Ratio = 2
The following definitions are useful in contexts that benefit from terse syntax, like graphics programming:
let g = Ratio.gold
let s = Ratio.silver
let b = Ratio.bronce
g[2].quotient // 2.618033988749895
To see some more elaborate and interactive examples, check out the playgrounds provided with this package under /Examples.playground
.
Feel free to fork this repository and build some cool extensions. I'd appreciate to see them, just send a pull request to discuss if it is compatible with my project vision. Similarly, creating an issue when you feel something is missing or not working as expected, is appreciated so I can get back behind the keyboard and fix it for you.