bgc-c/basic-geometry/vector3.c

65 lines
1.9 KiB
C

#include "vector3.h"
// =================== Angle ==================== //
float bgc_vector3_get_angle_fp32(const BgcVector3FP32* vector1, const BgcVector3FP32* vector2, const BgcAngleUnitEnum unit)
{
if (vector1 == 0 || vector2 == 0) {
return 0.0f;
}
const float square_modulus1 = bgc_vector3_get_square_modulus_fp32(vector1);
if (square_modulus1 <= BGC_SQUARE_EPSYLON_FP32) {
return 0.0f;
}
const float square_modulus2 = bgc_vector3_get_square_modulus_fp32(vector2);
if (square_modulus2 <= BGC_SQUARE_EPSYLON_FP32) {
return 0.0f;
}
const float cosine = bgc_vector3_scalar_product_fp32(vector1, vector2) / sqrtf(square_modulus1 * square_modulus2);
if (cosine >= 1.0f - BGC_EPSYLON_FP32) {
return 0.0f;
}
if (cosine <= -1.0f + BGC_EPSYLON_FP32) {
return bgc_angle_get_half_circle_fp32(unit);
}
return bgc_radians_to_units_fp32(acosf(cosine), unit);
}
double bgc_vector3_get_angle_fp64(const BgcVector3FP64* vector1, const BgcVector3FP64* vector2, const BgcAngleUnitEnum unit)
{
if (vector1 == 0 || vector2 == 0) {
return 0.0;
}
const double square_modulus1 = bgc_vector3_get_square_modulus_fp64(vector1);
if (square_modulus1 <= BGC_SQUARE_EPSYLON_FP64) {
return 0.0;
}
const double square_modulus2 = bgc_vector3_get_square_modulus_fp64(vector2);
if (square_modulus2 <= BGC_SQUARE_EPSYLON_FP64) {
return 0.0;
}
const double cosine = bgc_vector3_scalar_product_fp64(vector1, vector2) / sqrt(square_modulus1 * square_modulus2);
if (cosine >= 1.0 - BGC_EPSYLON_FP64) {
return 0.0;
}
if (cosine <= -1.0 + BGC_EPSYLON_FP64) {
return bgc_angle_get_half_circle_fp64(unit);
}
return bgc_radians_to_units_fp64(acos(cosine), unit);
}