-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTarget.pde
70 lines (57 loc) · 1.48 KB
/
Target.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
67
68
69
70
class Target {
float x, y, radius;
int colour;
Target(float x, float y, float radius) {
this.x = x;
this.y = y;
this.radius = radius;
this.colour = color(255);
}
void display() {
fill(colour);
stroke(0);
ellipse(x, y, radius * 2, radius * 2);
}
float getX() {
return x;
}
float getY() {
return y;
}
float getRadius() {
return radius;
}
void setColor(int colour) {
this.colour = colour;
}
boolean isClicked(float px, float py) {
return dist(px, py, x, y) <= radius;
}
float intersectionLength(float x1, float y1, float x2, float y2) {
float dx = x2 - x1;
float dy = y2 - y1;
float fx = x1 - x;
float fy = y1 - y;
float a = dx * dx + dy * dy;
float b = 2 * (fx * dx + fy * dy);
float c = (fx * fx + fy * fy) - radius * radius;
float discriminant = b * b - 4 * a * c;
if (discriminant < 0) {
return 0; // No intersection
} else {
discriminant = sqrt(discriminant);
float t1 = (-b - discriminant) / (2 * a);
float t2 = (-b + discriminant) / (2 * a);
if ((t1 < 0 && t2 < 0) || (t1 > 1 && t2 > 1)) return 0;
// Clamp the intersection points to the segment limits
t1 = max(0, min(t1, 1));
t2 = max(0, min(t2, 1));
// Calculate the intersection points
float inX1 = x1 + t1 * dx;
float inY1 = y1 + t1 * dy;
float inX2 = x1 + t2 * dx;
float inY2 = y1 + t2 * dy;
return dist(inX1, inY1, inX2, inY2);
}
}
}