SFML has a Y-axis oriented down but other uses (OPEN GL, graphs visualization, usual math representation) have Y-axis up. How to solve that in math functions that give completely different results based on the orientation of the Y-axis.
The main functions that give different results based on orientation are isClockwise, isCounterClockwise, objectOrientation and any sorting of points based on clockwise order.
I have thought about it but I would like to know how is it solved in professional programming. My thoughts of possibilities.
1. Macro
Define macro for AXIS_Y to be +1 or -1 depending on orientation.
Advantages: Easy, efficient (compile time)
Disadvantages: Ugly, the whole application needs to use the same orientation of the Y-axis
2. Function name
Define a pair of functions in each case.
Advantages: Efficient (compile time)
Disadvantages: Ugly for naming, duplicate code.
3. Function parameter
Each function has an additional last parameter with orientation of Y-Axis and it can even be defaulted in function and even the default can be set by macro.
Advantages: Easy, easy to maintain.
Disadvantages: Less efficient (runtime)
note: default changed by macro seems like a nest for potential bugs in case of change, the default parameter of a function can be an easy way for bugs to forget to pass the orientation of the Y-axis.
4. Functor
Make a helper implementation class with an overloaded operator (), a static member of the orientation of the axis with a static function to change orientation to up or down. Then make a static variable of the class that is used to call a functor.
Advantages: Work as a standard function, Axis orientation is preserved between calls but can be changed calling the static member function
Disadvantages: Less efficient (runtime)
5. Template
Make a template with the type to choose axis orientation. Then there can be constexpr branches.
Advantages: Efficient (compile time)
Disadvantages: not user-friendly, code will be not so obvious
So questions are:
- How is it solved in professional? (I am just a hobbyist)
- Are there any other better possibilities?