1
0

vec2.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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,
  64. float f)
  65. {
  66. vec2_set(dst, v->x+f, v->y+f);
  67. }
  68. static inline void vec2_subf(struct vec2 *dst, const struct vec2 *v,
  69. float f)
  70. {
  71. vec2_set(dst, v->x-f, v->y-f);
  72. }
  73. static inline void vec2_mulf(struct vec2 *dst, const struct vec2 *v,
  74. float f)
  75. {
  76. vec2_set(dst, v->x*f, v->y*f);
  77. }
  78. static inline void vec2_divf(struct vec2 *dst, const struct vec2 *v,
  79. float f)
  80. {
  81. vec2_set(dst, v->x/f, v->y/f);
  82. }
  83. static inline void vec2_neg(struct vec2 *dst, const struct vec2 *v)
  84. {
  85. vec2_set(dst, -v->x, -v->y);
  86. }
  87. static inline float vec2_dot(const struct vec2 *v1, const struct vec2 *v2)
  88. {
  89. return v1->x*v2->x + v1->y*v2->y;
  90. }
  91. static inline float vec2_len(const struct vec2 *v)
  92. {
  93. return sqrtf(v->x*v->x + v->y*v->y);
  94. }
  95. static inline float vec2_dist(const struct vec2 *v1, const struct vec2 *v2)
  96. {
  97. struct vec2 temp;
  98. vec2_sub(&temp, v1, v2);
  99. return vec2_len(&temp);
  100. }
  101. static inline void vec2_minf(struct vec2 *dst, const struct vec2 *v,
  102. float val)
  103. {
  104. if (v->x < val)
  105. dst->x = val;
  106. if (v->y < val)
  107. dst->y = val;
  108. }
  109. static inline void vec2_min(struct vec2 *dst, const struct vec2 *v,
  110. const struct vec2 *min_v)
  111. {
  112. if (v->x < min_v->x)
  113. dst->x = min_v->x;
  114. if (v->y < min_v->y)
  115. dst->y = min_v->y;
  116. }
  117. static inline void vec2_maxf(struct vec2 *dst, const struct vec2 *v,
  118. float val)
  119. {
  120. if (v->x > val)
  121. dst->x = val;
  122. if (v->y > val)
  123. dst->y = val;
  124. }
  125. static inline void vec2_max(struct vec2 *dst, const struct vec2 *v,
  126. const struct vec2 *max_v)
  127. {
  128. if (v->x > max_v->x)
  129. dst->x = max_v->x;
  130. if (v->y > max_v->y)
  131. dst->y = max_v->y;
  132. }
  133. EXPORT void vec2_abs(struct vec2 *dst, const struct vec2 *v);
  134. EXPORT void vec2_floor(struct vec2 *dst, const struct vec2 *v);
  135. EXPORT void vec2_ceil(struct vec2 *dst, const struct vec2 *v);
  136. EXPORT int vec2_close(const struct vec2 *v1, const struct vec2 *v2,
  137. float epsilon);
  138. EXPORT void vec2_norm(struct vec2 *dst, const struct vec2 *v);
  139. #ifdef __cplusplus
  140. }
  141. #endif