Blog

eXorithm – Execute Algorithm: View / Run Algorithm books_by

function books_by ($author
{
  // construct the query
  $query = array'limit'=>100, 
                 'name'=>null, 
                 'type'=>'/book/written_work'
                 'author'=>array'name'=>$author));
  
  // issue the query
  $results = freebase_query$query);
  
  $books = array();
  foreach ($results as $result) {
    if (isset$result'name'])) {
      $books[] = $result'name'];
    }
  }
  return $books

eXorithm – Execute Algorithm: View / Run Algorithm diceRoll

function diceRoll ($numSides$numDice
{
  $rolls = array();
  
  /*Rolls each die and stores it's value while also keeping a running
  total of the rolls.*/
  
  for ($i = 0; $i < $numDice$i++) 
         {
           $dieValue = rand(1, $numSides);
           $rollTotal += $dieValue
           Array_push$rolls$dieValue);
        }
  //Adds the roll total to the final index.
  Array_push$rolls$rollTotal);
  
  return $rolls

eXorithm – Execute Algorithm: View / Run Algorithm freebase_query

function freebase_query ($query
{
  $queryarray = array'q'=>array'query'=>array$query)));
  $jsonquerystr = json_encode$queryarray);
  $jsonquerystr = urlencode$jsonquerystr);
  
  $apiendpoint = "http://api.freebase.com/api/service/mqlread?queries"
  $ch = curl_init();
  curl_setopt$ch, CURLOPT_URL, "$apiendpoint=$jsonquerystr");
  curl_setopt$ch, CURLOPT_HEADER, 0);
  curl_setopt$ch, CURLOPT_RETURNTRANSFER, 1);
  $jsonresultstr = curl_exec$ch);
  curl_close$ch);
  
  $resultarray = json_decode$jsonresultstr, true);
  
  if (isset$resultarray'q']['result'])) {
    return $resultarray'q']['result'];
  } else {
    if (isset$resultarray'q']['messages'][0]['message'])) {
      throw new Exception"Error: "$resultarray'q']['messages'][0]['message']);
    } else {
      throw new Exception"Error");
    }
  }

eXorithm – Execute Algorithm: View / Run Algorithm draw_triangle

function draw_triangle ($points$color
{
  if (count$points)!=6) {
    throw new Exception'The points must be an array of 6 integers.');
  }
  
  $image = image_create_alphamax$points[0], $points[2], $points[4])+1, max$points[1], $points[3], $points[5])+1);
  
  $red = hexdecsubstr$color, 0, 2));
  $green  = hexdecsubstr$color, 2, 2));
  $blue  = hexdecsubstr$color, 4, 2));
    
  $color = imagecolorallocatealpha$image$red$green$blue, 0);
  
  imagefilledpolygon$image$points, 3, $color);
  
  return $image

eXorithm – Execute Algorithm: View / Run Algorithm draw_pyramid

function draw_pyramid ($image_size$degree_x$degree_y$degree_z$vdist$dist$vertex_color$side_color$wireframe$dashes
{
  $degree_x = $degree_x % 360;
  $degree_y = $degree_y % 360;
  $degree_z = $degree_z % 360;
  
  // construct the cube polygons
  $size = 400; // the size is arbitrary
  
  $x1$size/2;
  $x0$x1*-1;
  $y1$size/2;
  $y0$y1*-1;
  $z1$size/2;
  $z0$z1*-1;
  
  $sides = array();
  
  $sides[] = array$x0$y0$z0, 0,0,$z1$x0$y1$z0);
  $sides[] = array$x1$y0$z0, 0,0,$z1$x1$y1$z0);
  
  $sides[] = array$x0$y0$z0, 0,0,$z1$x1$y0$z0);
  $sides[] = array$x0$y1$z0, 0,0,$z1$x1$y1$z0);
  
  $sides[] = array$x0$y0$z0$x0$y1$z0$x1$y1$z0$x1$y0$z0);
  
  // project each of the 6 polygons that makes up the cube
  for ($i=0; $icount$sides); $i++) {
    $points[] = project_polygon$sides$i], $degree_x$degree_y$degree_z, 0, 0, 0, $vdist+($size/2), $dist+($size/2), true);
  }
  
  // scale the image somewhat
  $scale = $image_size/($size*1.3);
  
  return render_polygons$points$vertex_color$side_color$wireframe$dashes$image_size$scale);

eXorithm – Execute Algorithm: View / Run Algorithm add_transparency

function add_transparency ($image$percent
{
  $x = imagesx$image);
  $y = imagesy$image);
  
  $image2 = image_create_alpha$x$y);
  imagecopy$image2$image, 0, 0, 0, 0, $x$y);
  
  imagesavealpha$image2, true);
  
  if ($percent>0) {
    for ($ii=0;$ii$x$ii++) {
      for ($jj=0;$jj$y$jj++) {
        $c = imagecolorat$image2$ii$jj);
        $r = ($c >> 16) & 0xFF;
        $g = ($c >> 8) & 0xFF;
        $b = $c & 0xFF;
        $alpha = ($c >> 24) & 0xFF;
        $c = imagecolorallocatealpha$image2$r$g$b$alpha+($percent/100*(127-$alpha)));
        imagesetpixel$image2$ii$jj$c);
      }
    }
  }
  
  return $image2

Duotone Image

<?php

/**
 * duotone_image
 *
 * Change an image into a tinted grayscale.
 *
 * @version 0.5
 * @author Contributors at eXorithm
 * @link /algorithm/view/duotone_image Listing at eXorithm
 * @link /algorithm/history/duotone_image History at eXorithm
 * @license /home/show/license
 *
 * @param resource $image (GD image) The image to duotone.
 * @param number $rplus Red value to increase or decrease.
 * @param number $gplus Green value to increase or decrease.
 * @param number $bplus Blue value to increase or decrease.
 * @param bool $pcnt If checked, the values for rplus, gplus and bplus will be treated as percentages.
 * @return resource GD image
 */
function duotone_image($image=null,$rplus=0,$gplus=0,$bplus=60,$pcnt=false)
{
	// 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;
}

?>