eXorithm – Execute Algorithm: View / Run Algorithm binary_search

function binary_search ($array$item$low$high
{
  if ($low==-1) $low = 0;
  if ($high==-1) $high = count$array)-1;
  
  if ($low > $high) {
    // item not found
    return -1;  
  }  
  
  // get the middle
  $middle = floor(($low$high)/2);
  if ( $array$middle] == $item ) {
    // found it
    return $middle
  } elseif ($item < $array$middle]) {
    // search left
    return binary_search$array$item$low$middle-1);
  } else {
    // search right
    return binary_search$array$item$middle+1, $high);
  }

eXorithm – Execute Algorithm: History For Algorithm draw_cube

draw_cube     version 0.8     Draw a 3d cube.
Version Note Created Diff
0.8 [edit Oct 29, 2010 10:20 am by Mike Campbell
0.7 [revert add rainbow option Oct 26, 2010 02:21 pm by Mike Campbell
0.6 [revert Oct 15, 2010 12:37 am by Mike Campbell
0.5 [revert Oct 14, 2010 11:24 pm by Mike Campbell
0.4 [revert support for solid sides using painter’s algorithm Oct 14, 2010 09:20 am by Mike Campbell
0.3 [revert Oct 12, 2010 10:40 am by Mike Campbell
0.2 [revert Oct 12, 2010 10:31 am by Mike Campbell
0.1 [revert Oct 12, 2010 10:29 am by Mike Campbell

eXorithm – Execute Algorithm: View / Run Algorithm draw_cube

function draw_cube ($image_size$degree_x$degree_y$degree_z$vdist$dist$vertex_color$face_color$wireframe$dashes$rainbow
{
  $degree_x = $degree_x % 360;
  $degree_y = $degree_y % 360;
  $degree_z = $degree_z % 360;
  
  // construct the cube polygons
  $size = 400; // the size is arbitrary
  
  $x1$size/2;
  $x0=-($size/2);
  $y1$size/2;
  $y0=-($size/2);
  $z1$size/2;
  $z0=-($size/2);
  
  $sides = array();
  
  $sides[] = array$x0$y0$z0$x0$y0$z1$x0$y1$z1$x0$y1$z0);
  $sides[] = array$x1$y0$z0$x1$y0$z1$x1$y1$z1$x1$y1$z0);
  
  $sides[] = array$x0$y0$z0$x0$y0$z1$x1$y0$z1$x1$y0$z0);
  $sides[] = array$x0$y1$z0$x0$y1$z1$x1$y1$z1$x1$y1$z0);
  
  $sides[] = array$x0$y0$z0$x0$y1$z0$x1$y1$z0$x1$y0$z0);
  $sides[] = array$x0$y0$z1$x0$y1$z1$x1$y1$z1$x1$y0$z1);
  
  // project each of the 6 polygons that makes up the cube
  for ($i=0; $icount$sides); $i++) {
    $points[] = project_polygon$sides$i], $degree_x$degree_y$degree_z, 0, 0, 0, $vdist+($size/2), $dist+($size/2), true);
  }
  
  // scale the image somewhat
  $scale = $image_size/($size*1.8);
  
  if ($rainbow) {
    $face_color = array'ff0000''00d000''ffff00''a000a0''0000ff''FF8040');
  }
  
  return render_polygons$points$vertex_color$face_color$wireframe$dashes$image_size$scale);

eXorithm – Execute Algorithm: Embed Algorithm fibonacci_binet

Embed This Algorithm

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

eXorithm – Execute Algorithm: View / Run Algorithm point_in_polygon

function point_in_polygon ($point, $polygon_points
{
  // check points
  if ((count$polygon_points)%2)!=0) {
    throw new Exception'The points must be a list like x1, y1, x2, y2, etc. The number of points therefore must be divisible by two.');
  }
  
  // count the number of times a flat line originating from the point and moving right crosses
  // the edges of the polygon -- even number: outside polygon, odd number: inside polygon
  $ints = 0;
  
  $count = count$polygon_points);
  
  $x = $point[0];
  $y = $point[1];
  
  for ($i=2;$i<=$count$i$i+2) {
    $vertex1x = $polygon_points$i-2]; 
    $vertex1y = $polygon_points$i-1]; 
    $vertex2x = $polygon_points$i % count$polygon_points)];
    $vertex2y = $polygon_points[($i+1) % count$polygon_points)];
    // boundary condition: if the point is one of the vertices then we are inside
    if (($x == $vertex1x) && ($y == $vertex1y)) {
      return true;
    }
    if ($vertex1y == $vertex2y) { // horizontal edge
      // boundary condition: if point is on a horizontal polygon edge then we are inside
      if (($vertex1y == $y) && ($x > min$vertex1x, $vertex2x)) && ($x < max$vertex1x, $vertex2x))) {
        return true;
      }
    } else {
      if (($y > min$vertex1y, $vertex2y)) && ($y <= max$vertex1y, $vertex2y)) && ($x <= max$vertex1x, $vertex2x))) { 
        $xinters = ($y - $vertex1y) * ($vertex2x - $vertex1x) / ($vertex2y - $vertex1y) + $vertex1x; 
        // boundary condition: if point is on the polygon edge then we are inside
        if ($x == $xinters) {
          return true;
        }
        if ($x < $xinters) { 
          $ints++; 
        }
      } 
    } 
  }
  
  // if line crosses edges even number of times, then we are outside polygon
  if (($ints % 2) == 0) {
    return false;
  } else {
    return true;
  }
} 

eXorithm – Execute Algorithm: Embed Algorithm draw_mandelbrot


Embed This Algorithm

This page will help you embed the algorithm draw_mandelbrot 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.
width Argument info
Embed the form Embed only the output