Last active
December 13, 2015 17:29
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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