-
Notifications
You must be signed in to change notification settings - Fork 0
/
MathFunction.java
81 lines (56 loc) · 2.43 KB
/
MathFunction.java
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
76
77
78
79
80
81
package treamcode;
import org.opencv.core.Point;
import java.util.ArrayList;
import static java.lang.Math.*;
public class MathFunction {
public static double AngleWrap(double angle){
while (angle < -PI){
angle += 2* PI;
}
while (angle > PI){
angle -= 2* PI;
}
return angle;
}
public static ArrayList<Point> lineCircleIntersection(Point circleCenter, double radius,
Point linePoint1, Point linePoint2){
if(Math.abs(linePoint1.y - linePoint2.y) < 0.003){
linePoint1.y = linePoint2.y + 0.003;
}
if(Math.abs(linePoint1.x - linePoint2.x) < 0.003){
linePoint1.x = linePoint2.x + 0.003;
}
double m1 = (linePoint2.y - linePoint1.y)/(linePoint2.x - linePoint1.x);
double c = Math.sin(m1);
double quadraticA = 1.0 + pow(m1,2);
double x1 = linePoint1.x - circleCenter.x;
double y1 = linePoint1.y - circleCenter.y;
double quadraticB = (2.0 * m1 * y1) - (2.0 * pow(m1,2) * x1);
double quadraticC = ((pow(m1,2) * pow(x1,2))) - (2.0*y1*m1*x1) + pow(y1,2) - pow(radius,2);
ArrayList<Point> allPoints = new ArrayList<>();
try{
double xRoot1 = (-quadraticB + sqrt(pow(quadraticB,2) - (4.0 * quadraticA * quadraticC)))/(2.0*quadraticA);
double yRoot1 = m1 * (xRoot1 - x1) + y1;
//put back the offset
xRoot1 += circleCenter.x;
yRoot1 += circleCenter.y;
double minX = linePoint1.x < linePoint2.x ? linePoint1.x : linePoint2.x;
double maxX = linePoint1.x > linePoint2.x ? linePoint1.x : linePoint2.x;
if(xRoot1 > minX && xRoot1 < maxX){
allPoints.add(new Point(xRoot1,yRoot1));
}
double xRoot2 = (-quadraticB - sqrt(pow(quadraticB,2) - (4.0 * quadraticA * quadraticC)))/(2.0*quadraticA);
double yRoot2 = m1 * (xRoot2 - x1) + y1;
xRoot2 += circleCenter.x;
yRoot2 += circleCenter.y;
if(xRoot1 > minX && xRoot1 < maxX){
allPoints.add(new Point(xRoot2,yRoot2));
}
}catch(Exception e){
}
return allPoints;
}
public static double getHeading(Point a, Point b){
return (b.y-a.y)/(b.x-a.x);
}
}