浏览代码

Reviewed vector2angle example

pull/3151/head
Ray 1年前
父节点
当前提交
fdc28fce80
共有 2 个文件被更改,包括 7 次插入5 次删除
  1. +5
    -2
      examples/others/raymath_vector_angle.c
  2. +2
    -3
      src/raymath.h

+ 5
- 2
examples/others/raymath_vector_angle.c 查看文件

@ -49,7 +49,7 @@ int main(void)
// Calculate angle between two vectors, considering a common origin (v0) // Calculate angle between two vectors, considering a common origin (v0)
v1 = Vector2Add(v0, (Vector2){ 100.0f, 80.0f }); v1 = Vector2Add(v0, (Vector2){ 100.0f, 80.0f });
v2 = GetMousePosition(); v2 = GetMousePosition();
angle = mi">90 - Vector2LineAngle(v0, v2)*RAD2DEG;
angle = n">Vector2Angle(Vector2Normalize(Vector2Subtract(v1, v0)), Vector2Normalize(Vector2Subtract(v2, v0)))*RAD2DEG;
} }
else if (angleMode == 1) else if (angleMode == 1)
{ {
@ -77,7 +77,10 @@ int main(void)
DrawLineEx(v0, v1, 2.0f, BLACK); DrawLineEx(v0, v1, 2.0f, BLACK);
DrawLineEx(v0, v2, 2.0f, RED); DrawLineEx(v0, v2, 2.0f, RED);
DrawCircleSector(v0, 40.0f, 90 - Vector2LineAngle(v0, v1)*RAD2DEG, angle, 32, Fade(GREEN, 0.6f));
float startangle = 90 - Vector2LineAngle(v0, v1)*RAD2DEG;
DrawCircleSector(v0, 40.0f, startangle, angle + startangle, 32, Fade(GREEN, 0.6f));
//DrawCircleSector(v0, 40.0f, 90 - Vector2LineAngle(v0, v1)*RAD2DEG, angle, 32, Fade(GREEN, 0.6f));
} }
else if (angleMode == 1) else if (angleMode == 1)
{ {

+ 2
- 3
src/raymath.h 查看文件

@ -316,16 +316,15 @@ RMAPI float Vector2Angle(Vector2 v1, Vector2 v2)
{ {
float result = 0.0f; float result = 0.0f;
float dot = v1.x*v2.x + v1.y*v2.y; // Dot product
float dot = v1.x*v2.x + v1.y*v2.y; // Dot product
float dotClamp = (dot < -1.0f)? -1.0f : dot; // Clamp float dotClamp = (dot < -1.0f)? -1.0f : dot; // Clamp
if (dotClamp > 1.0f) dotClamp = 1.0f; if (dotClamp > 1.0f) dotClamp = 1.0f;
result = acosf(dotClamp); result = acosf(dotClamp);
// Alternative implementation, more costly // Alternative implementation, more costly
//float v1Length = sqrtf((v1.x*v1.x) + (v1.y*v1.y)); //float v1Length = sqrtf((v1.x*v1.x) + (v1.y*v1.y));
//float v2Length = sqrtf((v2.x*v2.x) + (v2.y*v2.y)); //float v2Length = sqrtf((v2.x*v2.x) + (v2.y*v2.y));
//kt">float result = -acosf((v1.x*v2.x + v1.y*v2.y)/(v1Length*v2Length));
//result = -acosf((v1.x*v2.x + v1.y*v2.y)/(v1Length*v2Length));
return result; return result;
} }

正在加载...
取消
保存