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)
38#undef A_INTERN
39#define A_INTERN A_PUBLIC extern
41#endif /* A_HAVE_INLINE */
42
51A_INTERN void a_vector2_val(a_vector2 const *ctx, a_real *x, a_real *y);
55A_INTERN void a_vector2_set_val(a_vector2 *ctx, a_real x, a_real y);
59A_INTERN void a_vector2_pol(a_vector2 const *ctx, a_real *rho, a_real *theta);
63A_INTERN void a_vector2_set_pol(a_vector2 *ctx, a_real rho, a_real theta);
64
68A_INTERN void a_vector2_add(a_vector2 const *lhs, a_vector2 const *rhs, a_vector2 *res);
72A_INTERN void a_vector2_sub(a_vector2 const *lhs, a_vector2 const *rhs, a_vector2 *res);
76A_INTERN void a_vector2_mul(a_vector2 const *lhs, a_real rhs, a_vector2 *res);
80A_INTERN void a_vector2_div(a_vector2 const *lhs, a_real rhs, a_vector2 *res);
84A_INTERN void a_vector2_neg(a_vector2 const *ctx, a_vector2 *res);
85
102A_EXTERN a_real a_vector2_norm(a_vector2 const *ctx);
113A_EXTERN a_real a_vector2_norm2(a_vector2 const *ctx);
127A_EXTERN a_real a_vector2_dot(a_vector2 const *lhs, a_vector2 const *rhs);
141A_EXTERN a_real a_vector2_dist(a_vector2 const *lhs, a_vector2 const *rhs);
155A_EXTERN a_real a_vector2_dist2(a_vector2 const *lhs, a_vector2 const *rhs);
171A_EXTERN a_real a_vector2_angle(a_vector2 const *lhs, a_vector2 const *rhs);
179A_EXTERN a_bool a_vector2_isver(a_vector2 const *lhs, a_vector2 const *rhs);
187A_EXTERN a_bool a_vector2_ispar(a_vector2 const *lhs, a_vector2 const *rhs);
202A_EXTERN a_real a_vector2_cross(a_vector2 const *lhs, a_vector2 const *rhs);
219A_EXTERN void a_vector2_outer(a_vector2 const *lhs, a_vector2 const *rhs, a_real res[4]);
220
236A_EXTERN int a_vector2_proj(a_vector2 const *ctx, a_vector2 const *dir, a_vector2 *res);
252A_EXTERN int a_vector2_perp(a_vector2 const *ctx, a_vector2 const *dir, a_vector2 *res);
268A_EXTERN int a_vector2_refl(a_vector2 const *ctx, a_vector2 const *dir, a_vector2 *res);
269
284A_EXTERN void a_vector2_rot_(a_vector2 const *ctx, a_real sin, a_real cos, a_vector2 *res);
298A_EXTERN void a_vector2_rot(a_vector2 const *ctx, a_real angle, a_vector2 *res);
299
300#if !defined A_HAVE_INLINE || defined(LIBA_VECTOR2_C)
302#undef A_INTERN
303#define A_INTERN static A_INLINE
305#endif /* A_HAVE_INLINE */
306#if defined(__cplusplus)
307} /* extern "C" */
308#endif /* __cplusplus */
309
310struct a_point2;
315{
316 a_real x, y;
317#if defined(__cplusplus)
319 A_INLINE void set(a_point2 const &p, a_point2 const &q);
321 A_INLINE a_real set_dir(a_real x_, a_real y_)
322 {
323 return a_vector2_set_dir(this, x_, y_);
324 }
325
326 A_INLINE void val(a_real &x_, a_real &y_) const
327 {
328 a_vector2_val(this, &x_, &y_);
329 }
330
331 A_INLINE void set_val(a_real x_, a_real y_)
332 {
333 a_vector2_set_val(this, x_, y_);
334 }
335
336 A_INLINE void pol(a_real &rho, a_real &theta) const
337 {
338 a_vector2_pol(this, &rho, &theta);
339 }
340
341 A_INLINE void set_pol(a_real rho, a_real theta)
342 {
343 a_vector2_set_pol(this, rho, theta);
344 }
345
346 A_INLINE void add(a_vector2 const &rhs, a_vector2 &res) const
347 {
348 a_vector2_add(this, &rhs, &res);
349 }
350
351 A_INLINE void sub(a_vector2 const &rhs, a_vector2 &res) const
352 {
353 a_vector2_sub(this, &rhs, &res);
354 }
355
356 A_INLINE void mul(a_real rhs, a_vector2 &res) const
357 {
358 a_vector2_mul(this, rhs, &res);
359 }
360
361 A_INLINE void div(a_real rhs, a_vector2 &res) const
362 {
363 a_vector2_div(this, rhs, &res);
364 }
365
366 A_INLINE void neg(a_vector2 &res) const
367 {
368 a_vector2_neg(this, &res);
369 }
370
371 A_INLINE a_real unit() { return a_vector2_unit(this); }
373 A_INLINE a_real norm() const { return a_vector2_norm(this); }
375 A_INLINE a_real norm2() const { return a_vector2_norm2(this); }
377 A_INLINE a_real dot(a_vector2 const &rhs) const { return a_vector2_dot(this, &rhs); }
379 A_INLINE a_real dist(a_vector2 const &rhs) const { return a_vector2_dist(this, &rhs); }
381 A_INLINE a_real dist2(a_vector2 const &rhs) const { return a_vector2_dist2(this, &rhs); }
383 A_INLINE a_real angle(a_vector2 const &rhs) const { return a_vector2_angle(this, &rhs); }
385 A_INLINE a_bool isver(a_vector2 const &rhs) const { return a_vector2_isver(this, &rhs); }
387 A_INLINE a_bool ispar(a_vector2 const &rhs) const { return a_vector2_ispar(this, &rhs); }
389 A_INLINE a_real cross(a_vector2 const &rhs) const { return a_vector2_cross(this, &rhs); }
391 A_INLINE void outer(a_vector2 const &rhs, a_real res[4]) const { a_vector2_outer(this, &rhs, res); }
393 A_INLINE int proj(a_vector2 const &dir, a_vector2 &res) const
394 {
395 return a_vector2_proj(this, &dir, &res);
396 }
397
398 A_INLINE int perp(a_vector2 const &dir, a_vector2 &res) const
399 {
400 return a_vector2_perp(this, &dir, &res);
401 }
402
403 A_INLINE int refl(a_vector2 const &dir, a_vector2 &res) const
404 {
405 return a_vector2_refl(this, &dir, &res);
406 }
407
408 A_INLINE void rot(a_real angle, a_vector2 &res) const { a_vector2_rot(this, angle, &res); }
410 A_INLINE void rot(a_real sin, a_real cos, a_vector2 &res) const { a_vector2_rot_(this, sin, cos, &res); }
412 friend A_INLINE a_real operator^(a_vector2 const &lhs, a_vector2 const &rhs)
413 {
414 return a_vector2_cross(&lhs, &rhs);
415 }
416
417 friend A_INLINE void operator+=(a_vector2 &lhs, a_vector2 const &rhs) { a_vector2_add(&lhs, &rhs, &lhs); }
419 friend A_INLINE a_vector2 operator+(a_vector2 const &lhs, a_vector2 const &rhs)
420 {
421 a_vector2 res;
422 a_vector2_add(&lhs, &rhs, &res);
423 return res;
424 }
425
426 friend A_INLINE void operator-=(a_vector2 &lhs, a_vector2 const &rhs) { a_vector2_sub(&lhs, &rhs, &lhs); }
428 friend A_INLINE a_vector2 operator-(a_vector2 const &lhs, a_vector2 const &rhs)
429 {
430 a_vector2 res;
431 a_vector2_sub(&lhs, &rhs, &res);
432 return res;
433 }
434
435 friend A_INLINE a_real operator*(a_vector2 const &lhs, a_vector2 const &rhs)
436 {
437 return a_vector2_dot(&lhs, &rhs);
438 }
439
440 friend A_INLINE void operator*=(a_vector2 &lhs, a_real rhs) { a_vector2_mul(&lhs, rhs, &lhs); }
442 friend A_INLINE a_vector2 operator*(a_real lhs, a_vector2 const &rhs)
443 {
444 a_vector2 res;
445 a_vector2_mul(&rhs, lhs, &res);
446 return res;
447 }
448
449 friend A_INLINE a_vector2 operator*(a_vector2 const &lhs, a_real rhs)
450 {
451 a_vector2 res;
452 a_vector2_mul(&lhs, rhs, &res);
453 return res;
454 }
455
456 friend A_INLINE void operator/=(a_vector2 &lhs, a_real rhs) { a_vector2_div(&lhs, rhs, &lhs); }
458 friend A_INLINE a_vector2 operator/(a_vector2 const &lhs, a_real rhs)
459 {
460 a_vector2 res;
461 a_vector2_div(&lhs, rhs, &res);
462 return res;
463 }
464 friend A_INLINE a_vector2 operator+(a_vector2 const &rhs) { return rhs; }
466 friend A_INLINE a_vector2 operator-(a_vector2 const &rhs)
467 {
468 a_vector2 res;
469 a_vector2_neg(&rhs, &res);
470 return res;
471 }
472#endif /* __cplusplus */
473};
474
475#if defined(LIBA_VECTOR2_C)
477#undef A_INTERN
478#define A_INTERN A_INLINE
480#endif /* LIBA_VECTOR2_C */
481#if defined(A_HAVE_INLINE) || defined(LIBA_VECTOR2_C)
482
484{
485 ctx->x = x;
486 ctx->y = y;
487 return a_vector2_unit(ctx);
488}
489A_INTERN void a_vector2_val(a_vector2 const *ctx, a_real *x, a_real *y)
490{
491 *x = ctx->x;
492 *y = ctx->y;
493}
494A_INTERN void a_vector2_set_val(a_vector2 *ctx, a_real x, a_real y)
495{
496 ctx->x = x;
497 ctx->y = y;
498}
499A_INTERN void a_vector2_pol(a_vector2 const *ctx, a_real *rho, a_real *theta)
500{
501 a_real_cart2pol(ctx->x, ctx->y, rho, theta);
502}
503A_INTERN void a_vector2_set_pol(a_vector2 *ctx, a_real rho, a_real theta)
504{
505 a_real_pol2cart(rho, theta, &ctx->x, &ctx->y);
506}
507A_INTERN void a_vector2_add(a_vector2 const *lhs, a_vector2 const *rhs, a_vector2 *res)
508{
509 res->x = lhs->x + rhs->x;
510 res->y = lhs->y + rhs->y;
511}
512A_INTERN void a_vector2_sub(a_vector2 const *lhs, a_vector2 const *rhs, a_vector2 *res)
513{
514 res->x = lhs->x - rhs->x;
515 res->y = lhs->y - rhs->y;
516}
517A_INTERN void a_vector2_mul(a_vector2 const *lhs, a_real rhs, a_vector2 *res)
518{
519 res->x = lhs->x * rhs;
520 res->y = lhs->y * rhs;
521}
522A_INTERN void a_vector2_div(a_vector2 const *lhs, a_real rhs, a_vector2 *res)
523{
524 res->x = lhs->x / rhs;
525 res->y = lhs->y / rhs;
526}
527A_INTERN void a_vector2_neg(a_vector2 const *ctx, a_vector2 *res)
528{
529 res->x = -ctx->x;
530 res->y = -ctx->y;
531}
532
533#endif /* A_HAVE_INLINE */
534#if defined(LIBA_VECTOR2_C)
536#undef A_INTERN
537#define A_INTERN static A_INLINE
539#endif /* LIBA_VECTOR2_C */
540
542
543#endif /* a/vector2.h */
algorithm library
void set(a_point2 const &p, a_point2 const &q)
set a 2D vector as the difference from point p to point q.
Definition point2.h:175
double a_real
compiler built-in floating-point number type
Definition a.h:1012
void a_vector2_add(a_vector2 const *lhs, a_vector2 const *rhs, a_vector2 *res)
add a 2D vector to a 2D vector.
void a_vector2_set_val(a_vector2 *ctx, a_real x, a_real y)
set the cartesian coordinates of a 2D vector.
a_real a_vector2_set_dir(a_vector2 *ctx, a_real x, a_real y)
set the components of a 2D vector and normalize it in place.
void a_vector2_mul(a_vector2 const *lhs, a_real rhs, a_vector2 *res)
multiplie a 2D vector by a scalar.
void a_vector2_pol(a_vector2 const *ctx, a_real *rho, a_real *theta)
get the polar coordinates of a 2D vector.
a_bool a_vector2_ispar(a_vector2 const *lhs, a_vector2 const *rhs)
check if two 2D vectors are parallel or anti-parallel.
a_real a_vector2_norm2(a_vector2 const *ctx)
compute the squared magnitude of a 2D vector.
a_bool a_vector2_isver(a_vector2 const *lhs, a_vector2 const *rhs)
check if two 2D vectors are orthogonal.
void a_vector2_set_pol(a_vector2 *ctx, a_real rho, a_real theta)
set the polar coordinates of a 2D vector.
a_real a_vector2_cross(a_vector2 const *lhs, a_vector2 const *rhs)
compute the cross product (vector product) of two 2D vectors
a_real a_vector2_dot(a_vector2 const *lhs, a_vector2 const *rhs)
compute the dot product (scalar product) of two 2D vectors.
a_real a_vector2_norm(a_vector2 const *ctx)
compute the magnitude of a 2D vector.
a_real a_vector2_unit(a_vector2 *ctx)
normalize a 2D vector in-place to unit length.
int a_vector2_proj(a_vector2 const *ctx, a_vector2 const *dir, a_vector2 *res)
project vector onto the direction of vector.
void a_vector2_div(a_vector2 const *lhs, a_real rhs, a_vector2 *res)
divide a 2D vector by a scalar.
a_real a_vector2_dist2(a_vector2 const *lhs, a_vector2 const *rhs)
compute the squared distance between two 2D vectors.
void a_vector2_neg(a_vector2 const *ctx, a_vector2 *res)
negate a 2D vector.
void a_vector2_outer(a_vector2 const *lhs, a_vector2 const *rhs, a_real res[4])
compute the outer product (tensor product) of two 2D vectors.
a_real a_vector2_angle(a_vector2 const *lhs, a_vector2 const *rhs)
compute the angle between two 2D vectors in radians.
void a_vector2_sub(a_vector2 const *lhs, a_vector2 const *rhs, a_vector2 *res)
subtract a 2D vector from a 2D vector.
int a_vector2_perp(a_vector2 const *ctx, a_vector2 const *dir, a_vector2 *res)
project vector onto the line perpendicular to vector.
int a_vector2_refl(a_vector2 const *ctx, a_vector2 const *dir, a_vector2 *res)
reflect vector across the line with direction vector.
void a_vector2_val(a_vector2 const *ctx, a_real *x, a_real *y)
get the cartesian coordinates of a 2D vector.
void a_vector2_rot_(a_vector2 const *ctx, a_real sin, a_real cos, a_vector2 *res)
rotate a 2D vector by a specified angle (in radians).
a_real a_vector2_dist(a_vector2 const *lhs, a_vector2 const *rhs)
compute the distance between two 2D vectors.
void a_vector2_rot(a_vector2 const *ctx, a_real angle, a_vector2 *res)
rotate a 2D vector by a specified angle (in radians).
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:185
instance structure for two-dimensional vector
Definition vector2.h:315
a_real unit()
normalize a 2D vector in-place to unit length.
Definition vector2.h:371
a_bool isver(a_vector2 const &rhs) const
check if two 2D vectors are orthogonal.
Definition vector2.h:385
friend a_real operator^(a_vector2 const &lhs, a_vector2 const &rhs)
compute the cross product (vector product) of two 2D vectors
Definition vector2.h:412
void rot(a_real angle, a_vector2 &res) const
rotate a 2D vector by a specified angle (in radians).
Definition vector2.h:408
void pol(a_real &rho, a_real &theta) const
get the polar coordinates of a 2D vector.
Definition vector2.h:336
friend a_vector2 operator*(a_real lhs, a_vector2 const &rhs)
multiplie a 2D vector by a scalar.
Definition vector2.h:442
void rot(a_real sin, a_real cos, a_vector2 &res) const
rotate a 2D vector by a specified angle (in radians).
Definition vector2.h:410
friend a_vector2 operator/(a_vector2 const &lhs, a_real rhs)
divide a 2D vector by a scalar.
Definition vector2.h:458
friend a_real operator*(a_vector2 const &lhs, a_vector2 const &rhs)
compute the dot product (scalar product) of two 2D vectors.
Definition vector2.h:435
void div(a_real rhs, a_vector2 &res) const
divide a 2D vector by a scalar.
Definition vector2.h:361
a_real cross(a_vector2 const &rhs) const
compute the cross product (vector product) of two 2D vectors
Definition vector2.h:389
a_real dist2(a_vector2 const &rhs) const
compute the squared distance between two 2D vectors.
Definition vector2.h:381
a_real dist(a_vector2 const &rhs) const
compute the distance between two 2D vectors.
Definition vector2.h:379
int perp(a_vector2 const &dir, a_vector2 &res) const
project vector onto the line perpendicular to vector.
Definition vector2.h:398
a_real dot(a_vector2 const &rhs) const
compute the dot product (scalar product) of two 2D vectors.
Definition vector2.h:377
void add(a_vector2 const &rhs, a_vector2 &res) const
add a 2D vector to a 2D vector.
Definition vector2.h:346
friend a_vector2 operator+(a_vector2 const &lhs, a_vector2 const &rhs)
add a 2D vector to a 2D vector.
Definition vector2.h:419
int refl(a_vector2 const &dir, a_vector2 &res) const
reflect vector across the line with direction vector.
Definition vector2.h:403
a_real norm() const
compute the magnitude of a 2D vector.
Definition vector2.h:373
void val(a_real &x_, a_real &y_) const
get the cartesian coordinates of a 2D vector.
Definition vector2.h:326
friend a_vector2 operator*(a_vector2 const &lhs, a_real rhs)
multiplie a 2D vector by a scalar.
Definition vector2.h:449
void mul(a_real rhs, a_vector2 &res) const
multiplie a 2D vector by a scalar.
Definition vector2.h:356
void neg(a_vector2 &res) const
negate a 2D vector.
Definition vector2.h:366
friend void operator+=(a_vector2 &lhs, a_vector2 const &rhs)
add a 2D vector to a 2D vector.
Definition vector2.h:417
void set_val(a_real x_, a_real y_)
set the cartesian coordinates of a 2D vector.
Definition vector2.h:331
friend a_vector2 operator-(a_vector2 const &rhs)
negate a 2D vector.
Definition vector2.h:466
int proj(a_vector2 const &dir, a_vector2 &res) const
project vector onto the direction of vector.
Definition vector2.h:393
friend void operator/=(a_vector2 &lhs, a_real rhs)
divide a 2D vector by a scalar.
Definition vector2.h:456
friend void operator-=(a_vector2 &lhs, a_vector2 const &rhs)
subtract a 2D vector from a 2D vector.
Definition vector2.h:426
void sub(a_vector2 const &rhs, a_vector2 &res) const
subtract a 2D vector from a 2D vector.
Definition vector2.h:351
friend void operator*=(a_vector2 &lhs, a_real rhs)
multiplie a 2D vector by a scalar.
Definition vector2.h:440
void outer(a_vector2 const &rhs, a_real res[4]) const
compute the outer product (tensor product) of two 2D vectors.
Definition vector2.h:391
a_bool ispar(a_vector2 const &rhs) const
check if two 2D vectors are parallel or anti-parallel.
Definition vector2.h:387
friend a_vector2 operator-(a_vector2 const &lhs, a_vector2 const &rhs)
subtract a 2D vector from a 2D vector.
Definition vector2.h:428
a_real set_dir(a_real x_, a_real y_)
set the components of a 2D vector and normalize it in place.
Definition vector2.h:321
void set_pol(a_real rho, a_real theta)
set the polar coordinates of a 2D vector.
Definition vector2.h:341
a_real norm2() const
compute the squared magnitude of a 2D vector.
Definition vector2.h:375
a_real angle(a_vector2 const &rhs) const
compute the squared distance between two 2D vectors.
Definition vector2.h:383