liba 0.1.15
An algorithm library based on C/C++
Loading...
Searching...
No Matches
point2.h
Go to the documentation of this file.
1
5
6#ifndef LIBA_POINT2_H
7#define LIBA_POINT2_H
8
9#include "a.h"
10#include "vector2.h"
11
17
18/* clang-format off */
20#define A_POINT2_C(x, y) {a_real_c(x), a_real_c(y)}
21/* clang-format on */
22
24#define a_point2_c(x) a_cast_s(a_point2, x)
25#define a_point2_(_, x) a_cast_s(a_point2 _, x)
26
27typedef struct a_point2 a_point2;
28
29#if defined(__cplusplus)
30namespace a
31{
32typedef struct a_point2 point2;
33} /* namespace a */
34extern "C" {
35#endif /* __cplusplus */
36#if !defined A_HAVE_INLINE || defined(LIBA_POINT2_C)
38#undef A_INTERN
39#define A_INTERN A_PUBLIC extern
41#endif /* A_HAVE_INLINE */
42
46A_INTERN void a_vector2_set(a_vector2 *ctx, a_point2 const *p, a_point2 const *q);
47
51A_INTERN void a_point2_val(a_point2 const *ctx, a_real *x, a_real *y);
55A_INTERN void a_point2_set_val(a_point2 *ctx, a_real x, a_real y);
59A_INTERN void a_point2_pol(a_point2 const *ctx, a_real *rho, a_real *theta);
63A_INTERN void a_point2_set_pol(a_point2 *ctx, a_real rho, a_real theta);
64
68A_INTERN void a_point2_add(a_point2 const *lhs, a_vector2 const *rhs, a_point2 *res);
72A_INTERN void a_point2_sub(a_point2 const *lhs, a_vector2 const *rhs, a_point2 *res);
76A_INTERN void a_point2_mul(a_point2 const *lhs, a_real rhs, a_point2 *res);
80A_INTERN void a_point2_div(a_point2 const *lhs, a_real rhs, a_point2 *res);
84A_INTERN void a_point2_pos(a_point2 const *ctx, a_vector2 *res);
88A_INTERN void a_point2_neg(a_point2 const *ctx, a_vector2 *res);
89
100A_EXTERN a_real a_point2_norm(a_point2 const *ctx);
111A_INTERN a_real a_point2_norm2(a_point2 const *ctx);
125A_EXTERN a_real a_point2_dist(a_point2 const *lhs, a_point2 const *rhs);
139A_INTERN a_real a_point2_dist2(a_point2 const *lhs, a_point2 const *rhs);
140
151A_EXTERN a_real a_point2_mindist(a_point2 const *ctx, a_point2 const *i_p, a_size i_n,
152 a_point2 *o_p /*=A_NULL*/, a_size *o_i /*=A_NULL*/);
153
164A_EXTERN a_real a_point2_maxdist(a_point2 const *ctx, a_point2 const *i_p, a_size i_n,
165 a_point2 *o_p /*=A_NULL*/, a_size *o_i /*=A_NULL*/);
166
179A_INTERN void a_point2_lerp(a_point2 const *lhs, a_point2 const *rhs, a_real val, a_point2 *res);
180
191A_EXTERN a_real a_point2_tricir(a_point2 const *p1, a_point2 const *p2, a_point2 const *p3, a_point2 *pc);
222A_EXTERN a_real a_point2_tricir2(a_point2 const *p1, a_point2 const *p2, a_point2 const *p3, a_point2 *pc);
223
233A_EXTERN int a_point2_cmpx(a_point2 const *lhs, a_point2 const *rhs);
234
244A_EXTERN int a_point2_cmpy(a_point2 const *lhs, a_point2 const *rhs);
245
246#if !defined A_HAVE_INLINE || defined(LIBA_POINT2_C)
248#undef A_INTERN
249#define A_INTERN static A_INLINE
251#endif /* A_HAVE_INLINE */
252#if defined(__cplusplus)
253} /* extern "C" */
254A_INLINE void a_vector2::set(a_point2 const &p, a_point2 const &q)
255{
256 a_vector2_set(this, &p, &q);
257}
258#endif /* __cplusplus */
259
264{
265 a_real x, y;
266#if defined(__cplusplus)
268 A_INLINE void val(a_real &x_, a_real &y_) const
269 {
270 a_point2_val(this, &x_, &y_);
271 }
272
273 A_INLINE void set_val(a_real x_, a_real y_)
274 {
275 a_point2_set_val(this, x_, y_);
276 }
277
278 A_INLINE void pol(a_real &rho, a_real &theta) const
279 {
280 a_point2_pol(this, &rho, &theta);
281 }
282
283 A_INLINE void set_pol(a_real rho, a_real theta)
284 {
285 a_point2_set_pol(this, rho, theta);
286 }
287
288 A_INLINE void add(a_vector2 const &rhs, a_point2 &res) const
289 {
290 a_point2_add(this, &rhs, &res);
291 }
292
293 A_INLINE void sub(a_vector2 const &rhs, a_point2 &res) const
294 {
295 a_point2_sub(this, &rhs, &res);
296 }
297
298 A_INLINE void mul(a_real rhs, a_point2 &res) const
299 {
300 a_point2_mul(this, rhs, &res);
301 }
302
303 A_INLINE void div(a_real rhs, a_point2 &res) const
304 {
305 a_point2_div(this, rhs, &res);
306 }
307
308 A_INLINE void pos(a_vector2 &res) const
309 {
310 a_point2_pos(this, &res);
311 }
312
313 A_INLINE void neg(a_vector2 &res) const
314 {
315 a_point2_neg(this, &res);
316 }
317
318 A_INLINE a_real norm() const { return a_point2_norm(this); }
320 A_INLINE a_real norm2() const { return a_point2_norm2(this); }
322 A_INLINE a_real dist(a_point2 const &rhs) const
323 {
324 return a_point2_dist(this, &rhs);
325 }
326
327 A_INLINE a_real dist2(a_point2 const &rhs) const
328 {
329 return a_point2_dist2(this, &rhs);
330 }
331
332 A_INLINE a_real mindist(a_point2 const *i_p, a_size i_n, a_point2 *o_p = A_NULL, a_size *o_i = A_NULL) const
333 {
334 return a_point2_mindist(this, i_p, i_n, o_p, o_i);
335 }
336
337 A_INLINE a_real maxdist(a_point2 const *i_p, a_size i_n, a_point2 *o_p = A_NULL, a_size *o_i = A_NULL) const
338 {
339 return a_point2_maxdist(this, i_p, i_n, o_p, o_i);
340 }
341
342 A_INLINE void lerp(a_point2 const &rhs, a_real val, a_point2 &res) const
343 {
344 a_point2_lerp(this, &rhs, val, &res);
345 }
346
347 A_INLINE a_real tricir(a_point2 const &p2, a_point2 const &p3, a_point2 &pc) const
348 {
349 return a_point2_tricir(this, &p2, &p3, &pc);
350 }
351
352 A_INLINE a_real tricir2(a_point2 const &p2, a_point2 const &p3, a_point2 &pc) const
353 {
354 return a_point2_tricir2(this, &p2, &p3, &pc);
355 }
356
357 friend A_INLINE void operator+=(a_point2 &lhs, a_vector2 const &rhs) { a_point2_add(&lhs, &rhs, &lhs); }
359 friend A_INLINE a_point2 operator+(a_point2 const &lhs, a_vector2 const &rhs)
360 {
361 a_point2 res;
362 a_point2_add(&lhs, &rhs, &res);
363 return res;
364 }
365
366 friend A_INLINE void operator-=(a_point2 &lhs, a_vector2 const &rhs) { a_point2_sub(&lhs, &rhs, &lhs); }
368 friend A_INLINE a_point2 operator-(a_point2 const &lhs, a_vector2 const &rhs)
369 {
370 a_point2 res;
371 a_point2_sub(&lhs, &rhs, &res);
372 return res;
373 }
374
375 friend A_INLINE a_vector2 operator-(a_point2 const &lhs, a_point2 const &rhs)
376 {
377 a_vector2 res;
378 a_vector2_set(&res, &rhs, &lhs);
379 return res;
380 }
381
382 friend A_INLINE void operator*=(a_point2 &lhs, a_real rhs) { a_point2_mul(&lhs, rhs, &lhs); }
384 friend A_INLINE a_point2 operator*(a_real lhs, a_point2 const &rhs)
385 {
386 a_point2 res;
387 a_point2_mul(&rhs, lhs, &res);
388 return res;
389 }
390
391 friend A_INLINE a_point2 operator*(a_point2 const &lhs, a_real rhs)
392 {
393 a_point2 res;
394 a_point2_mul(&lhs, rhs, &res);
395 return res;
396 }
397
398 friend A_INLINE void operator/=(a_point2 &lhs, a_real rhs) { a_point2_div(&lhs, rhs, &lhs); }
400 friend A_INLINE a_point2 operator/(a_point2 const &lhs, a_real rhs)
401 {
402 a_point2 res;
403 a_point2_div(&lhs, rhs, &res);
404 return res;
405 }
406
407 friend A_INLINE a_vector2 operator+(a_point2 const &rhs)
408 {
409 a_vector2 res;
410 a_point2_pos(&rhs, &res);
411 return res;
412 }
413
414 friend A_INLINE a_vector2 operator-(a_point2 const &rhs)
415 {
416 a_vector2 res;
417 a_point2_neg(&rhs, &res);
418 return res;
419 }
420#endif /* __cplusplus */
421};
422
423#if defined(LIBA_POINT2_C)
425#undef A_INTERN
426#define A_INTERN A_INLINE
428#endif /* LIBA_POINT2_C */
429#if defined(A_HAVE_INLINE) || defined(LIBA_POINT2_C)
430
431A_INTERN void a_vector2_set(a_vector2 *ctx, a_point2 const *p, a_point2 const *q)
432{
433 ctx->x = q->x - p->x;
434 ctx->y = q->y - p->y;
435}
436A_INTERN void a_point2_val(a_point2 const *ctx, a_real *x, a_real *y)
437{
438 *x = ctx->x;
439 *y = ctx->y;
440}
441A_INTERN void a_point2_set_val(a_point2 *ctx, a_real x, a_real y)
442{
443 ctx->x = x;
444 ctx->y = y;
445}
446A_INTERN void a_point2_pol(a_point2 const *ctx, a_real *rho, a_real *theta)
447{
448 a_real_cart2pol(ctx->x, ctx->y, rho, theta);
449}
450A_INTERN void a_point2_set_pol(a_point2 *ctx, a_real rho, a_real theta)
451{
452 a_real_pol2cart(rho, theta, &ctx->x, &ctx->y);
453}
454A_INTERN void a_point2_add(a_point2 const *lhs, a_vector2 const *rhs, a_point2 *res)
455{
456 res->x = lhs->x + rhs->x;
457 res->y = lhs->y + rhs->y;
458}
459A_INTERN void a_point2_sub(a_point2 const *lhs, a_vector2 const *rhs, a_point2 *res)
460{
461 res->x = lhs->x - rhs->x;
462 res->y = lhs->y - rhs->y;
463}
464A_INTERN void a_point2_mul(a_point2 const *lhs, a_real rhs, a_point2 *res)
465{
466 res->x = lhs->x * rhs;
467 res->y = lhs->y * rhs;
468}
469A_INTERN void a_point2_div(a_point2 const *lhs, a_real rhs, a_point2 *res)
470{
471 res->x = lhs->x / rhs;
472 res->y = lhs->y / rhs;
473}
474A_INTERN void a_point2_pos(a_point2 const *ctx, a_vector2 *res)
475{
476 res->x = +ctx->x;
477 res->y = +ctx->y;
478}
479A_INTERN void a_point2_neg(a_point2 const *ctx, a_vector2 *res)
480{
481 res->x = -ctx->x;
482 res->y = -ctx->y;
483}
484
485A_INTERN a_real a_point2_norm2(a_point2 const *ctx)
486{
487 return ctx->x * ctx->x + ctx->y * ctx->y;
488}
489
490A_INTERN a_real a_point2_dist2(a_point2 const *lhs, a_point2 const *rhs)
491{
492 a_real const x = rhs->x - lhs->x;
493 a_real const y = rhs->y - lhs->y;
494 return x * x + y * y;
495}
496
497A_INTERN void a_point2_lerp(a_point2 const *lhs, a_point2 const *rhs, a_real val, a_point2 *res)
498{
499 res->x = lhs->x + (rhs->x - lhs->x) * val;
500 res->y = lhs->y + (rhs->y - lhs->y) * val;
501}
502
503#endif /* A_HAVE_INLINE */
504#if defined(LIBA_POINT2_C)
506#undef A_INTERN
507#define A_INTERN static A_INLINE
509#endif /* LIBA_POINT2_C */
510
512
513#endif /* a/point2.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:254
a_real a_point2_dist(a_point2 const *lhs, a_point2 const *rhs)
compute the distance between two 2D points.
void a_vector2_set(a_vector2 *ctx, a_point2 const *p, a_point2 const *q)
set a 2D vector as the difference from point p to point q.
void a_point2_pol(a_point2 const *ctx, a_real *rho, a_real *theta)
get the polar coordinates of a 2D point.
void a_point2_div(a_point2 const *lhs, a_real rhs, a_point2 *res)
divide a 2D point by a scalar.
a_real a_point2_dist2(a_point2 const *lhs, a_point2 const *rhs)
compute the squared distance between two 2D points.
void a_point2_neg(a_point2 const *ctx, a_vector2 *res)
convert it into a vector from the point to the origin.
a_real a_point2_maxdist(a_point2 const *ctx, a_point2 const *i_p, a_size i_n, a_point2 *o_p, a_size *o_i)
compute the maximum distance from a reference point to a point set.
void a_point2_pos(a_point2 const *ctx, a_vector2 *res)
convert it into a vector from the origin to the point.
a_real a_point2_norm(a_point2 const *ctx)
compute the magnitude of a 2D point.
int a_point2_cmpy(a_point2 const *lhs, a_point2 const *rhs)
compare two 2D points primarily by Y-coordinate, then by X-coordinate.
void a_point2_set_pol(a_point2 *ctx, a_real rho, a_real theta)
set the polar coordinates of a 2D point.
void a_point2_lerp(a_point2 const *lhs, a_point2 const *rhs, a_real val, a_point2 *res)
compute linear interpolation (LERP) between two 2D points.
void a_point2_val(a_point2 const *ctx, a_real *x, a_real *y)
get the cartesian coordinates of a 2D point.
a_real a_point2_mindist(a_point2 const *ctx, a_point2 const *i_p, a_size i_n, a_point2 *o_p, a_size *o_i)
compute the minimum distance from a reference point to a point set.
a_real a_point2_norm2(a_point2 const *ctx)
compute the squared magnitude of a 2D point.
void a_point2_set_val(a_point2 *ctx, a_real x, a_real y)
set the cartesian coordinates of a 2D point.
void a_point2_add(a_point2 const *lhs, a_vector2 const *rhs, a_point2 *res)
add a 2D vector to a 2D point.
void a_point2_mul(a_point2 const *lhs, a_real rhs, a_point2 *res)
multiplie a 2D point by a scalar.
int a_point2_cmpx(a_point2 const *lhs, a_point2 const *rhs)
compare two 2D points primarily by X-coordinate, then by Y-coordinate.
void a_point2_sub(a_point2 const *lhs, a_vector2 const *rhs, a_point2 *res)
subtract a 2D vector from a 2D point.
a_real a_point2_tricir(a_point2 const *p1, a_point2 const *p2, a_point2 const *p3, a_point2 *pc)
compute the circumcenter and circumradius of a triangle defined by three 2D points.
a_real a_point2_tricir2(a_point2 const *p1, a_point2 const *p2, a_point2 const *p3, a_point2 *pc)
compute the circumcenter and squared circumradius of a triangle defined by three 2D points.
double a_real
compiler built-in floating-point number type
Definition a.h:1012
size_t a_size
unsigned integer type returned by the sizeof operator
Definition a.h:823
instance structure for two-dimensional point
Definition point2.h:264
void mul(a_real rhs, a_point2 &res) const
multiplie a 2D point by a scalar.
Definition point2.h:298
friend void operator/=(a_point2 &lhs, a_real rhs)
divide a 2D point by a scalar.
Definition point2.h:398
friend void operator*=(a_point2 &lhs, a_real rhs)
multiplie a 2D point by a scalar.
Definition point2.h:382
void add(a_vector2 const &rhs, a_point2 &res) const
add a 2D vector to a 2D point.
Definition point2.h:288
friend void operator-=(a_point2 &lhs, a_vector2 const &rhs)
subtract a 2D vector from a 2D point.
Definition point2.h:366
friend a_vector2 operator-(a_point2 const &lhs, a_point2 const &rhs)
set a 2D vector as the difference from point p to point q.
Definition point2.h:375
void set_pol(a_real rho, a_real theta)
set the polar coordinates of a 2D point.
Definition point2.h:283
friend a_point2 operator+(a_point2 const &lhs, a_vector2 const &rhs)
add a 2D vector to a 2D point.
Definition point2.h:359
friend void operator+=(a_point2 &lhs, a_vector2 const &rhs)
add a 2D vector to a 2D point.
Definition point2.h:357
void div(a_real rhs, a_point2 &res) const
divide a 2D point by a scalar.
Definition point2.h:303
friend a_point2 operator-(a_point2 const &lhs, a_vector2 const &rhs)
subtract a 2D vector from a 2D point.
Definition point2.h:368
void pos(a_vector2 &res) const
convert it into a vector from the origin to the point.
Definition point2.h:308
a_real tricir(a_point2 const &p2, a_point2 const &p3, a_point2 &pc) const
compute the circumcenter and circumradius of a triangle defined by three 2D points.
Definition point2.h:347
a_real dist(a_point2 const &rhs) const
compute the distance between two 2D points.
Definition point2.h:322
a_real maxdist(a_point2 const *i_p, a_size i_n, a_point2 *o_p=NULL, a_size *o_i=NULL) const
compute the maximum distance from a reference point to a point set.
Definition point2.h:337
void sub(a_vector2 const &rhs, a_point2 &res) const
subtract a 2D vector from a 2D point.
Definition point2.h:293
friend a_vector2 operator+(a_point2 const &rhs)
convert it into a vector from the origin to the point.
Definition point2.h:407
void neg(a_vector2 &res) const
convert it into a vector from the point to the origin.
Definition point2.h:313
a_real norm2() const
compute the squared magnitude of a 2D point.
Definition point2.h:320
friend a_vector2 operator-(a_point2 const &rhs)
convert it into a vector from the point to the origin.
Definition point2.h:414
void set_val(a_real x_, a_real y_)
set the cartesian coordinates of a 2D point.
Definition point2.h:273
a_real norm() const
compute the magnitude of a 2D point.
Definition point2.h:318
friend a_point2 operator/(a_point2 const &lhs, a_real rhs)
divide a 2D point by a scalar.
Definition point2.h:400
void pol(a_real &rho, a_real &theta) const
get the polar coordinates of a 2D point.
Definition point2.h:278
void val(a_real &x_, a_real &y_) const
get the cartesian coordinates of a 2D point.
Definition point2.h:268
friend a_point2 operator*(a_real lhs, a_point2 const &rhs)
multiplie a 2D point by a scalar.
Definition point2.h:384
a_real tricir2(a_point2 const &p2, a_point2 const &p3, a_point2 &pc) const
compute the circumcenter and squared circumradius of a triangle defined by three 2D points.
Definition point2.h:352
a_real dist2(a_point2 const &rhs) const
compute the squared distance between two 2D points.
Definition point2.h:327
a_real mindist(a_point2 const *i_p, a_size i_n, a_point2 *o_p=NULL, a_size *o_i=NULL) const
compute the minimum distance from a reference point to a point set.
Definition point2.h:332
friend a_point2 operator*(a_point2 const &lhs, a_real rhs)
multiplie a 2D point by a scalar.
Definition point2.h:391
void lerp(a_point2 const &rhs, a_real val, a_point2 &res) const
compute linear interpolation (LERP) between two 2D points.
Definition point2.h:342
instance structure for two-dimensional vector
Definition vector2.h:343
two-dimensional vector