-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathExample_-_Orientation_PointvsLine.c
77 lines (59 loc) · 2.8 KB
/
Example_-_Orientation_PointvsLine.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include "raylib.h"
// Return on which side of the line the point is. l-1 0= r=1
// lineback x,y linefront x,y point x,y
int orientation(int ax,int ay,int bx, int by, int cx, int cy);
int main(void)
{
// Initialization
//--------------------------------------------------------------------------------------
const int screenWidth = 800;
const int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib example.");
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
//--------------------------------------------------------------------------------------
Vector2 line1a=(Vector2){200,100};
Vector2 line1b=(Vector2){100,200};
Vector2 point = (Vector2){300,150};
// Main game loop
while (!WindowShouldClose()) // Detect window close button or ESC key
{
// Update
//----------------------------------------------------------------------------------
// Put the point below the mouse.
point = GetMousePosition();
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(RAYWHITE);
// Here we draw the first line.
DrawLine(line1a.x,line1a.y,line1b.x,line1b.y,RED);
DrawText("Back",line1a.x,line1a.y,10,BLACK);
DrawText("Front",line1b.x,line1b.y,10,BLACK);
// Draw the Point under the mouse.
DrawCircle(point.x,point.y,10,RED);
// Here we check if the mouse if left or right or on the same path of the line.
// line starts at a.xy to b.xy.
// This orientation function could be useful for turrets etc.
int orien = orientation(line1a.x,line1a.y,line1b.x,line1b.y,point.x,point.y);
if(orien==-1)DrawText("left",0,0,20,RED);
if(orien==0)DrawText("Same",0,0,20,RED);
if(orien==1)DrawText("right",0,0,20,RED);
EndDrawing();
//----------------------------------------------------------------------------------
}
// De-Initialization
//--------------------------------------------------------------------------------------
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
return 0;
}
//
// This is the orientation function. It returns -1 if the point is left of the inputted line.
// 0 if on the same and 1 if on the right of the line.
// aa,bb,point
int orientation(int ax,int ay,int bx, int by, int cx, int cy){
if(((bx-ax)*(cy-ay)-(by-ay)*(cx-ax))<0)return -1;
if(((bx-ax)*(cy-ay)-(by-ay)*(cx-ax))>0)return 1;
return 0;
}