Skip to content

Instantly share code, notes, and snippets.

@mathieujaumain
Last active December 13, 2015 17:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mathieujaumain/4948485 to your computer and use it in GitHub Desktop.
Save mathieujaumain/4948485 to your computer and use it in GitHub Desktop.
Method to get the circle object {x,y,radius} whose center is the nearest to the MotionEvent among those that are selected by said MotionEvent. Implement Euclidian distances for a greater precision.
public Circle getCircleAtPosition(MotionEvent event) {
// List des distances des cercles candidats
List<Float> distanceList = new ArrayList<Float>();
// Listes des cercles candidats
List<Circle> candidateList = new ArrayList<Circle>();
int bestcandidate = 0;
for (Circle c : circles) {
// distance euclidienne
float distance = (float) Math.sqrt(Math.pow(
Math.abs(c.x - event.getX()), 2)
+ Math.pow(Math.abs(c.y - event.getY()), 2));
if (distance < c.radius) {
distanceList.add(distance);
candidateList.add(c);
}
}
// selection du cercle le plus probable
if (!candidateList.isEmpty()) {
int counter = distanceList.size();
int i;
for (i = 1; i < counter; i++) {
if (distanceList.get(i) - distanceList.get(bestcandidate) > 0) {
bestcandidate = i;
}
}
return circles.get(bestcandidate);
} else {
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment