I have a codebase that deals with angles quite a bit, but sometimes the input comes in different angle formats. That is, some formats are degrees clockwise, some are degrees counter-clockwise, some are 0 to 360, some are -180 to 180, some are radians…
So far I have succeeded in keeping them all the same internally, but it’s always a worry, and the bugs that arise can be hard to track down.
I would like to create an angle class that keeps track of the units and direction as well as the magnitude so that I can deal with them in a uniform way without so much worries.
My googling has turned up Martin Fowler’s Value Object, but i’m having trouble understanding his language without a simple example to reference. I’ve also found a Java API that seems relevant, but it doesn’t look like a simple example either…
Can anyone point me towards a simple example of a class that incorporates units?
I would approach this by storing values internally in a single format (say, positive radians) and provide static factory methods to construct angles from various input formats. For example:
Then you could use it like this: