Last active
December 10, 2015 22:28
-
-
Save fi-tomek-augustyn/4502328 to your computer and use it in GitHub Desktop.
Mapping touch points to rings on stage.
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
/** | |
* 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