function hailstone ($number
{
$result = array();
while ($number > 1) {
$result[] = $number
if ($number & 1)
$number = 3 * $number + 1;
else
$number = $number / 2;
}
$result[] = $number
return $result
}
Category: Algorithm
eXorithm – Execute Algorithm: History For Algorithm round_corners
round_corners    version 0.2    Round the corners of an image. Transparency and anti-aliasing are supported.
eXorithm – Execute Algorithm: View / Run Algorithm blur_image
function blur_image ($image, $level
{
// based on http://www.tuxradar.com/practicalphp/11/2/25
$imagex = imagesx$image);
$imagey = imagesy$image);
$tc = imageistruecolor$image);
$new_image = imagecreatetruecolor$imagex, $imagey);
imagealphablending$new_image, false);
imagesavealpha$new_image, true);
$level = $level-1;
for ($x = 0; $x < $imagex; ++$x) {
for ($y = 0; $y < $imagey; ++$y) {
$newr = 0;
$newg = 0;
$newb = 0;
$newt = 0;
$colours = array();
$thiscol = imagecolorat$image, $x, $y);
for ($k = $x - $level; $k <= $x + $level; ++$k) {
for ($l = $y - $level; $l <= $y + $level; ++$l) {
if ($k < 0) { $colours[] = $thiscol; continue; }
if ($k >= $imagex) { $colours[] = $thiscol; continue; }
if ($l < 0) { $colours[] = $thiscol; continue; }
if ($l >= $imagey) { $colours[] = $thiscol; continue; }
$colours[] = imagecolorat$image, $k, $l);
}
}
foreach$colours as $colour) {
if ($tc) {
$newr += ($colour >> 16) & 0xFF;
$newg += ($colour >> 8) & 0xFF;
$newb += $colour & 0xFF;
$newt += ($colour >> 24) & 0xFF;
} else {
$c = imagecolorsforindex$image, $colour);
$newr += $c'red'];
$newg += $c'green'];
$newb += $c'blue'];
$newt += $c'alpha'];
}
}
$numelements = count$colours);
$newr /= $numelements
$newg /= $numelements
$newb /= $numelements
$newt /= $numelements
$newcol = imagecolorallocatealpha$new_image, $newr, $newg, $newb, $newt);
imagesetpixel$new_image, $x, $y, $newcol);
}
}
return $new_image
}
eXorithm – Execute Algorithm: View / Run Algorithm triangulate
function triangulate ($side1, $side2, $side3, $angle1, $angle2, $angle3, $radians
{
  $sides = array();
  $angles = array();
 Â
  // get sides
  if ($side1!='') $sides[1] = $side1+0;
  if ($side2!='') $sides[2] = $side2+0;
  if ($side3!='') $sides[3] = $side3+0;
 Â
  // get angles
  if ($angle1!='') $angles[1] = $angle1+0;
  if ($angle2!='') $angles[2] = $angle2+0;
  if ($angle3!='') $angles[3] = $angle3+0;
 Â
  // convert to radians if necessary
  if (!$radians) {
    foreach ($angles as $angle => $value) {
      $angles$angle] = deg2rad$value);
    }
  }
 Â
  // need 3 values and at least one side
  if (((count$sides)+count$angles))!=3) || (count$sides)==0)) {
    throw new Exception"You must only provide 3 values for the sides and angles. At least one value must be a side. Leave the other three values blank.");
  }
 Â
  // iterate three times to make sure that we get everything
  for ($count=0;$count<3;$count++) {
    // for the three sides/angles
    for ($i1=1;$i1<=3;$i1++) {
      $i2 = ($i1 % 3)+1; // the other side/angle
      $i3 = (($i1+1) % 3)+1; // the other other side/angle
      if (!isset$angles$i1])) {
        // try $angle1 = 180 - $angle2 - $angle3
        if ((isset$angles$i2])) && (isset$angles$i3]))) {
          $angles$i1] = pi() - $angles$i2] - $angles$i3];
        }
      }
      if (!isset$sides$i1])) {
        if (isset$angles$i1])) {
          // try $side1 = $side2 * (sin($angle1)/sin($angle2))
          if ((isset$sides$i2])) && (isset$angles$i2]))) {
            $sides$i1] = $sides$i2] * sin$angles$i1]) / sin$angles$i2]);
          }
          // try $side1 = $side3 * (sin($angle1)/sin($angle3))
          if ((isset$sides$i3])) && (isset$angles$i3]))) {
            $sides$i1] = $sides$i3] * sin$angles$i1]) / sin$angles$i3]);
          }
        }
      }
      if (!isset$angles$i1])) {
        if (isset$sides$i1])) {
          // try $angle1 = arcsin(sin($angle2) * $side1/$side2)
          if ((isset$sides$i2])) && (isset$angles$i2]))) {
            $angles$i1] = asinsin$angles$i2]) * $sides$i1] / $sides$i2]);
          }
          // try $angle1 = arcsin(sin($angle3) * $side1/$side3)
          if ((isset$sides$i3])) && (isset$angles$i3]))) {
            $angles$i1] = asinsin$angles$i3]) * $sides$i1] / $sides$i3]);
          }
        }
      }
      if (!isset$sides$i1])) {
        // try $side1 = sqrt($side2^2 + $side3^2 - 2*$side2*$side3*cos($angle1))
        if ((isset$sides$i2])) && (isset$sides$i3])) && (isset$angles$i1]))) {
          $sides$i1] = sqrt$sides$i2]*$sides$i2] + $sides$i3]*$sides$i3] - 2 * $sides$i2] * $sides$i3] * cos$angles$i1]));
        }
      }
      if (!isset$angles$i1])) {
        // try $angle1 = arccos($side2^2 + $side3^2 - $side1^2 / 2*$side2*$side3))
        if ((isset$sides$i2])) && (isset$sides$i3])) && (isset$sides$i1]))) {
          $angles$i1] = acos(($sides$i2]*$sides$i2] + $sides$i3]*$sides$i3] - $sides$i1]*$sides$i1]) / (2 * $sides$i2] * $sides$i3]));
        }
      }
    }
    // were we able to calculate everything?
    if ((count$sides)+count$angles))==6) break
  }
 Â
  // bad values?
  foreach ($angles as $value) {
    if (is_nan$value) || is_infinite$value) || ($value<=0)) {
      throw new Exception"A triangle cannot be constructed with those values.");
    }
  }
 Â
  ksort$sides);ksort$angles);
 Â
  // convert to degrees if necessary
  if (!$radians) {
    foreach ($angles as $angle => $value) {
      $angles$angle] = rad2deg$value);
    }
  }
 Â
  return (array'sides'=>$sides, 'angles'=>$angles));
}Â
eXorithm – Execute Algorithm: View / Run Algorithm backwards
function backwards ($text
{
// this function will put the words in $text backwards
$text = str_replace" ", " ", $text);
$text = strip_tags$text);
$text = str_replace"n", " ", $text);
$text = str_replace"r", " ", $text);
$text = preg_replace'/s+/', ' ', $text);
$arr = explode" ", $text);
$arr = array_reverse$arr);
return implode" ", $arr);
}
Magic8Ball
<?php /** * magic8ball * * Ask the magic 8 ball your question. * * @version 0.2 * @author Contributors at eXorithm * @link /algorithm/view/magic8ball Listing at eXorithm * @link /algorithm/history/magic8ball History at eXorithm * @license /home/show/license * * @return mixed */ function magic8ball() { $answers =array('It is certain', 'It is decidedly so', 'Without a doubt', 'Yes โ definitely', 'You may rely on it', 'As I see it, yes', 'Most likely', 'Outlook good', 'Signs point to yes', 'Yes', 'Reply hazy, try again', 'Ask again later', 'Better not tell you now', 'Cannot predict now', 'Concentrate and ask again', 'Don\'t bet on it', 'My reply is no', 'My sources say no', 'Outlook not so good', 'Very doubtful' ); $index = rand(0, count($answers)); return ($answers[$index]); } ?>
Photobucket
<?php /** * photobucket * * Gets all the pictures from a Photobucket album. * * @version 0.2 * @author Contributors at eXorithm * @link /algorithm/view/photobucket Listing at eXorithm * @link /algorithm/history/photobucket History at eXorithm * @license /home/show/license * * @param mixed $url Photobucket URL * @return mixed */ function photobucket($url='http://s283.photobucket.com/albums/kk285/konnarak_1608/romantic/') { $str = file_get_contents($url); preg_match_all('/<img[^>]+>/i',$str, $result); $strPics = ""; foreach( $result as $img_tag) { foreach( $img_tag as $img) { if( !strpos($img, 'class="under off"') ) continue; preg_match('/< *img[^>]*src *= *["\']?([^"\']*)/i', $img, $imgURLs); $imgURL = str_replace("/th_", "/", $imgURLs[1]); $strPics .= $imgURL . "\n"; } } return $strPics; } ?>
eXorithm – Execute Algorithm: View / Run Algorithm swirl_image
function swirl_image ($image, $iterations, $angle
{
  $height = imagesy$image);
  $width = imagesx$image);
 Â
  $new_image = imagecreatetruecolor$width, $height);
  $iterations = max(1, $iterations);
 Â
  $wstep = ($width/2)/($iterations+1);
  $hstep = ($height/2)/($iterations+1);
 Â
  for ($ii=0;$ii$iterations$ii++) {
    $w = $width - ($ii+1)*$wstep*2;
    $h = $height - ($ii+1)*$hstep*2;
    $images$ii] = imagecreatetruecolor$w, $h);
    imagecopy$images$ii], $image, 0, 0, ($ii+1)*$wstep, ($ii+1)*$hstep, $w, $h);
    $images$ii] = rotate_image_alpha$images$ii], $angle*($ii+1), 'ffffff', 127);
  }
 Â
  for ($ii=0;$ii$iterations$ii++) {
    // Set the brush
    imagesetbrush$image, $images$ii]);
    // Draw a couple of brushes, each overlaying each
    imageline$image, imagesx$image) / 2, imagesy$image) / 2, imagesx$image) / 2, imagesy$image) / 2, IMG_COLOR_BRUSHED);
  }
 Â
  return $image
}Â
eXorithm – Execute Algorithm: Embed Algorithm draw_cube
Embed This Algorithm
This page will help you embed the algorithm draw_cube on a page on your own website. Just configure the inputs, then click the generate button to get a snippet of code you can paste onto your site. You have two options.
- You can embed the entire form. Users will be able to enter their own arguments, and will need to press the run button to execute the algorithm.
- You can add only the output of the algorithm to your website. There will be no argument inputs or run button.
eXorithm – Execute Algorithm: View / Run Algorithm duotone_image
function duotone_image ($image, $rplus, $gplus, $bplus, $pcnt
{
// Adapted from http://www.tuxradar.com/practicalphp/11/2/21
$imagex = imagesx$image);
$imagey = imagesy$image);
$image2 = imagecreatetruecolor$imagex, $imagey);
imagesavealpha$image2, true);
imagealphablending$image2, false);
for ($x = 0; $x <$imagex; ++$x) {
for ($y = 0; $y <$imagey; ++$y) {
$rgb = imagecolorat$image, $x, $y);
$color = imagecolorsforindex$image, $rgb);
$grey = floor(($color'red']+$color'green']+$color'blue'])/3);
if ($pcnt) {
$red = $grey + $grey*($rplus/150);
$green = $grey + $grey*($gplus/150);
$blue = $grey + $grey*($bplus/150);
} else {
$red = $grey + $rplus
$green = $grey + $gplus
$blue = $grey + $bplus
}
if ($red > 255) $red = 255;
if ($green > 255) $green = 255;
if ($blue > 255) $blue = 255;
if ($red < 0) $red = 0;
if ($green < 0) $green = 0;
if ($blue < 0) $blue = 0;
$newcol = imagecolorallocatealpha$image2, $red$green$blue$color'alpha']);
imagesetpixel ($image2, $x, $y, $newcol);
}
}
return $image2
}