65 lines
1.9 KiB
C
65 lines
1.9 KiB
C
#include "vector2.h"
|
|
|
|
// =================== Angle ==================== //
|
|
|
|
float bgc_vector2_get_angle_fp32(const bgc_vector2_fp32_t* vector1, const bgc_vector2_fp32_t* vector2, const bgc_angle_unit_t unit)
|
|
{
|
|
if (vector1 == 0 || vector2 == 0) {
|
|
return 0.0f;
|
|
}
|
|
|
|
const float square_modulus1 = bgc_vector2_get_square_modulus_fp32(vector1);
|
|
|
|
if (square_modulus1 <= BGC_SQUARE_EPSYLON_FP32) {
|
|
return 0.0f;
|
|
}
|
|
|
|
const float square_modulus2 = bgc_vector2_get_square_modulus_fp32(vector2);
|
|
|
|
if (square_modulus2 <= BGC_SQUARE_EPSYLON_FP32) {
|
|
return 0.0f;
|
|
}
|
|
|
|
const float cosine = bgc_vector2_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_vector2_get_angle_fp64(const bgc_vector2_fp64_t* vector1, const bgc_vector2_fp64_t* vector2, const bgc_angle_unit_t unit)
|
|
{
|
|
if (vector1 == 0 || vector2 == 0) {
|
|
return 0.0;
|
|
}
|
|
|
|
const double square_modulus1 = bgc_vector2_get_square_modulus_fp64(vector1);
|
|
|
|
if (square_modulus1 <= BGC_SQUARE_EPSYLON_FP64) {
|
|
return 0.0;
|
|
}
|
|
|
|
const double square_modulus2 = bgc_vector2_get_square_modulus_fp64(vector2);
|
|
|
|
if (square_modulus2 <= BGC_SQUARE_EPSYLON_FP64) {
|
|
return 0.0;
|
|
}
|
|
|
|
const double cosine = bgc_vector2_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);
|
|
}
|