-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRectanglePoint.c
More file actions
64 lines (51 loc) · 1.35 KB
/
RectanglePoint.c
File metadata and controls
64 lines (51 loc) · 1.35 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
#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)));
}
// POINT/RECTANGLE
bool pointRect(float px, float py, float rx, float ry, float rw, float rh)
{
// is the point inside the rectangle's bounds?
if (px >= rx && // right of the left edge AND
px <= rx + rw && // left of the right edge AND
py >= ry && // below the top AND
py <= ry + rh)
{ // above the bottom
return true;
}
return false;
}
int main(void)
{
float px = 0; // point position (move with mouse)
float py = 0;
float sx = 200; // square position
float sy = 100;
float sw = 200; // and dimensions
float sh = 200;
Vector2 Startpos;
Vector2 Endpos;
InitWindow(640, 400, "Rectangle / Point");
while (!WindowShouldClose())
{
ClearBackground(WHITE);
BeginDrawing();
px = GetMouseX();
py = GetMouseY();
// Check for collision if hit, change the color of the rectangle
bool hit = pointRect(px, py, sx, sy, sw, sh);
if (hit)
{
DrawRectangle(sx, sy, sw, sh, RED);
}
else
{
DrawRectangle(sx, sy, sw, sh, SKYBLUE);
}
EndDrawing();
}
CloseWindow();
return 0;
}