liba 0.1.15
An algorithm library based on C/C++
Loading...
Searching...
No Matches
vector2.h
Go to the documentation of this file.
1
5
6#ifndef LIBA_VECTOR2_H
7#define LIBA_VECTOR2_H
8
9#include "a.h"
10#include "math.h"
11
17
18/* clang-format off */
20#define A_VECTOR2_C(x, y) {a_real_c(x), a_real_c(y)}
21/* clang-format on */
22
24#define a_vector2_c(x) a_cast_s(a_vector2, x)
25#define a_vector2_(_, x) a_cast_s(a_vector2 _, x)
26
27typedef struct a_vector2 a_vector2;
28
29#if defined(__cplusplus)
30namespace a
31{
32typedef struct a_vector2 vector2;
33} /* namespace a */
34extern "C" {
35#endif /* __cplusplus */
36#if !defined A_HAVE_INLINE || defined(LIBA_VECTOR2_C)
37#undef A_INTERN
38#define A_INTERN A_PUBLIC extern
39#endif /* A_HAVE_INLINE */
40
41A_INTERN a_real a_vector2_set_dir(a_vector2 *ctx, a_real x, a_real y);
42A_INTERN void a_vector2_val(a_vector2 const *ctx, a_real *x, a_real *y);
43A_INTERN void a_vector2_set_val(a_vector2 *ctx, a_real x, a_real y);
44A_INTERN void a_vector2_pol(a_vector2 const *ctx, a_real *rho, a_real *theta);
45A_INTERN void a_vector2_set_pol(a_vector2 *ctx, a_real rho, a_real theta);
46
47A_INTERN void a_vector2_add(a_vector2 const *lhs, a_vector2 const *rhs, a_vector2 *res);
48A_INTERN void a_vector2_sub(a_vector2 const *lhs, a_vector2 const *rhs, a_vector2 *res);
49A_INTERN void a_vector2_mul(a_vector2 const *lhs, a_real rhs, a_vector2 *res);
50A_INTERN void a_vector2_div(a_vector2 const *lhs, a_real rhs, a_vector2 *res);
51A_INTERN void a_vector2_neg(a_vector2 const *ctx, a_vector2 *res);
52
53A_EXTERN a_real a_vector2_unit(a_vector2 *ctx);
54A_EXTERN a_real a_vector2_norm(a_vector2 const *ctx);
55A_EXTERN a_real a_vector2_norm1(a_vector2 const *ctx);
56A_EXTERN a_real a_vector2_norm2(a_vector2 const *ctx);
57A_EXTERN a_real a_vector2_dot(a_vector2 const *lhs, a_vector2 const *rhs);
58A_EXTERN a_real a_vector2_dist(a_vector2 const *lhs, a_vector2 const *rhs);
59A_EXTERN a_real a_vector2_dist1(a_vector2 const *lhs, a_vector2 const *rhs);
60A_EXTERN a_real a_vector2_dist2(a_vector2 const *lhs, a_vector2 const *rhs);
61A_EXTERN a_real a_vector2_angle(a_vector2 const *lhs, a_vector2 const *rhs);
62A_EXTERN a_bool a_vector2_isver(a_vector2 const *lhs, a_vector2 const *rhs);
63A_EXTERN a_bool a_vector2_ispar(a_vector2 const *lhs, a_vector2 const *rhs);
64A_EXTERN a_real a_vector2_cross(a_vector2 const *lhs, a_vector2 const *rhs);
65A_EXTERN void a_vector2_outer(a_vector2 const *lhs, a_vector2 const *rhs, a_real res[4]);
66
67A_EXTERN void a_vector2_rot_(a_vector2 const *ctx, a_real sin, a_real cos, a_vector2 *res);
68A_EXTERN void a_vector2_rot(a_vector2 const *ctx, a_real angle, a_vector2 *res);
69
70#if !defined A_HAVE_INLINE || defined(LIBA_VECTOR2_C)
71#undef A_INTERN
72#define A_INTERN static A_INLINE
73#endif /* A_HAVE_INLINE */
74#if defined(__cplusplus)
75} /* extern "C" */
76#endif /* __cplusplus */
77
78struct a_point2;
83{
84 a_real x, y;
85#if defined(__cplusplus)
86 A_INLINE void set(a_point2 const &p, a_point2 const &q);
87 A_INLINE a_real set_dir(a_real x_, a_real y_)
88 {
89 return a_vector2_set_dir(this, x_, y_);
90 }
91 A_INLINE void val(a_real &x_, a_real &y_) const
92 {
93 a_vector2_val(this, &x_, &y_);
94 }
95 A_INLINE void set_val(a_real x_, a_real y_)
96 {
97 a_vector2_set_val(this, x_, y_);
98 }
99 A_INLINE void pol(a_real &rho, a_real &theta) const
100 {
101 a_vector2_pol(this, &rho, &theta);
102 }
103 A_INLINE void set_pol(a_real rho, a_real theta)
104 {
105 a_vector2_set_pol(this, rho, theta);
106 }
107 A_INLINE void add(a_vector2 const &rhs, a_vector2 &res) const
108 {
109 a_vector2_add(this, &rhs, &res);
110 }
111 A_INLINE void sub(a_vector2 const &rhs, a_vector2 &res) const
112 {
113 a_vector2_sub(this, &rhs, &res);
114 }
115 A_INLINE void mul(a_real rhs, a_vector2 &res) const
116 {
117 a_vector2_mul(this, rhs, &res);
118 }
119 A_INLINE void div(a_real rhs, a_vector2 &res) const
120 {
121 a_vector2_div(this, rhs, &res);
122 }
123 A_INLINE void neg(a_vector2 &res) const
124 {
125 a_vector2_neg(this, &res);
126 }
127 A_INLINE a_real unit() { return a_vector2_unit(this); }
128 A_INLINE a_real norm() const { return a_vector2_norm(this); }
129 A_INLINE a_real norm1() const { return a_vector2_norm1(this); }
130 A_INLINE a_real norm2() const { return a_vector2_norm2(this); }
131 A_INLINE a_real dot(a_vector2 const &rhs) const { return a_vector2_dot(this, &rhs); }
132 A_INLINE a_real dist(a_vector2 const &rhs) const { return a_vector2_dist(this, &rhs); }
133 A_INLINE a_real dist1(a_vector2 const &rhs) const { return a_vector2_dist1(this, &rhs); }
134 A_INLINE a_real dist2(a_vector2 const &rhs) const { return a_vector2_dist2(this, &rhs); }
135 A_INLINE a_real angle(a_vector2 const &rhs) const { return a_vector2_angle(this, &rhs); }
136 A_INLINE a_bool isver(a_vector2 const &rhs) const { return a_vector2_isver(this, &rhs); }
137 A_INLINE a_bool ispar(a_vector2 const &rhs) const { return a_vector2_ispar(this, &rhs); }
138 A_INLINE a_real cross(a_vector2 const &rhs) const { return a_vector2_cross(this, &rhs); }
139 A_INLINE void outer(a_vector2 const &rhs, a_real res[4]) const { a_vector2_outer(this, &rhs, res); }
140 A_INLINE void rot(a_real angle, a_vector2 &res) const { a_vector2_rot(this, angle, &res); }
141 friend A_INLINE a_real operator^(a_vector2 const &lhs, a_vector2 const &rhs)
142 {
143 return a_vector2_cross(&lhs, &rhs);
144 }
145 friend A_INLINE void operator+=(a_vector2 &lhs, a_vector2 const &rhs) { a_vector2_add(&lhs, &rhs, &lhs); }
146 friend A_INLINE a_vector2 operator+(a_vector2 const &lhs, a_vector2 const &rhs)
147 {
148 a_vector2 res;
149 a_vector2_add(&lhs, &rhs, &res);
150 return res;
151 }
152 friend A_INLINE void operator-=(a_vector2 &lhs, a_vector2 const &rhs) { a_vector2_sub(&lhs, &rhs, &lhs); }
153 friend A_INLINE a_vector2 operator-(a_vector2 const &lhs, a_vector2 const &rhs)
154 {
155 a_vector2 res;
156 a_vector2_sub(&lhs, &rhs, &res);
157 return res;
158 }
159 friend A_INLINE void operator*=(a_vector2 &lhs, a_real rhs) { a_vector2_mul(&lhs, rhs, &lhs); }
160 friend A_INLINE a_real operator*(a_vector2 const &lhs, a_vector2 const &rhs)
161 {
162 return a_vector2_dot(&lhs, &rhs);
163 }
164 friend A_INLINE a_vector2 operator*(a_real lhs, a_vector2 const &rhs)
165 {
166 a_vector2 res;
167 a_vector2_mul(&rhs, lhs, &res);
168 return res;
169 }
170 friend A_INLINE a_vector2 operator*(a_vector2 const &lhs, a_real rhs)
171 {
172 a_vector2 res;
173 a_vector2_mul(&lhs, rhs, &res);
174 return res;
175 }
176 friend A_INLINE void operator/=(a_vector2 &lhs, a_real rhs) { a_vector2_div(&lhs, rhs, &lhs); }
177 friend A_INLINE a_vector2 operator/(a_vector2 const &lhs, a_real rhs)
178 {
179 a_vector2 res;
180 a_vector2_div(&lhs, rhs, &res);
181 return res;
182 }
183 friend A_INLINE a_vector2 operator+(a_vector2 const &rhs) { return rhs; }
184 friend A_INLINE a_vector2 operator-(a_vector2 const &rhs)
185 {
186 a_vector2 res;
187 a_vector2_neg(&rhs, &res);
188 return res;
189 }
190#endif /* __cplusplus */
191};
192
193#if defined(LIBA_VECTOR2_C)
194#undef A_INTERN
195#define A_INTERN A_INLINE
196#endif /* LIBA_VECTOR2_C */
197#if defined(A_HAVE_INLINE) || defined(LIBA_VECTOR2_C)
198
199A_INLINE a_real a_vector2_set_dir(a_vector2 *ctx, a_real x, a_real y)
200{
201 ctx->x = x;
202 ctx->y = y;
203 return a_vector2_unit(ctx);
204}
205A_INTERN void a_vector2_val(a_vector2 const *ctx, a_real *x, a_real *y)
206{
207 *x = ctx->x;
208 *y = ctx->y;
209}
210A_INTERN void a_vector2_set_val(a_vector2 *ctx, a_real x, a_real y)
211{
212 ctx->x = x;
213 ctx->y = y;
214}
215A_INTERN void a_vector2_pol(a_vector2 const *ctx, a_real *rho, a_real *theta)
216{
217 a_real_cart2pol(ctx->x, ctx->y, rho, theta);
218}
219A_INTERN void a_vector2_set_pol(a_vector2 *ctx, a_real rho, a_real theta)
220{
221 a_real_pol2cart(rho, theta, &ctx->x, &ctx->y);
222}
223A_INTERN void a_vector2_add(a_vector2 const *lhs, a_vector2 const *rhs, a_vector2 *res)
224{
225 res->x = lhs->x + rhs->x;
226 res->y = lhs->y + rhs->y;
227}
228A_INTERN void a_vector2_sub(a_vector2 const *lhs, a_vector2 const *rhs, a_vector2 *res)
229{
230 res->x = lhs->x - rhs->x;
231 res->y = lhs->y - rhs->y;
232}
233A_INTERN void a_vector2_mul(a_vector2 const *lhs, a_real rhs, a_vector2 *res)
234{
235 res->x = lhs->x * rhs;
236 res->y = lhs->y * rhs;
237}
238A_INTERN void a_vector2_div(a_vector2 const *lhs, a_real rhs, a_vector2 *res)
239{
240 res->x = lhs->x / rhs;
241 res->y = lhs->y / rhs;
242}
243A_INTERN void a_vector2_neg(a_vector2 const *ctx, a_vector2 *res)
244{
245 res->x = -ctx->x;
246 res->y = -ctx->y;
247}
248
249#endif /* A_HAVE_INLINE */
250#if defined(LIBA_VECTOR2_C)
251#undef A_INTERN
252#define A_INTERN static A_INLINE
253#endif /* LIBA_VECTOR2_C */
254
256
257#endif /* a/vector2.h */
algorithm library
double a_real
compiler built-in floating-point number type
Definition a.h:1012
bool a_bool
type, capable of holding one of the two values: 1 and 0
Definition a.h:320
mathematical algorithm library
instance structure for two-dimensional point
Definition point2.h:75
instance structure for two-dimensional vector
Definition vector2.h:83