-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCircleRectangle.c
More file actions
75 lines (61 loc) · 1.66 KB
/
Copy pathCircleRectangle.c
File metadata and controls
75 lines (61 loc) · 1.66 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"
// CIRCLE/RECTANGLE
bool circleRect(float cx, float cy, float radius, float rx, float ry, float rw, float rh)
{
// Temporary variables to set edges for testing
float testX = cx;
float testY = cy;
// Which edge is closest?
if (cx < rx)
testX = rx; // Test left edge
else if (cx > rx + rw)
testX = rx + rw; // Right edge
if (cy < ry)
testY = ry; // Top edge
else if (cy > ry + rh)
testY = ry + rh; // Bottom edge
// Get distance from closest edges
float distX = cx - testX;
float distY = cy - testY;
float distance = sqrt((distX * distX) + (distY * distY));
// If the distance is less than the radius, collision!
if (distance <= radius)
{
return true;
}
return false;
}
int main(void)
{
float cx = 0; // Circle position (set with mouse)
float cy = 0;
float r = 30; // Circle radius
float sx = 200; // Square position
float sy = 100;
float sw = 200; // and dimensions
float sh = 200;
InitWindow(640, 400, "Circle / Rectangle");
while (!WindowShouldClose())
{
ClearBackground(WHITE);
// Update square to mouse coordinates
cx = GetMouseX();
cy = GetMouseY();
// Check for collision if hit, change rectangle color.
bool hit = circleRect(cx, cy, r, sx, sy, sw, sh);
if (hit)
{
DrawRectangle(sx, sy, sw, sh, RED);
}
else
{
DrawRectangle(sx, sy, sw, sh, SKYBLUE);
}
// Draw the circle
DrawCircle(cx, cy, r, GRAY);
EndDrawing();
}
CloseWindow();
return 0;
}