Não pode escolher mais do que 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.

122 linhas
4.9 KiB

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