Blog

eXorithm – Execute Algorithm: View / Run Algorithm draw_upc_barcode

function draw_upc_barcode ($number$show_numbers
{
  $number = str_replacearray'-'' '), ''$number);
  
  if (strlen$number)!=12)
    throw new Exception"UPC number must have 12 digits");
  
  for ($i=0;$i<12;$i++) {
    if (!is_numeric$number$i]))
      throw new Exception"UPC number must contain only digits");
  }
  
  $lcodes = array
    '0001101'
    '0011001'
    '0010011'
    '0111101'
    '0100011'
    '0110001'
    '0101111'
    '0111011'
    '0110111'
    '0001011'
  );
  
  $rcodes = array
    '1110010'
    '1100110'
    '1101100'
    '1000010'
    '1011100'
    '1001110'
    '1010000'
    '1000100'
    '1001000'
    '1110100'
  );
  
  $code = '101'
  for ($i=0;$i<6;$i++) {
    $code .= $lcodes$number$i]];
  }
  $code .= '01010'
  for ($i=6;$i<12;$i++) {
    $code .= $rcodes$number$i]];
  }
  $code .= '101'
  
  // create image
  $width=190;
  $height=100;
  $image = image_create_alpha$width$height);
  
  $white = imagecolorallocate$image, 255, 255, 255);
  imagefilledrectangle$image, 0, 0, $width$height$white);
  
  // draw lines
  $black = imagecolorallocate$image, 0, 0, 0);
  for ($i=0;$istrlen$code);$i++) {
    if ($code$i]=='1') {
      imageline$image$i*2,0, $i*2, $height$black);
      imageline$image$i*2+1,0, $i*2+1, $height$black);
    }
  }
  
  // draw numbers
  if ($show_numbers) {
    imagefilledrectangle$image, 6, $height-16, 90, $height$white);
    imagefilledrectangle$image, 98, $height-16, 182, $height$white);
    for ($i=0;$i<6;$i++) {
      imagestring$image, 2, 11+$i*14, $height-14, $number$i], $black);
    }
    for ($i=6;$i<12;$i++) {
      imagestring$image, 2, 19+$i*14, $height-14, $number$i], $black);
    }
  }
  
  return $image

eXorithm – Execute Algorithm: View / Run Algorithm simple_calculator

function simple_calculator ($number1$operator$number2
{
  // calculator
  
  switch ($operator) {
    case "plus"
      $res = $number1$number2
      break
    case "minus"
      $res = $number1$number2
      break
    case "times"
      $res = $number1$number2
      break
    case "divided by"
      $res = $number1$number2
      break
    default
      $res = 0;
  }
  
  return $res

eXorithm – Execute Algorithm: Embed Algorithm make_change


Embed This Algorithm

This page will help you embed the algorithm make_change 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.

  1. 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.
  2. You can add only the output of the algorithm to your website. There will be no argument inputs or run button.

amount Argument info
coins
Argument info
Embed the form Embed only the output

eXorithm – Execute Algorithm: Embed Algorithm draw_triangle


Embed This Algorithm

This page will help you embed the algorithm draw_triangle 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.

  1. 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.
  2. You can add only the output of the algorithm to your website. There will be no argument inputs or run button.

points
Argument info
color Argument info
Embed the form Embed only the output

eXorithm – Execute Algorithm: Embed Algorithm photobucket


Embed This Algorithm

This page will help you embed the algorithm photobucket 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.

  1. 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.
  2. You can add only the output of the algorithm to your website. There will be no argument inputs or run button.
url Argument info
Embed the form Embed only the output

Validate Domain

<?php

/**
 * validate_domain
 *
 * validate domain
 *
 * @version 0.5
 * @author Contributors at eXorithm
 * @link /algorithm/view/validate_domain Listing at eXorithm
 * @link /algorithm/history/validate_domain History at eXorithm
 * @license /home/show/license
 *
 * @param mixed $domain 
 * @return bool
 */
function validate_domain($domain='www.google.com')
{
	return preg_match ("/^[a-zA-Z0-9\-\.]+\.(com|org|net|mil|edu)$/i", $domain);
}

?>

eXorithm – Execute Algorithm: View / Run Algorithm raise_polygon

function raise_polygon ($points$height$direction$color
{
  // check points
  if ((count$points)==0) || ((count$points)%2)!=0)) {
    throw new Exception'The points must be an array with an even number of integers.');
  }
  
  // determine the extent of the polygon
  $maxx = $points[0];
  $maxy = $points[1];
  $minx = $points[0];
  $miny = $points[1];
  for ($i=2; $icount$points); $i$i+2) {
    $x = $points$i];
    $y = $points$i+1];
    if ($x$maxx$maxx = $x
    if ($y$maxy$maxy = $y
    if ($x$minx$minx = $x
    if ($y$miny$miny = $y
  }
  
  // determine the extent of the projection
  $proj_length = roundsqrt$height$height/2));
  switch ($direction) {
    case 'nw'
      $projx = $proj_length*-1;
      $projy = $proj_length*-1;
      break
    case 'ne'
      $projx = $proj_length
      $projy = $proj_length*-1;
      break
    case 'sw'
      $projx = $proj_length*-1;
      $projy = $proj_length
      break
    default
      $projx = $proj_length
      $projy = $proj_length
  }
  
  // determine where the polygon should be positioned
  for ($i=0; $icount$points); $i$i+2) {
    $points$i] = $points$i]-$minx+1;
    $points$i+1] = $points$i+1]-$miny+1;
    if ($projx<0) $points$i] = $points$i] - $projx
    if ($projy<0) $points$i+1] = $points$i+1] - $projy
  }
  
  // blank image
  $image = image_create_alpha$maxx$minx$proj_length+2, $maxy$miny$proj_length+2);
  
  // create the colors
  $r  = hexdecsubstr$color, 0, 2));
  $g  = hexdecsubstr$color, 2, 2));
  $b  = hexdecsubstr$color, 4, 2));
  $color = imagecolorallocate$image$r$g$b);
  
  // draw the first polygon
  imagepolygon$image$pointscount$points)/2, $color);
  
  // draw the lines + modify the points
  for ($i=0; $icount$points); $i$i+2) {
    imageline$image$points$i], $points$i+1], $points$i]+$projx$points$i+1]+$projy$color);
    $points$i] = $points$i]+$projx
    $points$i+1] = $points$i+1]+$projy
  }
  
  // draw the second polygon
  imagepolygon$image$pointscount$points)/2, $color);
  
  return $image

eXorithm – Execute Algorithm: View / Run Algorithm show_address

function show_address ($address
{
  $data = file_get_contents"http://maps.googleapis.com/maps/api/geocode/json?address="urlencode$address)."&sensor=false");
  $obj = json_decode$data);
  if ($obj) {
    if (isset$obj->results[0]->geometry->location)) {
      $loc = $obj->results[0]->geometry->location;
      return array'latitude'=>$loc->lat, 'longitude'=>$loc->lng);
    } else {
      throw new Exception'Lookup failed and/or address does not exist!');
    }
  } else {
    throw new Exception'Lookup failed and/or address does not exist!');
  }