vec2.h 3.8 KB

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