liba 0.1.15
An algorithm library based on C/C++
Loading...
Searching...
No Matches
two-dimensional vector
Collaboration diagram for two-dimensional vector:

Data Structures

struct  a_vector2
 instance structure for two-dimensional vector More...

Macros

#define A_VECTOR2_C(x, y)
#define a_vector2_c(x)
#define a_vector2_(_, x)

Typedefs

typedef struct a_vector2 a_vector2

Functions

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_val (a_vector2 const *ctx, a_real *x, a_real *y)
 get the cartesian coordinates of 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.
void a_vector2_pol (a_vector2 const *ctx, a_real *rho, a_real *theta)
 get the polar coordinates of a 2D vector.
void a_vector2_set_pol (a_vector2 *ctx, a_real rho, a_real theta)
 set the polar coordinates of a 2D vector.
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_sub (a_vector2 const *lhs, a_vector2 const *rhs, a_vector2 *res)
 subtract a 2D vector from a 2D vector.
void a_vector2_mul (a_vector2 const *lhs, a_real rhs, a_vector2 *res)
 multiplie a 2D vector by a scalar.
void a_vector2_div (a_vector2 const *lhs, a_real rhs, a_vector2 *res)
 divide a 2D vector by a scalar.
void a_vector2_neg (a_vector2 const *ctx, a_vector2 *res)
 negate a 2D vector.
a_real a_vector2_unit (a_vector2 *ctx)
 normalize a 2D vector in-place to unit length.
a_real a_vector2_norm (a_vector2 const *ctx)
 compute the magnitude of a 2D vector.
a_real a_vector2_norm2 (a_vector2 const *ctx)
 compute the squared magnitude of a 2D vector.
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_dist (a_vector2 const *lhs, a_vector2 const *rhs)
 compute the distance between two 2D vectors.
a_real a_vector2_dist2 (a_vector2 const *lhs, a_vector2 const *rhs)
 compute the squared distance between 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.
a_bool a_vector2_isver (a_vector2 const *lhs, a_vector2 const *rhs)
 check if two 2D vectors are orthogonal.
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_cross (a_vector2 const *lhs, a_vector2 const *rhs)
 compute the cross product (vector product) of two 2D vectors
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.
int a_vector2_proj (a_vector2 const *ctx, a_vector2 const *dir, a_vector2 *res)
 project vector onto the direction of 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_rot_ (a_vector2 const *ctx, a_real sin, a_real cos, a_vector2 *res)
 rotate a 2D vector by a specified angle (in radians).
void a_vector2_rot (a_vector2 const *ctx, a_real angle, a_vector2 *res)
 rotate a 2D vector by a specified angle (in radians).

Detailed Description

Macro Definition Documentation

◆ a_vector2_

#define a_vector2_ ( _,
x )
Value:
a_cast_s(a_vector2 _, x)
instance structure for two-dimensional vector
Definition vector2.h:315

◆ A_VECTOR2_C

#define A_VECTOR2_C ( x,
y )
Value:
#define a_real_c(x)
static cast to floating-point number
Definition a.h:1009

constructs a two-dimensional vector from cartesian coordinate

◆ a_vector2_c

#define a_vector2_c ( x)
Value:
a_cast_s(a_vector2, x)

static cast to two-dimensional vector

Function Documentation

◆ a_vector2_angle()

a_real a_vector2_angle ( a_vector2 const * lhs,
a_vector2 const * rhs )

compute the angle between two 2D vectors in radians.

In two-dimensional space, let vector \(\vec{a}\) have coordinates \((a_x,a_y)\) and let vector \(\vec{b}\) have coordinates \((b_x,b_y)\). Then the angle between \(\vec{a}\) and \(\vec{b}\) is given by:

\[ \theta=\arccos\left(\frac{\vec{a}\cdot\vec{b}}{\|\vec{a}\|\|\vec{b}\|}\right) =\arccos\left(\frac{\vec{a}\cdot\vec{b}}{\sqrt{\|\vec{a}\|^2\|\vec{b}\|^2}}\right) \]

Parameters
[in]lhsis left-hand side 2D vector
[in]rhsis right-hand side 2D vector
Returns
the angle between two 2D vectors in radians, in the range \([0,\pi]\)
Return values
0if the vector is null

◆ a_vector2_cross()

a_real a_vector2_cross ( a_vector2 const * lhs,
a_vector2 const * rhs )

compute the cross product (vector product) of two 2D vectors

In two-dimensional space, let vector \(\vec{a}\) have coordinates \((a_x,a_y)\) and let vector \(\vec{b}\) have coordinates \((b_x,b_y)\). Then the cross product of vectors \(\vec{a}\) and \(\vec{b}\) is defined as:

\[ \vec{a} \times \vec{b} = a_x b_y - a_y b_x \]

https://en.wikipedia.org/wiki/Cross_product

Parameters
[in]lhsis left-hand side 2D vector
[in]rhsis right-hand side 2D vector
Returns
the cross product of two 2D vectors

◆ a_vector2_dist()

a_real a_vector2_dist ( a_vector2 const * lhs,
a_vector2 const * rhs )

compute the distance between two 2D vectors.

In two-dimensional space, let vector \(\vec{a}\) have coordinates \((a_x,a_y)\) and let vector \(\vec{b}\) have coordinates \((b_x,b_y)\). Then the distance between \(\vec{a}\) and \(\vec{b}\) is given by:

\[ d(\vec{a},\vec{b})=\sqrt{(a_x-b_x)^2+(a_y-b_y)^2} \]

Parameters
[in]lhsis left-hand side 2D vector
[in]rhsis right-hand side 2D vector
Returns
the distance between two 2D vectors

◆ a_vector2_dist2()

a_real a_vector2_dist2 ( a_vector2 const * lhs,
a_vector2 const * rhs )

compute the squared distance between two 2D vectors.

In two-dimensional space, let vector \(\vec{a}\) have coordinates \((a_x,a_y)\) and let vector \(\vec{b}\) have coordinates \((b_x,b_y)\). Then the squared distance between \(\vec{a}\) and \(\vec{b}\) is given by:

\[ d(\vec{a},\vec{b})^2=(a_x-b_x)^2+(a_y-b_y)^2 \]

Parameters
[in]lhsis left-hand side 2D vector
[in]rhsis right-hand side 2D vector
Returns
the squared distance between two 2D vectors

◆ a_vector2_dot()

a_real a_vector2_dot ( a_vector2 const * lhs,
a_vector2 const * rhs )

compute the dot product (scalar product) of two 2D vectors.

In two-dimensional space, let vector \(\vec{a}\) have coordinates \((a_x,a_y)\) and let vector \(\vec{b}\) have coordinates \((b_x,b_y)\). Then the dot product of vectors \(\vec{a}\) and \(\vec{b}\) is defined as:

\[ \vec{a} \cdot \vec{b} = a_x b_x + a_y b_y \]

Parameters
[in]lhsis left-hand side 2D vector
[in]rhsis right-hand side 2D vector
Returns
the dot product of two 2D vectors

◆ a_vector2_ispar()

a_bool a_vector2_ispar ( a_vector2 const * lhs,
a_vector2 const * rhs )

check if two 2D vectors are parallel or anti-parallel.

Parameters
[in]lhsis left-hand side 2D vector
[in]rhsis right-hand side 2D vector
Returns
whether the vectors are nearly parallel or anti-parallel
Return values
trueif the vector is null

◆ a_vector2_isver()

a_bool a_vector2_isver ( a_vector2 const * lhs,
a_vector2 const * rhs )

check if two 2D vectors are orthogonal.

Parameters
[in]lhsis left-hand side 2D vector
[in]rhsis right-hand side 2D vector
Returns
whether the vectors are nearly orthogonal
Return values
trueif the vector is null

◆ a_vector2_norm()

a_real a_vector2_norm ( a_vector2 const * ctx)

compute the magnitude of a 2D vector.

In two-dimensional space, a vector \(\vec{v}\) with coordinates \((x,y)\) has a magnitude defined as:

\[ \|\vec{v}\|=\sqrt{x^2+y^2} \]

Parameters
[in]ctxpoints to the input vector
Returns
the magnitude of the vector

◆ a_vector2_norm2()

a_real a_vector2_norm2 ( a_vector2 const * ctx)

compute the squared magnitude of a 2D vector.

In two-dimensional space, a vector \(\vec{v}\) with coordinates \((x,y)\) has a squared magnitude defined as:

\[ \|\vec{v}\|^2=x^2+y^2 \]

Parameters
[in]ctxpoints to the input vector
Returns
the squared magnitude of the vector

◆ a_vector2_outer()

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.

In two-dimensional space, let vector \(\vec{a}\) have coordinates \((a_x,a_y)\) and let vector \(\vec{b}\) have coordinates \((b_x,b_y)\). Then the outer product of vectors \(\vec{a}\) and \(\vec{b}\) is defined as:

\[ \vec{a} \otimes \vec{b} = \begin{bmatrix} a_x b_x & a_x b_y \\ a_y b_x & a_y b_y \end{bmatrix} \]

https://en.wikipedia.org/wiki/Outer_product

Parameters
[in]lhsis left-hand side 2D vector
[in]rhsis right-hand side 2D vector
[out]resstores the 2×2 matrix in row-major order

◆ a_vector2_perp()

int a_vector2_perp ( a_vector2 const * ctx,
a_vector2 const * dir,
a_vector2 * res )

project vector onto the line perpendicular to vector.

In two-dimensional space, let \(\vec{v}\) be the vector to be projected, and let \(\vec{d}\) be the reference direction vector. Then the perpendicular projection of \(\vec{v}\) about the direction \(\vec{d}\) is defined as:

\[ \vec{v}' = \vec{v}-\frac{\vec{d}\cdot\vec{v}}{\|\vec{d}\|^2}\vec{d} \]

Parameters
[in]ctxpoints to the vector to be projected
[in]dirpoints to the reference direction vector
[out]resstores the perpendicular projection vector about the direction
Returns
error code value
Return values
<0failure
0success

◆ a_vector2_proj()

int a_vector2_proj ( a_vector2 const * ctx,
a_vector2 const * dir,
a_vector2 * res )

project vector onto the direction of vector.

In two-dimensional space, let \(\vec{v}\) be the vector to be projected, and let \(\vec{d}\) be the direction vector for projection. Then the projection of \(\vec{v}\) onto the direction of \(\vec{d}\) is defined as:

\[ \vec{v}' = \frac{\vec{d}\cdot\vec{v}}{\|\vec{d}\|^2}\vec{d} \]

Parameters
[in]ctxpoints to the vector to be projected
[in]dirpoints to the direction vector for projection
[out]resstores the projection vector onto the direction of vector
Returns
error code value
Return values
<0failure
0success

◆ a_vector2_refl()

int a_vector2_refl ( a_vector2 const * ctx,
a_vector2 const * dir,
a_vector2 * res )

reflect vector across the line with direction vector.

In two-dimensional space, let \(\vec{v}\) be the vector to be reflected, and let \(\vec{d}\) be the direction vector defining the reflection line. Then the reflection of \(\vec{v}\) about the line with direction \(\vec{d}\) is defined as:

\[ \vec{v}' = 2\frac{\vec{d}\cdot\vec{v}}{\|\vec{d}\|^2}\vec{d}-\vec{v} \]

Parameters
[in]ctxpoints to the vector to be reflected
[in]dirpoints to the direction vector defining the reflection line
[out]resstores the reflection vector about the line with direction
Returns
error code value
Return values
<0failure
0success

◆ a_vector2_rot()

void a_vector2_rot ( a_vector2 const * ctx,
a_real angle,
a_vector2 * res )

rotate a 2D vector by a specified angle (in radians).

In two-dimensional space, let \(v\) be any vector to rotate by angle \(\theta\) (right hand rule, anticlockwise). Then the rotated vector \(\vec{v}'\) is given by:

\[ \vec{v}' = \begin{bmatrix} \cos\theta & -\sin\theta \\ \sin\theta & \cos\theta \end{bmatrix} \vec{v} \]

https://en.wikipedia.org/wiki/Rotation_matrix

Parameters
[in]ctxpoints to the input vector to rotate
[in]angleis rotation angle in radians
[out]resstores the result vector

◆ a_vector2_rot_()

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).

In two-dimensional space, let \(v\) be any vector to rotate by angle \(\theta\) (right hand rule, anticlockwise). Then the rotated vector \(\vec{v}'\) is given by:

\[ \vec{v}' = \begin{bmatrix} \cos\theta & -\sin\theta \\ \sin\theta & \cos\theta \end{bmatrix} \vec{v} \]

https://en.wikipedia.org/wiki/Rotation_matrix

Parameters
[in]ctxpoints to the input vector to rotate
[in]sinis precomputed \(\sin(\theta)\)
[in]cosis precomputed \(\cos(\theta)\)
[out]resstores the result vector

◆ a_vector2_set_dir()

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.

Returns
the original length of the vector

◆ a_vector2_unit()

a_real a_vector2_unit ( a_vector2 * ctx)

normalize a 2D vector in-place to unit length.

Parameters
[in]ctxpoints to the input vector
Returns
the original length of the vector