Skip to content

Instantly share code, notes, and snippets.

@jeremiemartinez
Forked from mathieujaumain/gist:4948485
Created February 13, 2013 22:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeremiemartinez/4949069 to your computer and use it in GitHub Desktop.
Save jeremiemartinez/4949069 to your computer and use it in GitHub Desktop.
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