vec2.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 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,
  44. const struct vec2 *v2)
  45. {
  46. vec2_set(dst, v1->x + v2->x, v1->y + v2->y);
  47. }
  48. static inline void vec2_sub(struct vec2 *dst, const struct vec2 *v1,
  49. const struct vec2 *v2)
  50. {
  51. vec2_set(dst, v1->x - v2->x, v1->y - v2->y);
  52. }
  53. static inline void vec2_mul(struct vec2 *dst, const struct vec2 *v1,
  54. const struct vec2 *v2)
  55. {
  56. vec2_set(dst, v1->x * v2->x, v1->y * v2->y);
  57. }
  58. static inline void vec2_div(struct vec2 *dst, const struct vec2 *v1,
  59. const struct vec2 *v2)
  60. {
  61. vec2_set(dst, v1->x / v2->x, v1->y / v2->y);
  62. }
  63. static inline void vec2_addf(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_subf(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_mulf(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_divf(struct vec2 *dst, const struct vec2 *v, float f)
  76. {
  77. vec2_set(dst, v->x / f, v->y / f);
  78. }
  79. static inline void vec2_neg(struct vec2 *dst, const struct vec2 *v)
  80. {
  81. vec2_set(dst, -v->x, -v->y);
  82. }
  83. static inline float vec2_dot(const struct vec2 *v1, const struct vec2 *v2)
  84. {
  85. return v1->x * v2->x + v1->y * v2->y;
  86. }
  87. static inline float vec2_len(const struct vec2 *v)
  88. {
  89. return sqrtf(v->x * v->x + v->y * v->y);
  90. }
  91. static inline float vec2_dist(const struct vec2 *v1, const struct vec2 *v2)
  92. {
  93. struct vec2 temp;
  94. vec2_sub(&temp, v1, v2);
  95. return vec2_len(&temp);
  96. }
  97. static inline void vec2_minf(struct vec2 *dst, const struct vec2 *v, float val)
  98. {
  99. dst->x = (v->x < val) ? v->x : val;
  100. dst->y = (v->y < val) ? v->y : val;
  101. }
  102. static inline void vec2_min(struct vec2 *dst, const struct vec2 *v,
  103. const struct vec2 *min_v)
  104. {
  105. dst->x = (v->x < min_v->x) ? v->x : min_v->x;
  106. dst->y = (v->y < min_v->y) ? v->y : min_v->y;
  107. }
  108. static inline void vec2_maxf(struct vec2 *dst, const struct vec2 *v, float val)
  109. {
  110. dst->x = (v->x > val) ? v->x : val;
  111. dst->y = (v->y > val) ? v->y : val;
  112. }
  113. static inline void vec2_max(struct vec2 *dst, const struct vec2 *v,
  114. const struct vec2 *max_v)
  115. {
  116. dst->x = (v->x > max_v->x) ? v->x : max_v->x;
  117. dst->y = (v->y > max_v->y) ? v->y : max_v->y;
  118. }
  119. EXPORT void vec2_abs(struct vec2 *dst, const struct vec2 *v);
  120. EXPORT void vec2_floor(struct vec2 *dst, const struct vec2 *v);
  121. EXPORT void vec2_ceil(struct vec2 *dst, const struct vec2 *v);
  122. EXPORT int vec2_close(const struct vec2 *v1, const struct vec2 *v2,
  123. float epsilon);
  124. EXPORT void vec2_norm(struct vec2 *dst, const struct vec2 *v);
  125. #ifdef __cplusplus
  126. }
  127. #endif