Example_-_Orientation_PointvsLine.c 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. #include "raylib.h"
  2. // Return on which side of the line the point is. l-1 0= r=1
  3. // lineback x,y linefront x,y point x,y
  4. int orientation(int ax,int ay,int bx, int by, int cx, int cy);
  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. Vector2 line1a=(Vector2){200,100};
  15. Vector2 line1b=(Vector2){100,200};
  16. Vector2 point = (Vector2){300,150};
  17. // Main game loop
  18. while (!WindowShouldClose()) // Detect window close button or ESC key
  19. {
  20. // Update
  21. //----------------------------------------------------------------------------------
  22. // Put the point below the mouse.
  23. point = GetMousePosition();
  24. //----------------------------------------------------------------------------------
  25. // Draw
  26. //----------------------------------------------------------------------------------
  27. BeginDrawing();
  28. ClearBackground(RAYWHITE);
  29. // Here we draw the first line.
  30. DrawLine(line1a.x,line1a.y,line1b.x,line1b.y,RED);
  31. DrawText("Back",line1a.x,line1a.y,10,BLACK);
  32. DrawText("Front",line1b.x,line1b.y,10,BLACK);
  33. // Draw the Point under the mouse.
  34. DrawCircle(point.x,point.y,10,RED);
  35. // Here we check if the mouse if left or right or on the same path of the line.
  36. // line starts at a.xy to b.xy.
  37. // This orientation function could be useful for turrets etc.
  38. int orien = orientation(line1a.x,line1a.y,line1b.x,line1b.y,point.x,point.y);
  39. if(orien==-1)DrawText("left",0,0,20,RED);
  40. if(orien==0)DrawText("Same",0,0,20,RED);
  41. if(orien==1)DrawText("right",0,0,20,RED);
  42. EndDrawing();
  43. //----------------------------------------------------------------------------------
  44. }
  45. // De-Initialization
  46. //--------------------------------------------------------------------------------------
  47. CloseWindow(); // Close window and OpenGL context
  48. //--------------------------------------------------------------------------------------
  49. return 0;
  50. }
  51. //
  52. // This is the orientation function. It returns -1 if the point is left of the inputted line.
  53. // 0 if on the same and 1 if on the right of the line.
  54. // aa,bb,point
  55. int orientation(int ax,int ay,int bx, int by, int cx, int cy){
  56. if(((bx-ax)*(cy-ay)-(by-ay)*(cx-ax))<0)return -1;
  57. if(((bx-ax)*(cy-ay)-(by-ay)*(cx-ax))>0)return 1;
  58. return 0;
  59. }