Skip to content

Instantly share code, notes, and snippets.

@fi-tomek-augustyn
Last active December 10, 2015 22:28
Show Gist options
  • Save fi-tomek-augustyn/4502328 to your computer and use it in GitHub Desktop.
Save fi-tomek-augustyn/4502328 to your computer and use it in GitHub Desktop.
Mapping touch points to rings on stage.
/**
* Map the ring nearest to touch point
* @param {number} id touch point id
* @param {number} x touch point x
* @param {number} y touch point y
*/
function mapNearestRing(id, x, y) {
var minDistance = 1000000;
var nearestRing = null;
var SNAPPING_DISTANCE = 70;
for (var i = 0, numRings = rings.length; i < numRings; i++) {
var ring = rings[i];
var dx = x - ring.x;
var dy = y - ring.y;
var distance = Math.sqrt(dx * dx + dy * dy);
if (distance < minDistance) {
minDistance = distance;
if (distance <= SNAPPING_DISTANCE) {
nearestRing = ring;
}
}
}
if (nearestRing != null) {
return nearestRing;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment