function random_usort ($a, $b
{
return mt_rand(-1,1);
}
Category: View
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: 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
}
eXorithm – Execute Algorithm: View / Run Algorithm volume_prolate_spheroid
function volume_prolate_spheroid ($polar_radius$equatorial_radius
{
$volume = (4/3) * pi() * ($equatorial_radius * $equatorial_radius) * $polar_radius
return $volume
}
eXorithm – Execute Algorithm: View / Run Algorithm draw_mandelbrot
function draw_mandelbrot ($width
{
$widthround$width);
$heightround$width*2/3.5);
// create image
$image = image_create_alpha$width, $height);
$max_iteration = 100;
// color pallet
$colorsarray();
for ($i=0;$i$max_iteration$i++) {
$shade = 60+round$i$max_iteration*195);
$colors$i] = imagecolorallocate$image, $shade, $shade, $shade);
}
// the "inside" color
$colors$max_iteration] = imagecolorallocate$image, 0, 0, 0);
// loop through all the pixels
for ($h = 0; $h < $height; $h++) {
for ($w = 0; $w < $width; $w++) {
// normalize the location of the pixel
$x0 = (3.5 * $w / $width)-2.5; // between -2.5 and 1
$y0 = (2 * $h / $height)-1; // between -1 and 1
$x = 0;
$y = 0;
$iteration = 0;
// the calculation
while ( ($x$x + $y$y <= (2*2)) && ($iteration < $max_iteration) ) {
$xtemp = $x$x - $y$y + $x0
$y = 2*$x$y + $y0
$x = $xtemp
$iteration = $iteration + 1;
}
// set color
imagesetpixel$image, $w, $h, $colors$iteration]);
}
}
return $image
}
eXorithm – Execute Algorithm: View / Run Algorithm pie_chart
function pie_chart ($data
{
$size=200;
$centerx = 100;
$centery = 100;
$circle_diameter = 180;
$image = image_create_alpha$size, $size);
$blackimagecolorallocatealpha$image, 0, 0, 0, 0);
$total = array_sum$data);
$n = 0;
$start = 0;
foreach ($data as $key => $value) {
$length = 360 * $value$total
$color = allocate_color$image, generate_graph_color$n++));
imagefilledarc$image, $centerx, $centery, $circle_diameter, $circle_diameter, round$start), round$start$length), $color, IMG_ARC_PIE);
$start += $length
}
imageellipse$image, $centerx, $centery, $circle_diameter, $circle_diameter, $black);
return $image
}
eXorithm – Execute Algorithm: View / Run Algorithm moon_cycle
function moon_cycle ($date
{
$d = getdate$date);
$year = $d'year'];
$month = $d'mon'];
$day = $d'mday'];
$r = $year % 100;
$r %= 19;
if ($r>9)
$r -= 19;
$r = (($r * 11) % 30) + $month + $day
if ($month<3)
$r += 2;
$r -= (($year<2000) ? 4 : 8.3);
$r = floor$r+0.5)%30;
return ($r < 0) ? $r+30 : $r
}
eXorithm – Execute Algorithm: View / Run Algorithm rotate_image_alpha
function rotate_image_alpha ($image, $angle, $bgcolor, $bgtransparency
{
// seen at http://www.php.net/manual/en/function.imagerotate.php
$srcw = imagesx$image);
$srch = imagesy$image);
//Normalize angle
$angle %= 360;
//Set rotate to clockwise
$angle = -$angle
if$angle == 0) {
return $image
}
// Convert the angle to radians
$theta = deg2rad ($angle);
// Standard case of rotate
if ( (abs$angle) == 90) || (abs$angle) == 270) ) {
$width = $srch
$height = $srcw
if ( ($angle == 90) || ($angle == -270) ) {
$minX = 0;
$maxX = $width
$minY = -$height+1;
$maxY = 1;
$sin = 1;
} else if ( ($angle == -90) || ($angle == 270) ) {
$minX = -$width+1;
$maxX = 1;
$minY = 0;
$maxY = $height
$sin = -1;
}
$cos = 0;
} else if (abs$angle) === 180) {
$width = $srcw
$height = $srch
$minX = -$width+1;
$maxX = 1;
$minY = -$height+1;
$maxY = 1;
$sin = 0;
$cos = -1;
} else {
$sin = sin$theta);
$cos = cos$theta);
// Calculate the width of the destination image.
$temp = array (0,
$srcw * $cos
$srch * $sin
$srcw * $cos + $srch * $sin
);
$minX = floormin$temp));
$maxX = ceilmax$temp));
$width = $maxX - $minX
// Calculate the height of the destination image.
$temp = array (0,
$srcw * $sin * -1,
$srch * $cos
$srcw * $sin * -1 + $srch * $cos
);
$minY = floormin$temp));
$maxY = ceilmax$temp));
$height = $maxY - $minY
}
$destimg = imagecreatetruecolor$width, $height);
$bgcolor = allocate_color$destimg, $bgcolor, $bgtransparency);
imagefill$destimg, 0, 0, $bgcolor);
imagesavealpha$destimg, true);
// sets all pixels in the new image
for$x$minX; $x$maxX; $x++) {
for$y$minY; $y$maxY; $y++) {
// fetch corresponding pixel from the source image
$srcX = round$x * $cos - $y * $sin);
$srcY = round$x * $sin + $y * $cos);
if$srcX >= 0 && $srcX < $srcw && $srcY >= 0 && $srcY < $srch) {
$color = imagecolorat$image, $srcX, $srcY);
} else {
$color = $bgcolor
}
imagesetpixel$destimg, $x$minX, $y$minY, $color);
}
}
return $destimg
}