function address_elevation ($address
{
// 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: History For Algorithm duotone_image
Draw Triangle
<?php /** * draw_triangle * * Draw a filled triangle. * * @version 0.4 * @author Contributors at eXorithm * @link /algorithm/view/draw_triangle Listing at eXorithm * @link /algorithm/history/draw_triangle History at eXorithm * @license /home/show/license * * @param array $points This should be a list of six numbers that define the points of the triange (x1, y1, x2, y2, x3, y3) * @param string $color (hex color code) The color of the triangle * @return resource GD image */ function draw_triangle($points=array(0=>'0',1=>'0',2=>'0',3=>'8',4=>'8',5=>'4'),$color='000000') { if (count($points)!=6) { throw new Exception('The points must be an array of 6 integers.'); } $image = image_create_alpha(max($points[0], $points[2], $points[4])+1, max($points[1], $points[3], $points[5])+1); $red = hexdec(substr($color, 0, 2)); $green = hexdec(substr($color, 2, 2)); $blue = hexdec(substr($color, 4, 2)); $color = imagecolorallocatealpha($image, $red, $green, $blue, 0); imagefilledpolygon($image, $points, 3, $color); 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; } ?>
eXorithm – Execute Algorithm: Algorithms Beginning with R
eXorithm – Execute Algorithm: View / Run Algorithm biological_classification
function biological_classification ($lifeform
{
// construct the query
$query = array'name'=>$lifeform,
'type'=>'/biology/organism_classification',
'higher_classification'=>null,
'rank'=>null,
'scientific_name'=>null
);
// issue the query
$results = freebase_query$query);
if (isset$results[0])) {
if (isset$results[0]['name']))
$lifeform = $results[0]['name'];
$return = "$lifeformnn"
do {
$rank = '(unranked)'
$higher = ''
$scientific = ''
if (isset$results[0]['higher_classification']))
$higher = $results[0]['higher_classification'];
if (isset$results[0]['scientific_name']))
$scientific = $results[0]['scientific_name'];
if (isset$results[0]['rank']))
$rank = $results[0]['rank'];
// scientific name found
if ($scientific != '') {
$return .= "$rank: $scientificn"
} else {
$return .= "no informationn"
}
// higher lifeform found
if ($higher != '') {
$query'name'] = $higher
$results = freebase_query$query);
}
} while ($higher != '');
return $return
} else {
throw new Exception"Lifeform $lifeform not found");
}
}
eXorithm – Execute Algorithm: View / Run Algorithm country_distance
function country_distance ($country1, $country2
{
$p1 = locate_country$country1);
$p2 = locate_country$country2);
return distance_on_earth$p1, $p2);
}
eXorithm – Execute Algorithm: View / Run Algorithm draw_sphere
function draw_sphere ($image_size, $degree_x, $degree_y, $degree_z, $vdist, $dist, $vertex_color, $face_color, $wireframe, $dashes, $sphere_detail
{
$lats = 2*$sphere_detail+1; // latitudes, including equator and poles (should be odd)
$longs = $sphere_detail*4;
$degree_per_lat = 90/floor$lats/2);
$degree_per_long = 360/$longs
$radius = 200; // radius is arbitary
// build all the polygons that make up a sphere
for ($i=0;$iceil$lats/2);$i++) {
$y = $radius * cosdeg2rad$degree_per_lat$i));
$inner_radius = $radius * sindeg2rad$degree_per_lat$i));
for ($j=0;$j$longs$j++) {
$x = $inner_radius * sindeg2rad$degree_per_long$j));
$z = $inner_radius * cosdeg2rad$degree_per_long$j));
if ($iceil$lats/2)-1) {
$poly = $i$longs$j+1;
$sides$poly][6] = $x
$sides$poly][7] = $y
$sides$poly][8] = $z
$sides$poly*-1][6] = $x
$sides$poly*-1][7] = $y*-1;
$sides$poly*-1][8] = $z
if ($i>0) {
$poly = $i$longs+(($j$longs-1) % $longs)+1;
$sides$poly][9] = $x
$sides$poly][10] = $y
$sides$poly][11] = $z
$sides$poly*-1][9] = $x
$sides$poly*-1][10] = $y*-1;
$sides$poly*-1][11] = $z
}
}
if ($i>0) {
$poly = ($i-1)*$longs$j+1;
$sides$poly][3] = $x
$sides$poly][4] = $y
$sides$poly][5] = $z
$sides$poly*-1][3] = $x
$sides$poly*-1][4] = $y*-1;
$sides$poly*-1][5] = $z
$poly = ($i-1)*$longs+(($j$longs-1) % $longs)+1;
$sides$poly][0] = $x
$sides$poly][1] = $y
$sides$poly][2] = $z
$sides$poly*-1][0] = $x
$sides$poly*-1][1] = $y*-1;
$sides$poly*-1][2] = $z;
}
}
}
$sides = array_values$sides);
// project each of the polygons
for ($i=0; $icount$sides); $i++) {
$points[] = project_polygon$sides$i], $degree_x, $degree_y, $degree_z, 0, 0, 0, $vdist$radius, $dist$radius, true);
}
// scale the image somewhat
$scale = $image_size/($radius*2.2);
return render_polygons$points, $vertex_color, $face_color, $wireframe, $dashes, $image_size, $scale);
}
eXorithm – Execute Algorithm: View / Run Algorithm plot_function
function plot_function ($function, $variable, $range_start, $range_end, $width, $height, $pixels_per_plot, $color, $show_grid
{
  // fix the pixels/plot variable
  $pixels_per_plot = floor$pixels_per_plot);
  if ($pixels_per_plot<=0) $pixels_per_plot = 1;
 Â
  // calculate all our points
  $xy = array();
  for ($ii=0; $ii<($width$pixels_per_plot); $ii$ii$pixels_per_plot) {
    if ($ii>=$width) {
      $x = $range_start + ($range_end - $range_start);
    } else {
      $x = $range_start + $ii$width * ($range_end - $range_start);
    }
    $y = evaluate_for_x$function, $x);
    if (!is_nan$y)) {
      $xy[]=array$x$y);
    }
  }
 Â
  // find the min and max values of x
  for ($ii=0; $iicount$xy); $ii++) {
    if (is_finite$xy$ii][1])) {
      if ((isset$miny)) && (isset$maxy)))  {
        if ($xy$ii][1]<$miny) $miny = $xy$ii][1];
        if ($xy$ii][1]>$maxy) $maxy = $xy$ii][1];
      } else {
        $maxy = $xy$ii][1];
        $miny = $xy$ii][1];
      }
    }
  }
  if (!isset$maxy)) $maxy = 1;
  if (!isset$miny)) $miny = -1;
 Â
  // if height is not given, compute it automatically
  if ($height<=0) {
    $height = ceil(($maxy$miny)/($range_end$range_start)*$width);
    if ($height>2000) $height = 2000;
  }
 Â
  // create blank image
  $image = image_create_alpha$width, $height+1);
 Â
  // create the colors
  $r  = hexdecsubstr$color, 0, 2));
  $g  = hexdecsubstr$color, 2, 2));
  $b  = hexdecsubstr$color, 4, 2));
  $color = imagecolorallocate$image, $r, $g, $b);
 Â
  // determine the view port for the y axis (where on the y axis should we focus?)
  $bottom_y = ($maxy$miny)/2 - (($height$width)*($range_end$range_start)/2);
  $top_y = $bottom_y + (($height$width)*($range_end$range_start));
 Â
  // numbers to shift by so that the graph is focused on the right spot
  $shift_y = $bottom_y*-1;
  $shift_x = $range_start*-1;
  // factor to translate the plotted points to pixel locations
  $mult = $width/($range_end$range_start);
 Â
  // draw the graph lines
  if ($show_grid) {
    $graph_color = imagecolorallocate$image, 200, 200, 200);
    $minor_color = imagecolorallocate$image, 230, 230, 230);
 Â
    $line_every = 50/$mult; // lines approximately every 50 pixels
    $places = floorlog$line_every,10))*-1;
    $line_every = round$line_every/5, $places)*5; // try and make the graph lines appear at a nice divisible by 5 number
    if ($line_every==0) $line_every=1; // this shouldn't happen, but just in case don't want to divide by 0
   Â
    // vertical lines
    $vert_line = floor$range_start$line_every) * $line_every
    while ($vert_line<=$range_end) {
      imageline$image, round(($vert_line$shift_x)*$mult), 0, round(($vert_line$shift_x)*$mult), $height, $minor_color);
      imagestring$image, 1, round(($vert_line$shift_x)*$mult)+1, $height-10, "$vert_line", $graph_color);
      $vert_line = $vert_line$line_every
    }
 Â
    // horizontal lines
    $horiz_line = floor$bottom_y$line_every) * $line_every
    while ($horiz_line<=$top_y) {
      imageline$image, 0, $heightround(($horiz_line$shift_y)*$mult), $width, $heightround(($horiz_line$shift_y)*$mult), $minor_color);
      imagestring$image, 1, 5, $heightround(($horiz_line$shift_y)*$mult), "$horiz_line", $graph_color);
      $horiz_line = $horiz_line$line_every
    }
   Â
    // axis lines
    imageline$image, round$shift_x$mult), 0, round$shift_x$mult), $height, $graph_color);
    imageline$image, 0, $heightround$shift_y$mult), $width, $heightround$shift_y$mult), $graph_color);
  }
 Â
  // draw the line
  for ($ii=0; $iicount$xy)-1; $ii++) {
   Â
    // infinite number?
    if (is_infinite$xy$ii][1])) {
      if ($xy$ii][1]<0) $xy$ii][1]=$bottom_y-100000;
      else $xy$ii][1]=$top_y+100000;
    }
   Â
    // translate the plotted values to locations in the image
    $x1 = round(($xy$ii][0]+$shift_x)*$mult);
    $y1 = $heightround(($xy$ii][1]+$shift_y)*$mult);
    $x2 = round(($xy$ii+1][0]+$shift_x)*$mult);
    $y2 = $heightround(($xy$ii+1][1]+$shift_y)*$mult);
   Â
    // draw the line
    imageline$image, $x1, $y1, $x2, $y2, $color);
  }
 Â
  return $image
}Â
eXorithm – Execute Algorithm: View / Run Algorithm edgify
function edgify ($image, $threshold
{
// from http://community.invisionpower.com/topic/150046-php-functions/
$height = imagesy$image);
$width = imagesx$image);
$new_image = imagecreatetruecolor$width, $height);
$white = imagecolorallocate$new_image, 255, 255, 255);
$black = imagecolorallocate$new_image, 0, 0, 0);
$image = convert2grayscale$image);
// add edges using sobel technique
for ($x=0; $x$width; $x++) {
for ($y=0; $y$height; $y++) {
$x2 = $x+1;
$y2 = $y+1;
if ($x2>=$width) $x2 = $x
if ($y2>=$height) $y2 = $y;
$p1 = imagecolorat$image$x$y2) & 0xFF;
$p2 = imagecolorat$image$x2$y2) & 0xFF;
$p3 = imagecolorat$image$x$y) & 0xFF;
$p4 = imagecolorat$image$x2$y) & 0xFF;
$h = abs$p1 - $p4);
$k = abs$p2 - $p3);
$g = $h + $k
if ($g > $threshold){
imagesetpixel$new_image, $x, $y, $black);
} else {
imagesetpixel$new_image, $x, $y, $white);
}
}
}
return $new_image
}
eXorithm – Execute Algorithm: View / Run Algorithm star_polygon
function star_polygon ($radius, $points, $skip, $color
{
// blank image
$image = image_create_alpha$radius*2+4, $radius*2+4);
// create the color
$r = hexdecsubstr$color, 0, 2));
$g = hexdecsubstr$color, 2, 2));
$b = hexdecsubstr$color, 4, 2));
$color = imagecolorallocate$image, $r, $g, $b);
// The fudge factor is only used to rotate the star so its points line
// up at the bottom.
if (($points%2)==0) {
if (($points/2%2)==0) {
$fudge = pi()*1/$points
} else {
$fudge = 0;
}
} else {
if ((($points-1)/2%2)==0) {
$fudge = pi()*1.5/$points
} else {
$fudge = pi()*0.5/$points
}
}
$xy = array();
// for the number of points...
for ($i=0; $i$points; $i++) {
// compute a point on the perimeter $i/$points from the beginning
$x = round$radiuscos(2*pi()*$i$points$fudge)+$radius+2);
$y = round$radiussin(2*pi()*$i$points$fudge)+$radius+2);
$xy[]=array$x$y);
}
for ($from=0; $fromcount$xy); $from++) {
// where to draw the line to
$to = ($from$skip+1) % (count$xy));
// draw the line
imageline$image, $xy$from][0], $xy$from][1], $xy$to][0], $xy$to][1], $color);
}
return $image
}