eXorithm – Execute Algorithm: View / Run Algorithm render_polygons

function render_polygons ($polygons$vertex_color$face_color$wireframe$dashes$image_size$scale
{
  foreach ($polygons as $polygon) {
    if (!is_array$polygon)) {
      throw new Exception'Each polygon must be a list.');
    } else if ((count$polygon)%3)!=0) {
      throw new Exception'Each polygon must be a list like x1, y1, z1, x2, y2, z2, etc. The number of points therefore must be divisible by three.');
    }
  }
  
  if (is_array$vertex_color)) {
    if (count$vertex_color) != count$polygons)) {
      throw new Exception'If vertex colors is an array, it must contain the same number of colors as the number of polygons.');
    }
  }
  
  if (is_array$face_color)) {
    if (count$face_color) != count$polygons)) {
      throw new Exception'If face colors is an array, it must contain the same number of colors as the number of polygons.');
    }
  }
  
  // if scale=0 then we auto-scale
  if ($scale==0) {
    $max = 0;
    for ($i=0; $icount$polygons); $i++) {
      for ($j=0; $jcount$polygons$i]); $j$j+3) {  
        if (abs$polygons$i][$j])>$max
          $max = abs$polygons$i][$j]);
        if (abs$polygons$i][$j+1])>$max
          $max = abs$polygons$i][$j+1]);
      }
    }
    if ($max>0)
      $scale = ($image_size-2)/($max*2);
  }
  
  // the polygon arrays (x,y,z) must be converted into shapes (x,y)
  $shapes = array();
  $z_max = array();
  
  for ($i=0; $icount$polygons); $i++) {
    $max = $polygons$i][2];
    for ($j=0; $jcount$polygons$i]); $j$j+3) {  
      $x = $polygons$i][$j];
      $y = $polygons$i][$j+1];
      
      // map each x,y coord to a screen position
      $x = round$image_size/2 + $x$scale);
      $y = round$image_size/2 - $y$scale);
      
      $shapes$i][$j] = $x
      $shapes$i][$j+1] = $y
      
      // keep track of the maximum z-value for each shape
      if ($polygons$i][$j+2]>$max
        $max = $polygons$i][$j+2];
    }
    $shapes$i] = array_values$shapes$i]);
    $z_max$i] = $max
  }
  
  // create a blank image
  $image = image_create_alpha$image_size$image_size);
  
  // create the colors
  if (!is_array$vertex_color))
    $vertex_color = array_fill(0, count$polygons), $vertex_color);
  if (!is_array$face_color))
    $face_color = array_fill(0, count$polygons), $face_color);
  
  // painter's algorithm - draw farther polygons first
  array_multisort$z_max, SORT_DESC, $shapes$face_color$vertex_color);
  
  // draw the polygons
  for ($i=0; $icount$shapes); $i++) {
    $v_color = allocate_color$image$vertex_color$i]);
    $f_color = allocate_color$image$face_color$i]);
    if (!$wireframe) {
      imagefilledpolygon$image$shapes$i], count$shapes$i])/2, $f_color);
    }
    imagepolygon$image$shapes$i], count$shapes$i])/2, $v_color);
  }
  
  // draw dashes - BUGGY
  if ($dashes) {
    for ($i=0; $icount$shapes); $i++) {
      $v_color = allocate_color$image$vertex_color$i]);
      $style = array$v_color, IMG_COLOR_TRANSPARENT, IMG_COLOR_TRANSPARENT, IMG_COLOR_TRANSPARENT, IMG_COLOR_TRANSPARENT);
      imagesetstyle$image$style);
      imagepolygon$image$shapes$i], count$shapes$i])/2, IMG_COLOR_STYLED);
    }
  }
  
  return $image

eXorithm – Execute Algorithm: View / Run Algorithm convert2base

function convert2base ($num$base
{
  $symbols = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz+/'
  
  if (($base<2) || ($basestrlen$symbols)))
    throw new Exception'Base must be between 2 and 'strlen$symbols));
  
  $result = ''
  while ($num > 0) {
    $result = $symbols$num$base].$result
    $num = floor$num$base);
  } 
  return $result

eXorithm – Execute Algorithm: View / Run Algorithm prime_factors

function prime_factors ($number
{
  $factors = array();
  while$number > 1)
  {
    $prime = 2;
    while$number % $prime != 0)
    {
      $prime = next_prime$prime);
    }
    if$prime > floor$number/2))
    {
      $factors[] = $number
      break
    }
    $factors[] = $prime
    $number = $number$prime
  }
  return $factors

eXorithm – Execute Algorithm: Discuss Algorithm weather_forecast

weather_forecast     version 1.4     Display the weather for a location. Look up using the Google weather API.Subject: API no longer available

Mike Campbell posted: Sep 14, 2012 09:56 am [reply]

Google has discontinued their API — http://thenextweb.com/google/2012/08/28/did-google-just-quietly-kill-private-weather-api/

Subject: weather_forecast

Doug posted: Sep 12, 2012 05:38 am [reply]

I can’t seem to get it to work either here or my website. Is it possible that I need a Google key?

Doug posted: Sep 12, 2012 05:40 am [reply]

The Zip Code is 45830, The City is Columbus Grove, Ohio, USA

New Comment

eXorithm – Execute Algorithm: Embed Algorithm rot13


Embed This Algorithm

This page will help you embed the algorithm rot13 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.
string
Embed the form Embed only the output

eXorithm – Execute Algorithm: View / Run Algorithm approx_date_diff

function approx_date_diff ($date1$date2
{
  $times = array
    'year' => round(365.2425 * 86400),
    'month' => round(365.2425 * 86400 / 12),
    'week' => 7 * 86400,
    'day' => 86400,
    'hour' => 3600,
    'minute' => 60,
    'second' => 1,
  );
  
  $diff = abs$date1 - $date2);
  
  $str = '0 seconds'
  foreach ($times as $unit => $secs) {
    $num = $diff$secs
    
    if ($num>=1) {
      $num = round$num);
      if ($num>=2) $unit .= 's'
      $str = "$num $unit"
      break
    }
  }
  
  return $str