-
Notifications
You must be signed in to change notification settings - Fork 982
/
SpeechBalloonHelper.java
89 lines (77 loc) · 3.45 KB
/
SpeechBalloonHelper.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
82
83
84
85
86
87
88
89
package org.osmdroid.util;
import org.osmdroid.views.overlay.SpeechBalloonOverlay;
/**
* Helper class for {@link SpeechBalloonOverlay}
* whose main interest is method {@link #compute(RectL, PointL, double, PointL, PointL)}
*
* @author Fabrice Fontaine
* @since 6.1.1
*/
public class SpeechBalloonHelper {
public static final int CORNER_INSIDE = -1;
public static final int CORNER_NONE = 0;
public static final int CORNER_LEFT = 1;
public static final int CORNER_RIGHT = 2;
public static final int CORNER_TOP = 4;
public static final int CORNER_BOTTOM = 8;
private final PointL mTrianglePoint = new PointL();
private RectL mRect;
private PointL mPoint;
/**
* Computes the intersection between a rectangle and the triangle that starts from a Point
* and goes to a circle centered on the rectangle's center
*
* @return CORNER_INSIDE if the Point is within the rectangle, CORNER_NONE if both intersections
* are on the same border, or a combination of CORNER_% that tells which rectangle's corner is
* included between both intersections
*/
public int compute(final RectL pInputRect, final PointL pInputPoint, final double pInputRadius,
final PointL pOutputIntersection1, final PointL pOutputIntersection2) {
mRect = pInputRect;
mPoint = pInputPoint;
if (pInputRect.contains(mPoint.x, mPoint.y)) {
return CORNER_INSIDE;
}
final double angle = MyMath.computeAngle(mRect.centerX(), mRect.centerY(), mPoint.x, mPoint.y);
computeCirclePoint(mTrianglePoint, pInputRadius, angle, false);
final int corner1 = checkIntersection(pOutputIntersection1);
computeCirclePoint(mTrianglePoint, pInputRadius, angle, true);
final int corner2 = checkIntersection(pOutputIntersection2);
if (corner1 == corner2) {
return CORNER_NONE;
}
return corner1 | corner2;
}
private int checkIntersection(final PointL pIntersection) {
if (mPoint.y <= mRect.top && checkIntersectionY(mRect.top, pIntersection)) {
return CORNER_TOP;
}
if (mPoint.y >= mRect.bottom && checkIntersectionY(mRect.bottom, pIntersection)) {
return CORNER_BOTTOM;
}
if (mPoint.x <= mRect.left && checkIntersectionX(mRect.left, pIntersection)) {
return CORNER_LEFT;
}
if (mPoint.x >= mRect.right && checkIntersectionX(mRect.right, pIntersection)) {
return CORNER_RIGHT;
}
throw new IllegalArgumentException();
}
private boolean checkIntersectionX(final long pX, final PointL pIntersection) {
return SegmentIntersection.intersection(
mPoint.x, mPoint.y, mTrianglePoint.x, mTrianglePoint.y,
pX, mRect.top, pX, mRect.bottom,
pIntersection);
}
private boolean checkIntersectionY(final long pY, final PointL pIntersection) {
return SegmentIntersection.intersection(
mPoint.x, mPoint.y, mTrianglePoint.x, mTrianglePoint.y,
mRect.left, pY, mRect.right, pY,
pIntersection);
}
private void computeCirclePoint(final PointL pDestination, final double pRadius,
final double pAngle, final boolean pFirst) {
MyMath.computeCirclePoint(mRect.centerX(), mRect.centerY(), pRadius,
pAngle + Math.PI / 2 * (pFirst ? 1 : -1), pDestination);
}
}