Skip to content

Commit

Permalink
Implemented Vector3's cross product
Browse files Browse the repository at this point in the history
  • Loading branch information
iWas-Coder committed Nov 6, 2024
1 parent f2c5d6f commit 173c488
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
1 change: 1 addition & 0 deletions carbon.h
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ CARBON_API CBN_Vec2 carbon_math_vec2_sub(CBN_Vec2 u, CBN_Vec2 v);
CARBON_API CBN_Vec3 carbon_math_vec3_sub(CBN_Vec3 u, CBN_Vec3 v);
CARBON_API f32 carbon_math_vec2_dot(CBN_Vec2 u, CBN_Vec2 v);
CARBON_API f32 carbon_math_vec3_dot(CBN_Vec3 u, CBN_Vec3 v);
CARBON_API CBN_Vec3 carbon_math_vec3_cross(CBN_Vec3 u, CBN_Vec3 v);
CARBON_API CBN_Vec2 carbon_math_vec2_rotate(CBN_Vec2 v, f32 angle);

#ifdef __cplusplus
Expand Down
8 changes: 8 additions & 0 deletions src/carbon_math.c
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,14 @@ f32 carbon_math_vec3_dot(CBN_Vec3 u, CBN_Vec3 v) {
return (u.x * v.x) + (u.y * v.y) + (u.z * v.z);
}

CBN_Vec3 carbon_math_vec3_cross(CBN_Vec3 u, CBN_Vec3 v) {
return (CBN_Vec3) {
.x = (u.y * v.z) - (u.z * v.y),
.y = (u.z * v.x) - (u.x * v.z),
.z = (u.x * v.y) - (u.y * v.x)
};
}

CBN_Vec2 carbon_math_vec2_rotate(CBN_Vec2 v, f32 angle) {
f32 rads = angle * (CARBON_PI / 180);
f32 c = cosf(rads), s = sinf(rads);
Expand Down
10 changes: 10 additions & 0 deletions test/carbon_math_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,13 @@ CARBON_TEST(carbon_math, vec3_dot) {
carbon_should_be_f(32, carbon_math_vec3_dot(u, v));
return CARBON_OK;
}

CARBON_TEST(carbon_math, vec3_cross) {
CBN_Vec3 u = {{3, -3, 1}};
CBN_Vec3 v = {{4, 9, 2}};
CBN_Vec3 r = carbon_math_vec3_cross(u, v);
carbon_should_be_f(-15, r.x);
carbon_should_be_f(-2, r.y);
carbon_should_be_f(39, r.z);
return CARBON_OK;
}

0 comments on commit 173c488

Please sign in to comment.