Functions_-_distance_EuclMan.c 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #include "raylib.h"
  2. #include <math.h>
  3. static float distance(float x1,float y1,float x2,float y2);
  4. static float edistance(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 = 400;
  22. int y2 = 200;
  23. //----------------------------------------------------------------------------------
  24. // Draw
  25. //----------------------------------------------------------------------------------
  26. BeginDrawing();
  27. ClearBackground(RAYWHITE);
  28. DrawLine(x1,y1,x2,y2,RED);
  29. DrawText(FormatText("Manhattan distance : %f ",distance(x1,y1,x2,y2)),0,0,20,DARKGRAY);
  30. DrawText(FormatText("Euclidean distance : %f ",edistance(x1,y1,x2,y2)),0,20,20,DARKGRAY);
  31. EndDrawing();
  32. //----------------------------------------------------------------------------------
  33. }
  34. // De-Initialization
  35. //--------------------------------------------------------------------------------------
  36. CloseWindow(); // Close window and OpenGL context
  37. //--------------------------------------------------------------------------------------
  38. return 0;
  39. }
  40. // Manhattan Distance (less precise)
  41. float distance(float x1,float y1,float x2,float y2){
  42. return (float)abs(x2-x1)+abs(y2-y1);
  43. }
  44. // Euclidean distance (more precise)
  45. float edistance(float x1,float y1,float x2,float y2){
  46. return sqrt( (x1-x2)*(x1-x2)+(y1-y2)*(y1-y2) );
  47. }