vec2.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /******************************************************************************
  2. Copyright (C) 2023 by Lain Bailey <lain@obsproject.com>
  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 2 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. #pragma once
  15. #include "../util/c99defs.h"
  16. #include <math.h>
  17. #ifdef __cplusplus
  18. extern "C" {
  19. #endif
  20. struct vec2 {
  21. union {
  22. struct {
  23. float x, y;
  24. };
  25. float ptr[2];
  26. };
  27. };
  28. static inline void vec2_zero(struct vec2 *dst)
  29. {
  30. dst->x = 0.0f;
  31. dst->y = 0.0f;
  32. }
  33. static inline void vec2_set(struct vec2 *dst, float x, float y)
  34. {
  35. dst->x = x;
  36. dst->y = y;
  37. }
  38. static inline void vec2_copy(struct vec2 *dst, const struct vec2 *v)
  39. {
  40. dst->x = v->x;
  41. dst->y = v->y;
  42. }
  43. static inline void vec2_add(struct vec2 *dst, const struct vec2 *v1, const struct vec2 *v2)
  44. {
  45. vec2_set(dst, v1->x + v2->x, v1->y + v2->y);
  46. }
  47. static inline void vec2_sub(struct vec2 *dst, const struct vec2 *v1, const struct vec2 *v2)
  48. {
  49. vec2_set(dst, v1->x - v2->x, v1->y - v2->y);
  50. }
  51. static inline void vec2_mul(struct vec2 *dst, const struct vec2 *v1, const struct vec2 *v2)
  52. {
  53. vec2_set(dst, v1->x * v2->x, v1->y * v2->y);
  54. }
  55. static inline void vec2_div(struct vec2 *dst, const struct vec2 *v1, const struct vec2 *v2)
  56. {
  57. vec2_set(dst, v1->x / v2->x, v1->y / v2->y);
  58. }
  59. static inline void vec2_addf(struct vec2 *dst, const struct vec2 *v, float f)
  60. {
  61. vec2_set(dst, v->x + f, v->y + f);
  62. }
  63. static inline void vec2_subf(struct vec2 *dst, const struct vec2 *v, float f)
  64. {
  65. vec2_set(dst, v->x - f, v->y - f);
  66. }
  67. static inline void vec2_mulf(struct vec2 *dst, const struct vec2 *v, float f)
  68. {
  69. vec2_set(dst, v->x * f, v->y * f);
  70. }
  71. static inline void vec2_divf(struct vec2 *dst, const struct vec2 *v, float f)
  72. {
  73. vec2_set(dst, v->x / f, v->y / f);
  74. }
  75. static inline void vec2_neg(struct vec2 *dst, const struct vec2 *v)
  76. {
  77. vec2_set(dst, -v->x, -v->y);
  78. }
  79. static inline float vec2_dot(const struct vec2 *v1, const struct vec2 *v2)
  80. {
  81. return v1->x * v2->x + v1->y * v2->y;
  82. }
  83. static inline float vec2_len(const struct vec2 *v)
  84. {
  85. return sqrtf(v->x * v->x + v->y * v->y);
  86. }
  87. static inline float vec2_dist(const struct vec2 *v1, const struct vec2 *v2)
  88. {
  89. struct vec2 temp;
  90. vec2_sub(&temp, v1, v2);
  91. return vec2_len(&temp);
  92. }
  93. static inline void vec2_minf(struct vec2 *dst, const struct vec2 *v, float val)
  94. {
  95. dst->x = (v->x < val) ? v->x : val;
  96. dst->y = (v->y < val) ? v->y : val;
  97. }
  98. static inline void vec2_min(struct vec2 *dst, const struct vec2 *v, const struct vec2 *min_v)
  99. {
  100. dst->x = (v->x < min_v->x) ? v->x : min_v->x;
  101. dst->y = (v->y < min_v->y) ? v->y : min_v->y;
  102. }
  103. static inline void vec2_maxf(struct vec2 *dst, const struct vec2 *v, float val)
  104. {
  105. dst->x = (v->x > val) ? v->x : val;
  106. dst->y = (v->y > val) ? v->y : val;
  107. }
  108. static inline void vec2_max(struct vec2 *dst, const struct vec2 *v, const struct vec2 *max_v)
  109. {
  110. dst->x = (v->x > max_v->x) ? v->x : max_v->x;
  111. dst->y = (v->y > max_v->y) ? v->y : max_v->y;
  112. }
  113. EXPORT void vec2_abs(struct vec2 *dst, const struct vec2 *v);
  114. EXPORT void vec2_floor(struct vec2 *dst, const struct vec2 *v);
  115. EXPORT void vec2_ceil(struct vec2 *dst, const struct vec2 *v);
  116. EXPORT int vec2_close(const struct vec2 *v1, const struct vec2 *v2, float epsilon);
  117. EXPORT void vec2_norm(struct vec2 *dst, const struct vec2 *v);
  118. #ifdef __cplusplus
  119. }
  120. #endif