function namePrefix ($number
{
//Similar scrip in jquery https://jsfiddle.net/ayn_/z2677vs5/
$currentNum = $number
$currentNum = preg_replace'{/s|,|$|h}', '', $currentNum);
if ($currentNum > 999 && $currentNum < 999999) {
$newNum = ($currentNum / 1000);
$newNum = floor$newNum*100)/100 . 'k'
} else if ($currentNum > 999999) {
$newNum = ($currentNum / 1000000);
$newNum = floor$newNum*100)/100 . 'M'
} else {
$newNum = 'Error, number format is invalid'
}
return $newNum
}
Tag: Prime
eXorithm – Execute Algorithm: View / Run Algorithm invert_color
function invert_color ($color
{
$new = ''
for ($i=0;$i<3;$i++){
$c = 255 - hexdecsubstr$color,(2*$i),2));
$c = dechex$c);
$new .= (strlen$c) < 2) ? '0'$c : $c
}
return $new
}
eXorithm – Execute Algorithm: View / Run Algorithm radioactive_decay
function radioactive_decay ($half_life, $time
{
$lambda = log(2)/$half_life
return 100 - 100 * powexp(1), -1 * $lambda * $time);
}
eXorithm – Execute Algorithm: View / Run Algorithm pixelate
function pixelate ($image, $blocksize
{
// Based on http://www.tuxradar.com/practicalphp/11/2/24
$imagex = imagesx$image);
$imagey = imagesy$image);
$image2 = imagecreatetruecolor$imagex, $imagey);
imagesavealpha$image2, true);
imagealphablending$image2, false);
if ($blocksize<1) {
$blocksize = 1;
}
for ($x = 0; $x < $imagex; $x += $blocksize) {
for ($y = 0; $y < $imagey; $y += $blocksize) {
// get the pixel colour at the top-left of the square
$colour = imagecolorat$image, $x, $y);
// set the new red, green, blue and alpha values to 0
$newr = 0;
$newg = 0;
$newb = 0;
$newt = 0;
// create an empty array for the colours
$colours = array();
// cycle through each pixel in the block
for ($k = $x; $k < $x + $blocksize; ++$k) {
for ($l = $y; $l < $y + $blocksize; ++$l) {
// if we are outside the valid bounds of the image, use a safe colour
if ($k < 0) { $colours[] = $colour; continue; }
if ($k >= $imagex) { $colours[] = $colour; continue; }
if ($l < 0) { $colours[] = $colour; continue; }
if ($l >= $imagey) { $colours[] = $colour; continue; }
// if not outside the image bounds, get the colour at this pixel
$colours[] = imagecolorat$image, $k, $l);
}
}
// cycle through all the colours we can use for sampling
foreach$colours as $colour) {
$colour = imagecolorsforindex$image, $colour);
// add their red, green, and blue values to our master numbers
$newr += $colour'red'];
$newg += $colour'green'];
$newb += $colour'blue'];
$newt += $colour'alpha'];
}
// now divide the master numbers by the number of valid samples to get an average
$numelements = count$colours);
$newr /= $numelements
$newg /= $numelements
$newb /= $numelements
$newt /= $numelements
// and use the new numbers as our colour
$newcol = imagecolorallocatealpha$image2, $newr, $newg, $newb, $newt);
imagefilledrectangle$image2, $x, $y, $x + $blocksize - 1, $y + $blocksize - 1, $newcol);
}
}
return $image2
}
eXorithm – Execute Algorithm: View / Run Algorithm quick_sort
function quick_sort ($array
{
if (count$array)<=1) {
return $array
} else {
$pivot = $array[0];
$lesser = array();
$greater = array();
for ($i=1;$icount$array);$i++) {
if ($array$i]<=$pivot) {
$lesser[] = $array$i];
} else {
$greater[] = $array$i];
}
}
return array_mergequick_sort$lesser), array$pivot), quick_sort$greater));
}
}
eXorithm – Execute Algorithm: View / Run Algorithm lcm
function lcm ($a, $b
{
return abs$a$b)/gcd_euclid$a, $b);
}
eXorithm – Execute Algorithm: View / Run Algorithm random_name_caption
function random_name_caption ($image, $your_name
{
$sizey = imagesy$image);
$sizex = imagesx$image);
if (mt_rand(0,1)==1)
$color = imagecolorallocate$image, 254, 0, 0);
else
$color = imagecolorallocate$image, 0, 0, 254);
$y = mt_rand(0,$sizey-10);
$x = mt_rand(0,$sizex-10);
imagestring$image, 4, $x, $y, $your_name, $color);
$return = array
'return' => $image
'arguments' => array$image, $your_name
);
return $return
}
eXorithm – Execute Algorithm: View / Run Algorithm next_prime
function next_prime ($start
{
$primes = -1;
for$i = $start+1; ; $i++)
{
$is_prime = true;
for$j = 2; $j < floor$i/2); $j++)
{
if(($i % $j) == 0)
{
$is_prime = false;
break
}
}
if ($is_prime
{
$prime = $i
break
}
}
return $prime
}
eXorithm – Execute Algorithm: View / Run Algorithm random_usort
function random_usort ($a, $b
{
return mt_rand(-1,1);
}
eXorithm – Execute Algorithm: View / Run Algorithm render_polygons
function render_polygons ($polygons, $vertex_color, $face_color, $wireframe, $dashes, $image_size, $scale
{
foreach ($polygons as $polygon) {
if (!is_array$polygon)) {
throw new Exception'Each polygon must be a list.');
} else if ((count$polygon)%3)!=0) {
throw new Exception'Each polygon must be a list like x1, y1, z1, x2, y2, z2, etc. The number of points therefore must be divisible by three.');
}
}
if (is_array$vertex_color)) {
if (count$vertex_color) != count$polygons)) {
throw new Exception'If vertex colors is an array, it must contain the same number of colors as the number of polygons.');
}
}
if (is_array$face_color)) {
if (count$face_color) != count$polygons)) {
throw new Exception'If face colors is an array, it must contain the same number of colors as the number of polygons.');
}
}
// if scale=0 then we auto-scale
if ($scale==0) {
$max = 0;
for ($i=0; $icount$polygons); $i++) {
for ($j=0; $jcount$polygons$i]); $j$j+3) {
if (abs$polygons$i][$j])>$max
$max = abs$polygons$i][$j]);
if (abs$polygons$i][$j+1])>$max
$max = abs$polygons$i][$j+1]);
}
}
if ($max>0)
$scale = ($image_size-2)/($max*2);
}
// the polygon arrays (x,y,z) must be converted into shapes (x,y)
$shapes = array();
$z_max = array();
for ($i=0; $icount$polygons); $i++) {
$max = $polygons$i][2];
for ($j=0; $jcount$polygons$i]); $j$j+3) {
$x = $polygons$i][$j];
$y = $polygons$i][$j+1];
// map each x,y coord to a screen position
$x = round$image_size/2 + $x$scale);
$y = round$image_size/2 - $y$scale);
$shapes$i][$j] = $x
$shapes$i][$j+1] = $y
// keep track of the maximum z-value for each shape
if ($polygons$i][$j+2]>$max
$max = $polygons$i][$j+2];
}
$shapes$i] = array_values$shapes$i]);
$z_max$i] = $max
}
// create a blank image
$image = image_create_alpha$image_size, $image_size);
// create the colors
if (!is_array$vertex_color))
$vertex_color = array_fill(0, count$polygons), $vertex_color);
if (!is_array$face_color))
$face_color = array_fill(0, count$polygons), $face_color);
// painter's algorithm - draw farther polygons first
array_multisort$z_max, SORT_DESC, $shapes, $face_color, $vertex_color);
// draw the polygons
for ($i=0; $icount$shapes); $i++) {
$v_color = allocate_color$image, $vertex_color$i]);
$f_color = allocate_color$image, $face_color$i]);
if (!$wireframe) {
imagefilledpolygon$image, $shapes$i], count$shapes$i])/2, $f_color);
}
imagepolygon$image, $shapes$i], count$shapes$i])/2, $v_color);
}
// draw dashes - BUGGY
if ($dashes) {
for ($i=0; $icount$shapes); $i++) {
$v_color = allocate_color$image, $vertex_color$i]);
$style = array$v_color, IMG_COLOR_TRANSPARENT, IMG_COLOR_TRANSPARENT, IMG_COLOR_TRANSPARENT, IMG_COLOR_TRANSPARENT);
imagesetstyle$image, $style);
imagepolygon$image, $shapes$i], count$shapes$i])/2, IMG_COLOR_STYLED);
}
}
return $image
}