Library for unit based mathematics
In my last project I faced a few tasks of page layouting in a dynamic manner. I had to code rules to position and scale elements on documents with the complication of all data being of dynamic nature. So forget using a clean sheet of paper, a pencil, a ruler and a pocket calculator. Instead a basic layout has to be filled at runtime with data from various sources, text, logos, plans, tables. Sure, not such a complex thing from a mathematical point of view: geometry defines all required tools, all computation rules to find out where to position the top left corner of a logo file which has to be scaled by what factor to fit inside a predefined area whilst still leaving some space defined elsewhere. All fine, so let’s go… However everyone who has coded such a layouting routine will agree that this quickly leads to endless monotonous computations in the code, an flowing list of numbers getting converted, added, subtracted, factored. When you want to find out afterwards why that code scales a logo slightly wider than expected things get annoying. Because all you see is endless computations, but no layouting.
So instead (ok, after a few frustrating hours) I created a simple library to be able to use higher level concepts of geometry for this. Instead of computing an endless series of numbers and trying to keep the code readable by using self-explaning variable names I now can use things like an “area” or a “distance”. This immediately results in much shorter code, since all those endless computations are not inside the layouting routines any more, but safely stored away in the tested types offered by the library. The extra topping you can always expect if I implement something is this:
The library knows about units! And how to handle them! This means: you can define an area by its dimensions, say 200cm x 3,5m and subtract a length of 567mm from its width. The resulting area does not only have the correct size, you can also ask its size in whatever units you want to, let’s say feet (though I never understood the concept of that twisted and just plain stupid american units system). So you can simply code what layout operation you want, specify the units your measurement numbers are in and things work. You get a logos dimensions in pixels, whilst your page layout is measured in mm? Fine, just place the logo and have it scaled. The library does the computation.
The current implementation lives on github. It certainly is limited, there is little sense in coding completeness without usage. But I am convinced that there are new challenges to come, be it for me or someone else. So let’s see, it would be great if more types in geometry, more fields in physics and even stuff beyond that (finance, chemisty, …) would get implemented by the time they are required. Everyone is invited to try the library, use it in open projects and contribute extensions!