<?php /** * address_elevation * * Returns the elevation (in meters) above sea level for an address. * * @version 0.2 * @author Contributors at eXorithm * @link /algorithm/view/address_elevation Listing at eXorithm * @link /algorithm/history/address_elevation History at eXorithm * @license /home/show/license * * @param mixed $address The address to get the elevation for. * @return mixed */ function address_elevation($address='Denver, Colorado') { // get the lat/long for this address $data = file_get_contents("http://maps.google.com/maps/geo?output=csv&q=".urlencode($address)); $arr = explode(",", $data); if (count($arr)>=4) { if ($arr[0]==200) { // get the elevation for this lat/long $data = file_get_contents("http://maps.googleapis.com/maps/api/elevation/xml?sensor=false&locations=".$arr[2].','.$arr[3]); $obj = simplexml_load_string($data); if ($obj instanceof SimpleXMLElement) { $obj = (array) $obj; $obj = $obj['result']; if ($obj instanceof SimpleXMLElement) { $obj = (array) $obj; return $obj['elevation']; } else { throw new Exception('Elevation lookup failed'); } } else { throw new Exception('Elevation lookup failed'); } } else { throw new Exception('Address lookup failed'); } } else { throw new Exception('Address lookup failed'); } } ?>
Category: Algorithm
eXorithm – Execute Algorithm: View / Run Algorithm check_domain
function check_domain ($url, $white_list, $black_list
{
foreach ($white_list as $re) {
$re = preg_quote$re);
$re = str_replace'*', '.*', $re);
if (preg_match'|^'$re'$|', $url)) {
return true;
}
}
foreach ($black_list as $re) {
$re = preg_quote$re);
$re = str_replace'*', '.*', $re);
if (preg_match'|^'$re'$|', $url)) {
return false;
}
}
return true;
}
eXorithm – Execute Algorithm: Algorithms Beginning with H
eXorithm – Execute Algorithm: View / Run Algorithm show_unix_time
function show_unix_time ($time, $format
{
return date$format,($time == 0) ? time() : $time);
}
eXorithm – Execute Algorithm: View / Run Algorithm constrain_image
function constrain_image ($image, $width, $height, $mode, $border_color, $transparent_border, $never_scale_up
{
  if (!$image
    throw new Exception'Invalid image');
 Â
  $iwidth = imagesx$image);
  $iheight = imagesy$image);
 Â
  if ($never_scale_up) {
    if ($width$iwidth && $height$iheight) {
      $mode = 'crop'
    }
    if ($width$iwidth || $height$iheight) {
      if (($mode=='scale') || ($mode=='scale_crop'))
        $mode = 'crop'
    }
  }
 Â
  // scaling phase
  switch ($mode) {
 Â
    case 'scale_crop': // scale and crop
      if ($width$iwidth > $height$iheight) {
        $iheight = round$iheight$width$iwidth);
        $iwidth =  $width
      } else {
        $iwidth =  round$iwidth$height$iheight);
        $iheight = $height
      }
      $image2 = image_create_alpha$iwidth, $iheight);
      imagecopyresampled$image2, $image, 0, 0, 0, 0, $iwidth, $iheight, imagesx$image), imagesy$image));
      break
    Â
    case 'scale_border': // scale and add borders
      if ($width$iwidth < $height$iheight) {
        $iheight = round$iheight$width$iwidth);
        $iwidth =  $width
      } else {
        $iwidth =  round$iwidth$height$iheight);
        $iheight = $height
      }
      $image2 = image_create_alpha$iwidth, $iheight);
      imagecopyresampled$image2, $image, 0, 0, 0, 0, $iwidth, $iheight, imagesx$image), imagesy$image));
      break
     Â
    case 'scale': // scale
      $image2 = image_create_alpha$width, $height);
      imagecopyresampled$image2, $image, 0, 0, 0, 0, $width, $height, imagesx$image), imagesy$image));
      break
 Â
    default
      $image2 = $image
  }
 Â
  // crop / borders phase
  switch ($mode) {
    case 'crop':Â
    case 'scale_crop':Â
    case 'scale_border':Â
     Â
      $image3 = image_create_alpha$width, $height);
     Â
      $r  = hexdecsubstr$border_color, 0, 2));
      $g  = hexdecsubstr$border_color, 2, 2));
      $b  = hexdecsubstr$border_color, 4, 2));
      if ($transparent_border
        $border_color = imagecolorallocatealpha$image3, $r, $g, $b, 127);
      else
        $border_color = imagecolorallocatealpha$image3, $r, $g, $b, 0);
      imagefilledrectangle$image3, 0, 0, $width, $height, $border_color);
     Â
      // x,y to paste to
      $px = ($iwidth$width)   ? round(($width - $iwidth)/2) : 0;
      $py = ($iheight$height) ? round(($height - $iheight)/2) : 0;
      // x,y to start cut from
      $sx = ($iwidth$width)   ? 0 : round(($iwidth - $width)/2);
      $sy = ($iheight$height) ? 0 : round(($iheight - $height)/2);
      // x,y to end cut at
      $ex = ($iwidth$width)   ? $iwidth : $sx$width
      $ey = ($iheight$height) ? $iheight : $sy$height
      imagecopy$image3, $image2, $px, $py, $sx, $sy, $ex, $ey);
      break
     Â
    default
      $image3 = $image2
  }
 Â
  return $image3
}Â
eXorithm – Execute Algorithm: View / Run Algorithm volume_cone
function volume_cone ($radius, $height
{
  return (1/3) * pi() * ($radius$radius) * $height
}Â
eXorithm – Execute Algorithm: View / Run Algorithm bubble_sort
function bubble_sort ($array
{
do {
$again = false;
for$ii=0; $ii<(count$array)-1); $ii++) {
if$array$ii] > $array$ii+1]) {
$temp = $array$ii];
$array$ii] = $array$ii+1];
$array$ii+1] = $temp
$again = true;
}
}
} while ($again==true);
return $array
}
eXorithm – Execute Algorithm: History For Algorithm make_change
eXorithm – Execute Algorithm: View / Run Algorithm primes
function primes ($number_of_primes
{
$n = $number_of_primes
$primes = array();
for$i = 2; ; $i++)
{
$is_prime = true;
for$j = 2; $j < $i; $j++)
{
if(($i % $j) == 0)
{
$is_prime = false;
break
}
if$j > floor$i/2)) break
}
if ($is_prime
{
$primes[] = $i
$n--;
}
if ($n == 0) break
}
return $primes
}
eXorithm – Execute Algorithm: Embed Algorithm pi_digits
Embed This Algorithm
This page will help you embed the algorithm pi_digits 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.