2
0

Functions_-_getangle.c 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #include "raylib.h"
  2. #include <math.h>
  3. // Get our angle between two points.
  4. static float getangle(float x1,float y1,float x2,float y2);
  5. int main(void)
  6. {
  7. // Initialization
  8. //--------------------------------------------------------------------------------------
  9. const int screenWidth = 800;
  10. const int screenHeight = 450;
  11. InitWindow(screenWidth, screenHeight, "raylib example.");
  12. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  13. //--------------------------------------------------------------------------------------
  14. // Main game loop
  15. while (!WindowShouldClose()) // Detect window close button or ESC key
  16. {
  17. // Update
  18. //----------------------------------------------------------------------------------
  19. int x1 = GetMouseX();
  20. int y1 = GetMouseY();
  21. int x2 = screenWidth/2;
  22. int y2 = screenHeight/2;
  23. //----------------------------------------------------------------------------------
  24. // Draw
  25. //----------------------------------------------------------------------------------
  26. BeginDrawing();
  27. ClearBackground(RAYWHITE);
  28. DrawLine(x1,y1,x2,y2,RED);
  29. DrawText(FormatText("Angle between x1,y1,x2,y2 : %f",getangle(x1,y1,x2,y2)),0,0,20,DARKGRAY);
  30. EndDrawing();
  31. //----------------------------------------------------------------------------------
  32. }
  33. // De-Initialization
  34. //--------------------------------------------------------------------------------------
  35. CloseWindow(); // Close window and OpenGL context
  36. //--------------------------------------------------------------------------------------
  37. return 0;
  38. }
  39. // Return the angle from - to in float
  40. float getangle(float x1,float y1,float x2,float y2){
  41. return (float)atan2(y2-y1, x2-x1);
  42. }