/
PointCircle.pde
69 lines (49 loc) · 1.19 KB
/
PointCircle.pde
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
/*
POINT/CIRCLE
Jeff Thompson | 2015 | www.jeffreythompson.org
*/
float px = 0; // point position
float py = 0;
float cx = 300; // circle center position
float cy = 200;
float radius = 100; // circle's radius
void setup() {
size(600,400);
noCursor();
strokeWeight(5); // thicker stroke so points are easier to see
}
void draw() {
background(255);
// update point position to mouse coordinates
px = mouseX;
py = mouseY;
// check for collision!
boolean hit = pointCircle(px,py, cx,cy, radius);
// draw circle
// change fill color if hit
if (hit) {
fill(255,150,0);
}
else {
fill(0,150,255);
}
noStroke();
ellipse(cx,cy, radius*2,radius*2);
// draw the point
stroke(0);
point(px, py);
}
// POINT/CIRCLE
boolean pointCircle(float px, float py, float cx, float cy, float r) {
// get distance between the point and circle's center
// using the Pythagorean Theorem
float distX = px - cx;
float distY = py - cy;
float distance = sqrt( (distX*distX) + (distY*distY) );
// if the distance is less than the circle's
// radius the point is inside!
if (distance <= r) {
return true;
}
return false;
}