Blog

Unique Image

<?php

/**
 * unique_image
 *
 * Generate a pseudo-unique "hash" image based on a string.
 *
 * @version 0.3
 * @author Contributors at eXorithm
 * @link /algorithm/view/unique_image Listing at eXorithm
 * @link /algorithm/history/unique_image History at eXorithm
 * @license /home/show/license
 *
 * @param mixed $string 
 * @return resource GD image
 */
function unique_image($string='whatever')
{
	$size=200;
	$steps=5;
	$step=$size/$steps;
	
	$image = image_create_alpha($size, $size);
	
	$n = 0;
	$prev = 0;
	$len = strlen($string);
	$sum = 0;
	for ($i=0;$i<$len;$i++) $sum += ord($string[$i]);
	
	for ($i=0;$i<$steps;$i++) {
		for ($j=0;$j<$steps;$j++) {
			$letter = $string[$n++ % $len];
			
			$u = ($n % (ord($letter)+$sum)) + ($prev % (ord($letter)+$len)) + (($sum-1) % ord($letter));
			$color = imagecolorallocate($image, pow($u*$prev+$u+$prev+5,2)%256, pow($u*$prev+$u+$prev+3,2)%256, pow($u*$prev+$u+$prev+1,2)%256);
			if (($u%2)==0)
				imagefilledpolygon($image, array($i*$step, $j*$step, $i*$step+$step, $j*$step, $i*$step, $j*$step+$step), 3, $color);
			$prev = $u;
			
			$u = ($n % (ord($letter)+$len)) + ($prev % (ord($letter)+$sum)) + (($sum-1) % ord($letter));
			if (($u%2)==0)
				imagefilledpolygon($image, array($i*$step, $j*$step+$step, $i*$step+$step, $j*$step+$step, $i*$step+$step, $j*$step), 3, $color);
			$prev = $u;
		
		}
	}
	
	return $image;
}

/**
 * image_create_alpha
 *
 * Helper function to create a new blank image with transparency.
 *
 * @version 0.1
 * @author Contributors at eXorithm
 * @link /algorithm/view/image_create_alpha Listing at eXorithm
 * @link /algorithm/history/image_create_alpha History at eXorithm
 * @license /home/show/license
 *
 * @param mixed $width 
 * @param mixed $height 
 * @return resource GD image
 */
function image_create_alpha($width='',$height='')
{
	// Create a normal image and apply required settings
	$img = imagecreatetruecolor($width, $height);
	imagealphablending($img, false);
	imagesavealpha($img, true);
	
	// Apply the transparent background
	$trans = imagecolorallocatealpha($img, 0, 0, 0, 127);
	for ($x = 0; $x < $width; $x++)
	{
		for ($y = 0; $y < $height; $y++)
		{
			imagesetpixel($img, $x, $y, $trans);
		}
	}
	
	return $img;
}

?>

Simple Sort

<?php

/**
 * simple_sort
 *
 * Sort an array and display it.
 *
 * @version 0.1
 * @author Contributors at eXorithm
 * @link /algorithm/view/simple_sort Listing at eXorithm
 * @link /algorithm/history/simple_sort History at eXorithm
 * @license /home/show/license
 *
 * @param array $array 
 * @return array
 */
function simple_sort($array=array(0=>'1',1=>'11',2=>'22',3=>'3',4=>'4'))
{
	sort($array);
	return $array;
}

?>

eXorithm – Execute Algorithm: View / Run Algorithm convert2grayscale

function convert2grayscale ($image
{
  $height = imagesy$image);
  $width = imagesx$image);
  $tc = imageistruecolor$image);
  
  $new_image = imagecreatetruecolor$width$height);
  imagealphablending$new_image, false);
  imagesavealpha$new_image, true);
  
  for ($ii=0; $ii$width$ii++) {
    for ($jj=0; $jj$height$jj++) {
      $rgb = imagecolorat$image$ii$jj);
      if ($tc) {
        $r = ($rgb >> 16) & 0xFF;
        $g = ($rgb >> 8) & 0xFF;
        $b = $rgb & 0xFF;
        $alpha = ($rgb >> 24) & 0xFF;
      } else {
        $rgb = imagecolorsforindex$image$rgb);
        $r = $rgb'red'];
        $g = $rgb'green'];
        $b = $rgb'blue'];
        $alpha = $rgb'alpha'];
      }
      $avg = round(($r$g$b)/3);
      $color = imagecolorallocatealpha$new_image$avg$avg$avg$alpha);
      imagesetpixel$new_image$ii$jj$color);
    }
  }
  
  return $new_image

PI Digits

<?php

/**
 * pi_digits
 *
 * Calculate the digits of pi.
 *
 * @version 0.4
 * @author Contributors at eXorithm
 * @link /algorithm/view/pi_digits Listing at eXorithm
 * @link /algorithm/history/pi_digits History at eXorithm
 * @license /home/show/license
 *
 * @param number $digits 
 * @return mixed
 */
function pi_digits($digits=500)
{
	$n = floor($digits * 14/4);
	
	$scale = 10000;
	$init = 2000;
	$carry = 0;
	$result = '';
	
	for($i=0;$i<=$n;$i++) {
		$arr[$i] = $init;
	}
	
	for($i=$n;$i>0;$i=$i-14) {  
		$sum = 0;  
		for($j=$i;$j>0;$j--) {  
			$sum = ($sum * $j) + ($scale * $arr[$j]);  
			$arr[$j] = $sum % (($j*2)-1);  
			$sum = floor($sum / (($j*2)-1));  
		}
		$result .= sprintf("%04d", ($carry + ($sum / $scale)));
		$carry = $sum % $scale;  
	}
	
	if ($digits>1) {
		return $result[0].'.'.substr($result,1,$digits-1);
	} else {
		return substr($result,0,$digits);
	}
}

?>

eXorithm – Execute Algorithm: View / Run Algorithm rot13

function rot13 ($string
{
  for$i=0;$istrlen$string);$i++) {
    $jord$string$i]);
    if ((($j>=ord"n")) & ($j<=ord"z"))) | ($j>=ord"N")) & ($j<=ord"Z"))) {
      $j$j-13;
    }
    elseif ((($j>=ord"a")) & ($j<=ord"m"))) | ($j>=ord"A")) & ($j<=ord"M"))) {
      $j$j+13;
    }
    $new.=chr$j);
  }
  return$new);

eXorithm – Execute Algorithm: View / Run Algorithm draw_sierpinski

function draw_sierpinski ($size$color
{
  // see http://en.wikipedia.org/wiki/Sierpinski_triangle
  // this code from http://php.net/manual/en/function.imagesetpixel.php
  
  $hght = roundsqrt$size$size - ($size/2)*($size/2)));
  $diff = round(($size - $hght)/2);
  
  $gd = imagecreatetruecolor$size$size);
   
  $corners[0] = array'x' => round$size/2), 'y' => $diff);
  $corners[1] = array'x' =>   0, 'y' => $size$diff);
  $corners[2] = array'x' => $size'y' => $size$diff);
  
  $red = hexdecsubstr$color, 0, 2));
  $green  = hexdecsubstr$color, 2, 2));
  $blue  = hexdecsubstr$color, 4, 2));
  
  $color = imagecolorallocate$gd$red$green$blue);
  
  $x = $size
  $y = $size
  
  for ($i = 0; $i < 200000; $i++) {
    imagesetpixel$gdround$x),round$y), $color);
    $a = rand(0, 2);
    $x = ($x + $corners$a]['x']) / 2;
    $y = ($y + $corners$a]['y']) / 2;
  }
  return $gd