math-extra.h 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /******************************************************************************
  2. Copyright (C) 2013 by Hugh Bailey <[email protected]>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ******************************************************************************/
  14. #ifndef MATH_EXTRA_H
  15. #define MATH_EXTRA_H
  16. #include "../util/c99defs.h"
  17. /*
  18. * A few general math functions that I couldn't really decide where to put.
  19. *
  20. * Polar/Cart conversion, torque functions (for smooth movement), percentage,
  21. * random floats.
  22. */
  23. #ifdef __cplusplus
  24. extern "C" {
  25. #endif
  26. struct vec2;
  27. struct vec3;
  28. EXPORT void polar_to_cart(struct vec3 *dst, const struct vec3 *v);
  29. EXPORT void cart_to_polar(struct vec3 *dst, const struct vec3 *v);
  30. EXPORT void norm_to_polar(struct vec2 *dst, const struct vec3 *norm);
  31. EXPORT void polar_to_norm(struct vec3 *dst, const struct vec2 *polar);
  32. EXPORT float calc_torquef(float val1, float val2, float torque,
  33. float min_adjust, float t);
  34. EXPORT void calc_torque(struct vec3 *dst, const struct vec3 *v1,
  35. const struct vec3 *v2, float torque, float min_adjust,
  36. float t);
  37. static inline float get_percentage(float start, float end, float mid)
  38. {
  39. return (mid-start) / (end-start);
  40. }
  41. static inline float get_percentagei(int start, int end, int mid)
  42. {
  43. return (float)(mid-start) / (float)(end-start);
  44. }
  45. EXPORT float rand_float(int positive_only);
  46. #ifdef __cplusplus
  47. }
  48. #endif
  49. #endif