-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGeometry.java
More file actions
58 lines (52 loc) · 1.86 KB
/
Geometry.java
File metadata and controls
58 lines (52 loc) · 1.86 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
/**
* Geometry helper methods
*
* @author Chris Bailey-Kellogg, Dartmouth CS 10, Spring 2015
* @author CBK, Fall 2016, separated from quadtree, instrumented to count calls
*
*/
public class Geometry {
private static int numInCircleTests = 0; // keeps track of how many times pointInCircle has been called
private static int numCircleRectangleTests = 0; // keeps track of how many times circleIntersectsRectangle has been called
public static int getNumInCircleTests() {
return numInCircleTests;
}
public static void resetNumInCircleTests() {
numInCircleTests = 0;
}
public static int getNumCircleRectangleTests() {
return numCircleRectangleTests;
}
public static void resetNumCircleRectangleTests() {
numCircleRectangleTests = 0;
}
/**
* Returns whether or not the point is within the circle
* @param px point x coord
* @param py point y coord
* @param cx circle center x
* @param cy circle center y
* @param cr circle radius
*/
public static boolean pointInCircle(double px, double py, double cx, double cy, double cr) {
numInCircleTests++;
return (px-cx)*(px-cx) + (py-cy)*(py-cy) <= cr*cr;
}
/**
* Returns whether or not the circle intersects the rectangle
* Based on discussion at http://stackoverflow.com/questions/401847/circle-rectangle-collision-detection-intersection
* @param cx circle center x
* @param cy circle center y
* @param cr circle radius
* @param x1 rectangle min x
* @param y1 rectangle min y
* @param x2 rectangle max x
* @param y2 rectangle max y
*/
public static boolean circleIntersectsRectangle(double cx, double cy, double cr, double x1, double y1, double x2, double y2) {
numCircleRectangleTests++;
double closestX = Math.min(Math.max(cx, x1), x2);
double closestY = Math.min(Math.max(cy, y1), y2);
return (cx-closestX)*(cx-closestX) + (cy-closestY)*(cy-closestY) <= cr*cr;
}
}