Suppose the bounding box for the graph fills the view accurately. We can act as follows. The field of interest is limited by two concentric circles and in appearance. (The center of the circle is the lower right angle of view.) When we get a touch, we just need to calculate the distance from the touch coordinate to the lower right corner and compare this with the two radii of the circle. (You can avoid the square root by comparing the square of the distance with the square of the radii.) If the distance falls between the radii, the touch is in blue (or any other color). We do not need to calculate whether the touch is in the bounding box; we already know that since the event was delivered to the view.
Here is a sample code. This is for a detector that determines whether a point lies within two concentric circles (annular space) and, if so, in which quadrant it hit.
public class ArcHitDetector { public enum Quadrant { TOP_LEFT, TOP_RIGHT, BOTTOM_LEFT, BOTTOM_RIGHT } private int xCenter, yCenter, innerR2, outerR2; public ArcHitDetector(int xCenter, int yCenter, int outerR, int innerR) { this.xCenter = xCenter; this.yCenter = yCenter; this.outerR2 = outerR * outerR; this.innerR2 = innerR * innerR; } public Quadrant classifyHit(int x, int y) { int dx = x - xCenter; int dy = y - yCenter; int d2 = dx * dx + dy * dy; if (d2 <= outerR2 && d2 >= innerR2) { if (x >= xCenter) { return y <= yCenter ? TOP_RIGHT : BOTTOM_RIGHT; } else { return y <= yCenter ? TOP_LEFT : BOTTOM_LEFT; } } else { return null; } } }
Ted hopp
source share