Skip to content

Instantly share code, notes, and snippets.

@e-orlov
Forked from papoms/Click Coordinates
Created December 18, 2015 16:41
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 e-orlov/733e2a4d27a677987d80 to your computer and use it in GitHub Desktop.
Save e-orlov/733e2a4d27a677987d80 to your computer and use it in GitHub Desktop.
Get Click Coordinates based on a normal distribution
<?php
// Return Random Float between min and max
function randomFloat($min = 0, $max = 1) {
return $min + mt_rand() / mt_getrandmax() * ($max - $min);
}
// returns normally distributed coordinates in a given range (0 to nMax)
function getClickCoordinates(&$x, &$y, $xMax = 100, $yMax = 100){
// Polar-Method to get Normal Distributed coordinates
do {
$u = randomFloat(-1,1);
$v = randomFloat(-1,1);
$q =$u * $u + $v * $v;
} while ($q == 0 || $q >= 1);
$p = sqrt( (-10 * log($q)) / $q);
//Used to normalize numbers between -1 and 1
$boundary = 10;
// |----------------- between -1 and 1 ---------------|
$x = min(max($u * $p, -$boundary), $boundary) / $boundary * $xMax/2 + $xMax/2;
$y = min(max($v * $p, -$boundary), $boundary) / $boundary * $yMax/2 + $yMax/2;
}
// Output some demo variables for visualization
for ($i=0; $i < 10000 ; $i++) {
getClickCoordinates($x, $y, 180, 180);
echo "$x;$y\n";
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment