Repository navigation
Expand file tree
/
Copy pathLinePoint.c
More file actions
75 lines (61 loc) · 1.56 KB
/
Copy pathLinePoint.c
File metadata and controls
75 lines (61 loc) · 1.56 KB
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
#include <math.h>
#include "raylib.h"
float distance(float x1, float y1, float x2, float y2)
{
return sqrt(((y2 - y1) * (y2 - y1)) + ((x2 - x1) * (x2 - x1)));
}
// LINE / POINT
bool linePoint(float x1, float y1, float x2, float y2, float px, float py)
{
// Get distance from the point to the two ends of the line
float d1 = distance(px, py, x1, y1);
float d2 = distance(px, py, x2, y2);
// Get the length of the line
float lineLen = distance(x1, y1, x2, y2);
// Since floats are so minutely accurate, add a little buffer zone that will give collision
float buffer = 0.1; // higher # = less accurate
// If the two distances are equal to the line's length, the point is on the line!
// Note we use the buffer here to give a range, rather than one #
if (d1 + d2 >= lineLen - buffer && d1 + d2 <= lineLen + buffer)
{
return true;
}
return false;
}
int main(void)
{
float x1 = 100; // Line defined by two points
float y1 = 300;
float x2 = 500;
float y2 = 100;
float px = 0;
float py = 0;
Vector2 Startpos;
Vector2 Endpos;
InitWindow(640, 400, "Line / Point");
while (!WindowShouldClose())
{
ClearBackground(WHITE);
BeginDrawing();
px = GetMouseX();
py = GetMouseY();
// Used for drawing the line
Startpos.x = x1;
Startpos.y = y1;
Endpos.x = x2;
Endpos.y = y2;
// Check for collision if hit, change the color of the line
bool hit = linePoint(x1, y1, x2, y2, px, py);
if (hit)
{
DrawLineEx(Startpos, Endpos, 4, RED);
}
else
{
DrawLineEx(Startpos, Endpos, 3, SKYBLUE);
}
EndDrawing();
}
CloseWindow();
return 0;
}