function degtorad ($degrees
{
  return ($degrees * (pi()/180.0));
}Â
Category: View
eXorithm – Execute Algorithm: View / Run Algorithm address_elevation
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');
}
}
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
}
eXorithm – Execute Algorithm: View / Run Algorithm ascii_art
function ascii_art ($img
{
$width = imagesx$img);
$height = imagesy$img);
$ascii = '<pre style="font-size:8px;line-height:1px;letter-spacing:-2px;">'
for$y=0;$y$height$y++)
{
for$x=0;$x$width$x++)
{
$c = imagecolorat$img, $x, $y);
$r = ($c >> 16) & 0xFF;
$g = ($c >> 8) & 0xFF;
$b = $c & 0xFF;
$t = ($c >> 24) & 0xFF;
if ($t>=0)
{
/** transparency **/
$r = $r+(255-$r)*$t/127;
$g = $g+(255-$g)*$t/127;
$b = $b+(255-$b)*$t/127;
}
$r = floor$r/16);
$g = floor$g/16);
$b = floor$b/16);
$ascii .= '<span style="color:#'dechex$r).dechex$g).dechex$b).'">#</span>'
}
$ascii .= '<br>'
}
$ascii .= '</pre>'
return $ascii
}
eXorithm – Execute Algorithm: View / Run Algorithm project_polygon
function project_polygon ($points, $degree_x, $degree_y, $degree_z, $center_x, $center_y, $center_z, $dist1, $dist2, $include_z
{
  // check points
  if ((count$points)%3)!=0) {
    throw new Exception'The points must be a list like x1, y1, z1, x2, y2, z2, etc. The number of points therefore must be divisible by three.');
  }
 Â
  $degree_x = deg2rad$degree_x);
  $degree_y = deg2rad$degree_y);
  $degree_z = deg2rad$degree_z);
 Â
  $cosx = cos$degree_x);
  $sinx = sin$degree_x);
  $cosy = cos$degree_y);
  $siny = sin$degree_y);
  $cosz = cos$degree_z);
  $sinz = sin$degree_z);
 Â
  $array = array();
 Â
  for ($i=0;$icount$points);$i$i+3) {
    $x0 = $points$i]-$center_x
    $y0 = $points$i+1]-$center_y
    $z0 = $points$i+2]-$center_z
   Â
    $x1 = $cosy*($sinz$y0 + $cosz$x0) - $siny$z0
    $y1 = $sinx*($cosy$z0 + $siny*($sinz$y0 + $cosz$x0)) + $cosx*($cosz$y0 - $sinz$x0);
    $z1 = $cosx*($cosy$z0 + $siny*($sinz$y0 + $cosz$x0)) - $sinx*($cosz$y0 - $sinz$x0);
 Â
    $x2 = $x1$dist1/($z1$dist1$dist2);
    $y2 = $y1$dist1/($z1$dist1$dist2);
    $z2 = $z1$dist1/($z1$dist1$dist2);
 Â
    $array[] = $x2
    $array[] = $y2
    if ($include_z) $array[] = $z2
  }
 Â
  return $array
}Â