Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

124 linhas
5.0 KiB

  1. /*******************************************************************************************
  2. *
  3. * raylib [shapes] example - Vector Angle
  4. *
  5. * Example complexity rating: [] 2/4
  6. *
  7. * Example originally created with raylib 1.0, last time updated with raylib 4.6
  8. *
  9. * Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
  10. * BSD-like license that allows static linking with closed source software
  11. *
  12. * Copyright (c) 2023-2025 Ramon Santamaria (@raysan5)
  13. *
  14. ********************************************************************************************/
  15. #include "raylib.h"
  16. #include "raymath.h"
  17. //------------------------------------------------------------------------------------
  18. // Program main entry point
  19. //------------------------------------------------------------------------------------
  20. int main(void)
  21. {
  22. // Initialization
  23. //--------------------------------------------------------------------------------------
  24. const int screenWidth = 800;
  25. const int screenHeight = 450;
  26. InitWindow(screenWidth, screenHeight, "raylib [math] example - vector angle");
  27. Vector2 v0 = { screenWidth/2, screenHeight/2 };
  28. Vector2 v1 = Vector2Add(v0, (Vector2){ 100.0f, 80.0f });
  29. Vector2 v2 = { 0 }; // Updated with mouse position
  30. float angle = 0.0f; // Angle in degrees
  31. int angleMode = 0; // 0-Vector2Angle(), 1-Vector2LineAngle()
  32. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  33. //--------------------------------------------------------------------------------------
  34. // Main game loop
  35. while (!WindowShouldClose()) // Detect window close button or ESC key
  36. {
  37. // Update
  38. //----------------------------------------------------------------------------------
  39. float startangle = 0.0f;
  40. if (angleMode == 0) startangle = -Vector2LineAngle(v0, v1)*RAD2DEG;
  41. if (angleMode == 1) startangle = 0.0f;
  42. v2 = GetMousePosition();
  43. if (IsKeyPressed(KEY_SPACE)) angleMode = !angleMode;
  44. if ((angleMode == 0) && IsMouseButtonDown(MOUSE_BUTTON_RIGHT)) v1 = GetMousePosition();
  45. if (angleMode == 0)
  46. {
  47. // Calculate angle between two vectors, considering a common origin (v0)
  48. Vector2 v1Normal = Vector2Normalize(Vector2Subtract(v1, v0));
  49. Vector2 v2Normal = Vector2Normalize(Vector2Subtract(v2, v0));
  50. angle = Vector2Angle(v1Normal, v2Normal)*RAD2DEG;
  51. }
  52. else if (angleMode == 1)
  53. {
  54. // Calculate angle defined by a two vectors line, in reference to horizontal line
  55. angle = Vector2LineAngle(v0, v2)*RAD2DEG;
  56. }
  57. //----------------------------------------------------------------------------------
  58. // Draw
  59. //----------------------------------------------------------------------------------
  60. BeginDrawing();
  61. ClearBackground(RAYWHITE);
  62. if (angleMode == 0)
  63. {
  64. DrawText("MODE 0: Angle between V1 and V2", 10, 10, 20, BLACK);
  65. DrawText("Right Click to Move V2", 10, 30, 20, DARKGRAY);
  66. DrawLineEx(v0, v1, 2.0f, BLACK);
  67. DrawLineEx(v0, v2, 2.0f, RED);
  68. DrawCircleSector(v0, 40.0f, startangle, startangle + angle, 32, Fade(GREEN, 0.6f));
  69. }
  70. else if (angleMode == 1)
  71. {
  72. DrawText("MODE 1: Angle formed by line V1 to V2", 10, 10, 20, BLACK);
  73. DrawLine(0, screenHeight/2, screenWidth, screenHeight/2, LIGHTGRAY);
  74. DrawLineEx(v0, v2, 2.0f, RED);
  75. DrawCircleSector(v0, 40.0f, startangle, startangle - angle, 32, Fade(GREEN, 0.6f));
  76. }
  77. DrawText("v0", v0.x, v0.y, 10, DARKGRAY);
  78. // If the line from v0 to v1 would overlap the text, move it's position up 10
  79. if (angleMode == 0 && Vector2Subtract(v0, v1).y > 0.0f) DrawText("v1", v1.x, v1.y-10.0f, 10, DARKGRAY);
  80. if (angleMode == 0 && Vector2Subtract(v0, v1).y < 0.0f) DrawText("v1", v1.x, v1.y, 10, DARKGRAY);
  81. // If angle mode 1, use v1 to emphasize the horizontal line
  82. if (angleMode == 1) DrawText("v1", v0.x + 40.0f, v0.y, 10, DARKGRAY);
  83. // position adjusted by -10 so it isn't hidden by cursor
  84. DrawText("v2", v2.x-10.0f, v2.y-10.0f, 10, DARKGRAY);
  85. DrawText("Press SPACE to change MODE", 460, 10, 20, DARKGRAY);
  86. DrawText(TextFormat("ANGLE: %2.2f", angle), 10, 70, 20, LIME);
  87. EndDrawing();
  88. //----------------------------------------------------------------------------------
  89. }
  90. // De-Initialization
  91. //--------------------------------------------------------------------------------------
  92. CloseWindow(); // Close window and OpenGL context
  93. //--------------------------------------------------------------------------------------
  94. return 0;
  95. }