Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Y-Axis in math functions  (Read 5322 times)

0 Members and 1 Guest are viewing this topic.

Garwin

  • Jr. Member
  • **
  • Posts: 59
    • View Profile
Y-Axis in math functions
« on: July 27, 2024, 07:50:21 pm »
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?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Y-Axis in math functions
« Reply #1 on: July 28, 2024, 12:35:45 pm »
I don't know how you'd define "professional", but what I'd do is keep the graphical representation and the math parts separated and have a final render/translation layer just before showing it on screen.

This not only allows you to not care about the direction of the y-axis until the last displaying step, but also ensures that you're not mixing concerns of displaying and rendering with your math bits, and allows you to potentially represent the math bits in other ways or reuse it for other situations (e.g. writing to file, headless server, etc).

Plus your rendering layer can then also control other things as axis positioning (i.e. where's the zero point located on the window, etc.).
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Garwin

  • Jr. Member
  • **
  • Posts: 59
    • View Profile
Re: Y-Axis in math functions
« Reply #2 on: July 29, 2024, 09:36:00 pm »
...

Thanks, you are completely right, and practically, it needs minimal changes of code.