2D vectors libraryVector2D.h
2D vectors library provide primitives for math calculations with two dimensional vectors. It implements basic functions of linear algebra in vector space and can be used in analytic geometry, engineering, physics, natural sciences, computer science, and the social sciences (particularly in economics). The library provides structures/classes with public components which represent projections of vector onto common axes: X and Y. You may set these components directly or change them via the library functions.
Contents
- 2D vector structure
- Arithmetic operations
- Unary operations
- Normalization (direction cosines)
- Reflection of vector
- Reflection through the origin
- Reflection through the X axis
- Reflection through the Y axis
- Binary operations
- Addition of vectors
- Subtraction of vectors
- Multiplication by scalar value
- Division by scalar value
- Rotation of vector
- Shearing of vector
- Shearing parallel to the X axis
- Shearing parallel to the Y axis
- Scaling of vector
- Scalar product
- Absolute value
- Cosine value of angle between the vectors
- Projection of the vector to another vector
- Checks
- Check for zero vector
- Check for equality of the vectors
- Check for negativity of the vectors
- Check for collinearity of the vectors
- Check for orthogonality of the vectors
Function list
C function name | Functions | C++ function name | Functions |
---|---|---|---|
Abs | 2 functions | Abs | 2 functions |
Add | 2 functions | Add | 2 functions |
Cos | 2 functions | Cos | 2 functions |
Div | 2 functions | Div | 2 functions |
IsCollinear | 2 functions | IsCollinear | 2 functions |
IsEqual | 2 functions | IsEqual | 2 functions |
IsNeg | 2 functions | IsNeg | 2 functions |
IsOrthogonal | 2 functions | IsOrthogonal | 2 functions |
IsZero | 2 functions | IsZero | 2 functions |
Mul | 2 functions | Mul | 2 functions |
Normalize | 2 functions | Normalize | 2 functions |
Projection | 2 functions | Projection | 2 functions |
ReflectOrigin | 2 functions | ReflectOrigin | 2 functions |
ReflectX | 2 functions | ReflectX | 2 functions |
ReflectY | 2 functions | ReflectY | 2 functions |
Rotate | 2 functions | Rotate | 2 functions |
ScalarProduct | 2 functions | ScalarProduct | 2 functions |
Scale | 2 functions | Scale | 2 functions |
ShearX | 2 functions | ShearX | 2 functions |
ShearY | 2 functions | ShearY | 2 functions |
Sub | 2 functions | Sub | 2 functions |
C function name | Functions | C++ function name | Functions |
2D vector structure
Vector structure has two elements, which represent x and y components of vector. You may set these fields directly or change their values in your code. Vector structure is defined below.
C/C++Single precision 2D vector struct v2D32_t { flt32_t x; flt32_t y; }; Double precision 2D vector struct v2D64_t { flt64_t x; flt64_t y; };
Both 32-bit and 64-bit vector structures have following members:
- x - projection of vector onto X axis
- y - projection of vector onto Y axis
Arithmetic operations
Algorithms of arithmetic operations with vectors implement standard math operations of vector algebra. They are divided onto two groups: unary and binary. Both operations change components of target vector and store the result into the same vector. I.e. they modify original vector (unary operations) and change target vector (binary operations).
Unary operations
Unary operations change vector components and store the result into the same vector. They implement basic manipulations with vectors like normalizations and reflections through the axes.
Normalization (direction cosines)
Cvoid Vector2D_Normalize_flt32 (struct v2D32_t *vector); void Vector2D_Normalize_flt64 (struct v2D64_t *vector);
C++void Vector2D::Normalize (v2D32_t *vector); void Vector2D::Normalize (v2D64_t *vector);
Description: Normalize vector (find its direction cosines) by division the vector to its absolute value.
Parameters:
- vector - pointer to the vector
Return value: None.
Reflection of vector
Reflection functions change sign of vector components to reflect vectors through origin and axes.
Reflection through the origintop
Cvoid Vector2D_ReflectOrigin_flt32 (struct v2D32_t *vector); void Vector2D_ReflectOrigin_flt64 (struct v2D64_t *vector);
C++void Vector2D::ReflectOrigin (v2D32_t *vector); void Vector2D::ReflectOrigin (v2D64_t *vector;
Description: Reflect vector through the origin (change sign of X, and Y coordinates).
Parameters:
- vector - pointer to the vector
Return value: None.
Reflection through the X axistop
Cvoid Vector2D_ReflectX_flt32 (struct v2D32_t *vector); void Vector2D_ReflectX_flt64 (struct v2D64_t *vector);
C++void Vector2D::ReflectX (v2D32_t *vector); void Vector2D::ReflectX (v2D64_t *vector);
Description: Reflect vector through the X axis (change sign of Y coordinate).
Parameters:
- vector - pointer to the vector
Return value: None.
Reflection through the Y axistop
Cvoid Vector2D_ReflectY_flt32 (struct v2D32_t *vector); void Vector2D_ReflectY_flt64 (struct v2D64_t *vector);
C++void Vector2D::ReflectY (v2D32_t *vector); void Vector2D::ReflectY (v2D64_t *vector);
Description: Reflect vector through the Y axis (change sign of X coordinate).
Parameters:
- vector - pointer to the vector
Return value: None.
Binary operations
Binary operations with vectors take two operands, which called target and source. Then they apply arithmetic operations to target vector, using source vector as second operand, and then store the result into target operand.
Addition of vectors
Cvoid Vector2D_Add_flt32 (struct v2D32_t *target, const struct v2D32_t *source); void Vector2D_Add_flt64 (struct v2D64_t *target, const struct v2D64_t *source);
C++void Vector2D::Add (v2D32_t *target, const v2D32_t *source); void Vector2D::Add (v2D64_t *target, const v2D64_t *source);
Description: Add source vector to target vector and store the result into the same place (into target vector).
Parameters:
- target - pointer to target vector
- source - pointer to source vector
Return value: None.
Subtraction of vectors
Cvoid Vector2D_Sub_flt32 (struct v2D32_t *target, const struct v2D32_t *source); void Vector2D_Sub_flt64 (struct v2D64_t *target, const struct v2D64_t *source);
C++void Vector2D::Sub (v2D32_t *target, const v2D32_t *source); void Vector2D::Sub (v2D64_t *target, const v2D64_t *source);
Description: Subtract source vector from target vector and store the result into the same place (into target vector).
Parameters:
- target - pointer to target vector
- source - pointer to source vector
Return value: None.
Multiplication by scalar value
Cvoid Vector2D_Mul_flt32 (struct v2D32_t *vector, flt32_t value); void Vector2D_Mul_flt64 (struct v2D64_t *vector, flt64_t value);
C++void Vector2D::Mul (v2D32_t *vector, flt32_t value); void Vector2D::Mul (v2D64_t *vector, flt64_t value);
Description: Multiply vector by scalar value and store the result into the same place.
Parameters:
- vector - pointer to the vector
- value - value to multiply
Return value: None.
Division by scalar value
Cvoid Vector2D_Div_flt32 (struct v2D32_t *vector, flt32_t value); void Vector2D_Div_flt64 (struct v2D64_t *vector, flt64_t value);
C++void Vector2D::Div (v2D32_t *vector, flt32_t value); void Vector2D::Div (v2D64_t *vector, flt64_t value);
Description: Divide vector by scalar value and store the result into the same place.
Parameters:
- vector - pointer to the vector
- value - value to divide
Return value: None.
Rotation of vector
Cvoid Vector2D_Rotate_flt32 (struct v2D32_t *vector, flt32_t cos, flt32_t sin); void Vector2D_Rotate_flt64 (struct v2D64_t *vector, flt64_t cos, flt64_t sin);
C++void Vector2D::Rotate (v2D32_t *vector, flt32_t cos, flt32_t sin); void Vector2D::Rotate (v2D64_t *vector, flt64_t cos, flt64_t sin);
Description: Rotate vector using cosine and sine value of rotation angle.
Parameters:
- vector - pointer to the vector
- cos - cosine value of angle to rotate
- sin - sine value of angle to rotate
Return value: None.
Shearing of vector
Shear functions do shear operations to vector in parallel to common axes. They use the value you provide to multiply it on second vector component, then add this value as shearing factor.
Shearing parallel to the X axis
Cvoid Vector2D_ShearX_flt32 (struct v2D32_t *vector, flt32_t value); void Vector2D_ShearX_flt64 (struct v2D64_t *vector, flt64_t value);
C++void Vector2D::ShearX (v2D32_t *vector, flt32_t value); void Vector2D::ShearX (v2D64_t *vector, flt64_t value);
Description: Shear vector parallel to the X axis.
Parameters:
- vector - pointer to the vector
- value - shear value
Return value: None.
Shearing parallel to the Y axis
Cvoid Vector2D_ShearY_flt32 (struct v2D32_t *vector, flt32_t value); void Vector2D_ShearY_flt64 (struct v2D64_t *vector, flt64_t value);
C++void Vector2D::ShearY (v2D32_t *vector, flt32_t value); void Vector2D::ShearY (v2D64_t *vector, flt64_t value);
Description: Shear vector parallel to the Y axis.
Parameters:
- vector - pointer to the vector
- value - shear value
Return value: None.
Scaling of vector
Cvoid Vector2D_Scale_flt32 (struct v2D32_t *target, const struct v2D32_t *source); void Vector2D_Scale_flt64 (struct v2D64_t *target, const struct v2D64_t *source);
C++void Vector2D::Scale (v2D32_t *target, const v2D32_t *source); void Vector2D::Scale (v2D64_t *target, const v2D64_t *source);
Description: Scale target vector using appropriate coordinates of source vector as scale values.
Parameters:
- target - pointer to target vector
- source - pointer to source vector
Return value: None.
Scalar product
Cflt32_t Vector2D_ScalarProduct_flt32 (const struct v2D32_t *target, const struct v2D32_t *source); flt64_t Vector2D_ScalarProduct_flt64 (const struct v2D64_t *target, const struct v2D64_t *source);
C++flt32_t Vector2D::ScalarProduct (const v2D32_t *target, const v2D32_t *source); flt64_t Vector2D::ScalarProduct (const v2D64_t *target, const v2D64_t *source);
Description: Compute scalar product of target and source vectors.
Parameters:
- target - pointer to target vector
- source - pointer to source vector
Return value:
- Scalar product of target and source vectors.
- NaN (not a number) if either target or source vector has NaN values.
Absolute value
Cflt32_t Vector2D_Abs_flt32 (const struct v2D32_t *vector); flt64_t Vector2D_Abs_flt64 (const struct v2D64_t *vector);
C++flt32_t Vector2D::Abs (const v2D32_t *vector); flt64_t Vector2D::Abs (const v2D64_t *vector);
Description: Calculate absolute value of vector.
Parameters:
- vector - pointer to the vector
Return value:
- Absolute value of vector.
- Inf (infinity) if absolute value is too big and cannot be represented in selected floating-point type.
- Subnormal value in case of underflow.
- 0 (zero) if vector absolute value can not be represented as subnormal value, or it is equal to zero.
Cosine value of angle between the vectors
Cflt32_t Vector2D_Cos_flt32 (const struct v2D32_t *target, const struct v2D32_t *source); flt64_t Vector2D_Cos_flt64 (const struct v2D64_t *target, const struct v2D64_t *source);
C++flt32_t Vector2D::Cos (const v2D32_t *target, const v2D32_t *source); flt64_t Vector2D::Cos (const v2D64_t *target, const v2D64_t *source);
Description: Find cosine value between the vectors.
Parameters:
- target - pointer to target vector
- source - pointer to source vector
Return value:
- Cosine value of angle between the vectors.
- NaN (not a number) if either target or source vector is equal to zero.
Projection of the vector to another vector
Cflt32_t Vector2D_Projection_flt32 (const struct v2D32_t *target, const struct v2D32_t *source); flt64_t Vector2D_Projection_flt64 (const struct v2D64_t *target, const struct v2D64_t *source);
C++flt32_t Vector2D::Projection (const v2D32_t *target, const v2D32_t *source); flt64_t Vector2D::Projection (const v2D64_t *target, const v2D64_t *source);
Description: Compute projection of target vector onto source vector.
Parameters:
- target - pointer to target vector
- source - pointer to source vector
Return value:
- Projection value of target vector onto source vector.
- Nan (not a number) if source vector is equal to zero.
Checks
Check functions compare vectors and return relation state between them. They also check vectors for special cases like zero vector.
Check for zero vector
Cbool Vector2D_IsZero_flt32 (const struct v2D32_t *vector); bool Vector2D_IsZero_flt64 (const struct v2D64_t *vector);
C++bool Vector2D::IsZero (const v2D32_t *vector); bool Vector2D::IsZero (const v2D64_t *vector);
Description: Check vector for zero value.
Parameters:
- vector - pointer to the vector
Return value:
- TRUE (1) if vector has zero value.
- FALSE (0) in other case.
Check for equality of the vectors
Cbool Vector2D_IsEqual_flt32 (const struct v2D32_t *target, const struct v2D32_t *source); bool Vector2D_IsEqual_flt64 (const struct v2D64_t *target, const struct v2D64_t *source);
C++bool Vector2D::IsEqual (const v2D32_t *target, const v2D32_t *source); bool Vector2D::IsEqual (const v2D64_t *target, const v2D64_t *source);
Description: Check vectors for equality.
Parameters:
- target - pointer to target vector
- source - pointer to source vector
Return value:
- TRUE (1) if all coordinates of target vector are equal to corresponding coordinates of source vector.
- FALSE (0) in other case.
Check for negativity of the vectors
Cbool Vector2D_IsNeg_flt32 (const struct v2D32_t *target, const struct v2D32_t *source); bool Vector2D_IsNeg_flt64 (const struct v2D64_t *target, const struct v2D64_t *source);
C++bool Vector2D::IsNeg (const v2D32_t *target, const v2D32_t *source); bool Vector2D::IsNeg (const v2D64_t *target, const v2D64_t *source);
Description: Check vectors for negativity (one vector is negative variant of another).
Parameters:
- target - pointer to target vector
- source - pointer to source vector
Return value:
- TRUE (1) if one vector is negative variant of another.
- FALSE (0) in other case.
Check for collinearity of the vectors
Cbool Vector2D_IsCollinear_flt32 (const struct v2D32_t *target, const struct v2D32_t *source); bool Vector2D_IsCollinear_flt64 (const struct v2D64_t *target, const struct v2D64_t *source);
C++bool Vector2D::IsCollinear (const v2D32_t *target, const v2D32_t *source); bool Vector2D::IsCollinear (const v2D64_t *target, const v2D64_t *source);
Description: Check if vectors are collinear.
Parameters:
- target - pointer to target vector
- source - pointer to source vector
Return value:
- TRUE (1) if vectors are collinear.
- FALSE (0) in other case.
Check for orthogonality of the vectors
Cbool Vector2D_IsOrthogonal_flt32 (const struct v2D32_t *target, const struct v2D32_t *source); bool Vector2D_IsOrthogonal_flt64 (const struct v2D64_t *target, const struct v2D64_t *source);
C++bool Vector2D::IsOrthogonal (const v2D32_t *target, const v2D32_t *source); bool Vector2D::IsOrthogonal (const v2D64_t *target, const v2D64_t *source);
Description: Check if vectors are orthogonal.
Parameters:
- target - pointer to target vector
- source - pointer to source vector
Return value:
- TRUE (1) if vectors are orthogonal.
- FALSE (0) in other case.