function area_circle ($radius
{
return $radius$radiuspi();
}
Category: View
eXorithm – Execute Algorithm: View / Run Algorithm calculate_e
function calculate_e ($accuracy
{
$e = 0;
$factorial = 1;
for$i=1;$i<=$accuracy$i++) {
$e += 1 / $factorial
$factorial *= $i
}
return $e
}
eXorithm – Execute Algorithm: View / Run Algorithm number_string
function number_string ($number
{
$names = array
0=>"zero", 1=>"one", 2=>"two", 3=>"three", 4=>"four"
5=>"five", 6=>"six", 7=>"seven", 8=>"eight", 9=>"nine"
10=>"ten", 11=>"eleven", 12=>"twelve", 13=>"thirteen", 14=>"fourteen"
15=>"fifteen", 16=>"sixteen", 17=>"seventeen", 18=>"eighteen", 19=>"nineteen"
20=>"twenty", 30=>"thirty", 40=>"forty", 50=>"fifty"
60=>"sixty", 70=>"seventy", 80=>"eighty", 90=>"ninety"
);
$triplets = array
100=>"hundred"
1000=>"thousand"
1000000=>"million"
1000000000=>"billion"
1000000000000=>"trillion"
1000000000000000=>"quadrillion"
1000000000000000000=>"quintillion"
1000000000000000000000=>"sextillion"
1000000000000000000000000=>"septillion"
1000000000000000000000000000=>"octillion"
1000000000000000000000000000000=>"nonillion"
);
krsort$names);
krsort$triplets);
if (abs$number) > PHP_INT_MAX)
throw new Exception'Maximum integer size for this version of PHP is '.PHP_INT_MAX);
$prefix = ''
$str = ''
if ($number<0) {
$prefix = 'negative '
$number = $number*-1;
}
$remainder = 0;
if ($number>=100) {
foreach ($triplets as $num=>$name) {
if ($num<=$number) {
$str = $name
$remainder = $number % $num
$number = floor$number$num);
break
}
}
// how many of the triplets?
$str = number_string$number).' '$str
if ($remainder<100) $separator = ' and '
else $separator = ', '
} else {
foreach ($names as $num=>$name) {
if ($num<=$number) {
$str = $name
$remainder = $number % $num
break
}
}
$separator = '-'
}
// remainder to tack on?
if ($remainder>0) {
$str = $str$separatornumber_string$remainder);
}
return $prefix$str
}
eXorithm – Execute Algorithm: View / Run Algorithm draw_bezier
function draw_bezier ($p0, $p1, $p2, $p3, $detail, $show_bounds
{
$x0$p0[0];$x1$p1[0];$x2$p2[0];$x3$p3[0];
$y0$p0[1];$y1$p1[1];$y2$p2[1];$y3$p3[1];
$cx=3*($x1$x0);
$bx=3*($x2$x1)-$cx
$ax$x3$x0$cx$bx
$cy=3*($y1$y0);
$by=3*($y2$y1)-$cy
$ay$y3$y0$cy$by
// generate the bezier points
for$i=0; $i<=$detail; $i++) {
$t = $i$detail
$x = $ax$t$t$t$bx$t$t$cx$t$x0
$y = $ay$t$t$t$by$t$t$cy$t$y0
$points$i] = array$x$y);
}
$width=400;
$height=400;
$imageimage_create_alpha$width$height);
$cimagecolorallocate$image,0,0,0);
$c2imagecolorallocate$image,200,200,200);
// determine scale so curve shows within our image
$minxmin$x0$x1$x2$x3);
$maxxmax$x0$x1$x2$x3);
$minymin$y0$y1$y2$y3);
$maxymax$y0$y1$y2$y3);
if ($maxx==$minx
$scale = ($height-1)/($maxy$miny);
else if ($maxy==$miny
$scale = ($width-1)/($maxx$minx);
else
$scale = min(($width-1)/($maxx$minx), ($height-1)/($maxy$miny));
if ($show_bounds) {
imageline$image, ($x0$minx)*$scale, ($y0$miny)*$scale, ($x1$minx)*$scale, ($y1$miny)*$scale, $c2);
imageline$image, ($x1$minx)*$scale, ($y1$miny)*$scale, ($x2$minx)*$scale, ($y2$miny)*$scale, $c2);
imageline$image, ($x2$minx)*$scale, ($y2$miny)*$scale, ($x3$minx)*$scale, ($y3$miny)*$scale, $c2);
}
// draw the bezier
for ($i=1;$icount$points);$i++) {
imageline$image, ($points$i-1][0]-$minx)*$scale, ($points$i-1][1]-$miny)*$scale, ($points$i][0]-$minx)*$scale, ($points$i][1]-$miny)*$scale, $c);
}
return $image
}
eXorithm – Execute Algorithm: View / Run Algorithm locate_country
function locate_country ($country
{
$data = file_get_contents"http://maps.google.com/maps/geo?output=csv&q="urlencode$country));
$arr = explode",", $data);
if (count$arr)>=4) {
if ($arr[0]==200) {
return array'latitude'=>$arr[2], 'longitude'=>$arr[3]);
} else {
throw new Exception'Country could not be geocoded');
}
} else {
throw new Exception'Country could not be geocoded');
}
}
eXorithm – Execute Algorithm: View / Run Algorithm flip_image
function flip_image ($image, $mode
{
$width = imagesx$image);
$height = imagesy$image);
$src_x = 0;
$src_y = 0;
$src_width = $width
$src_height = $height
switch ( $mode ) {
case 'vertical': //vertical
$src_y = $height -1;
$src_height = -$height
break
case 'horizontal': //horizontal
$src_x = $width -1;
$src_width = -$width
break
case 'both': //both
$src_x = $width -1;
$src_y = $height -1;
$src_width = -$width
$src_height = -$height
break
default
return $image
}
$imgdest = imagecreatetruecolor$width, $height);
imagealphablending$imgdest, false);
imagesavealpha$imgdest, true);
if (imagecopyresampled$imgdest, $image, 0, 0, $src_x, $src_y , $width, $height, $src_width, $src_height)) {
return $imgdest
}
return $image
}
eXorithm – Execute Algorithm: View / Run Algorithm draw_upc_barcode
function draw_upc_barcode ($number, $show_numbers
{
$number = str_replacearray'-'' '), '', $number);
if (strlen$number)!=12)
throw new Exception"UPC number must have 12 digits");
for ($i=0;$i<12;$i++) {
if (!is_numeric$number$i]))
throw new Exception"UPC number must contain only digits");
}
$lcodes = array
'0001101'
'0011001'
'0010011'
'0111101'
'0100011'
'0110001'
'0101111'
'0111011'
'0110111'
'0001011'
);
$rcodes = array
'1110010'
'1100110'
'1101100'
'1000010'
'1011100'
'1001110'
'1010000'
'1000100'
'1001000'
'1110100'
);
$code = '101'
for ($i=0;$i<6;$i++) {
$code .= $lcodes$number$i]];
}
$code .= '01010'
for ($i=6;$i<12;$i++) {
$code .= $rcodes$number$i]];
}
$code .= '101'
// create image
$width=190;
$height=100;
$image = image_create_alpha$width, $height);
$white = imagecolorallocate$image, 255, 255, 255);
imagefilledrectangle$image, 0, 0, $width, $height, $white);
// draw lines
$black = imagecolorallocate$image, 0, 0, 0);
for ($i=0;$istrlen$code);$i++) {
if ($code$i]=='1') {
imageline$image, $i*2,0, $i*2, $height, $black);
imageline$image, $i*2+1,0, $i*2+1, $height, $black);
}
}
// draw numbers
if ($show_numbers) {
imagefilledrectangle$image, 6, $height-16, 90, $height, $white);
imagefilledrectangle$image, 98, $height-16, 182, $height, $white);
for ($i=0;$i<6;$i++) {
imagestring$image, 2, 11+$i*14, $height-14, $number$i], $black);
}
for ($i=6;$i<12;$i++) {
imagestring$image, 2, 19+$i*14, $height-14, $number$i], $black);
}
}
return $image
}
eXorithm – Execute Algorithm: View / Run Algorithm simple_calculator
function simple_calculator ($number1, $operator, $number2
{
// calculator
switch ($operator) {
case "plus"
$res = $number1$number2
break
case "minus"
$res = $number1$number2
break
case "times"
$res = $number1$number2
break
case "divided by"
$res = $number1$number2
break
default
$res = 0;
}
return $res
}
eXorithm – Execute Algorithm: View / Run Algorithm raise_polygon
function raise_polygon ($points, $height, $direction, $color
{
// check points
if ((count$points)==0) || ((count$points)%2)!=0)) {
throw new Exception'The points must be an array with an even number of integers.');
}
// determine the extent of the polygon
$maxx = $points[0];
$maxy = $points[1];
$minx = $points[0];
$miny = $points[1];
for ($i=2; $icount$points); $i$i+2) {
$x = $points$i];
$y = $points$i+1];
if ($x$maxx) $maxx = $x
if ($y$maxy) $maxy = $y
if ($x$minx) $minx = $x
if ($y$miny) $miny = $y
}
// determine the extent of the projection
$proj_length = roundsqrt$height$height/2));
switch ($direction) {
case 'nw'
$projx = $proj_length*-1;
$projy = $proj_length*-1;
break
case 'ne'
$projx = $proj_length
$projy = $proj_length*-1;
break
case 'sw'
$projx = $proj_length*-1;
$projy = $proj_length
break
default
$projx = $proj_length
$projy = $proj_length
}
// determine where the polygon should be positioned
for ($i=0; $icount$points); $i$i+2) {
$points$i] = $points$i]-$minx+1;
$points$i+1] = $points$i+1]-$miny+1;
if ($projx<0) $points$i] = $points$i] - $projx
if ($projy<0) $points$i+1] = $points$i+1] - $projy
}
// blank image
$image = image_create_alpha$maxx$minx$proj_length+2, $maxy$miny$proj_length+2);
// create the colors
$r = hexdecsubstr$color, 0, 2));
$g = hexdecsubstr$color, 2, 2));
$b = hexdecsubstr$color, 4, 2));
$color = imagecolorallocate$image, $r, $g, $b);
// draw the first polygon
imagepolygon$image, $points, count$points)/2, $color);
// draw the lines + modify the points
for ($i=0; $icount$points); $i$i+2) {
imageline$image, $points$i], $points$i+1], $points$i]+$projx, $points$i+1]+$projy, $color);
$points$i] = $points$i]+$projx
$points$i+1] = $points$i+1]+$projy
}
// draw the second polygon
imagepolygon$image, $points, count$points)/2, $color);
return $image
}
eXorithm – Execute Algorithm: View / Run Algorithm show_address
function show_address ($address
{
$data = file_get_contents"http://maps.googleapis.com/maps/api/geocode/json?address="urlencode$address)."&sensor=false");
$obj = json_decode$data);
if ($obj) {
if (isset$obj->results[0]->geometry->location)) {
$loc = $obj->results[0]->geometry->location;
return array'latitude'=>$loc->lat, 'longitude'=>$loc->lng);
} else {
throw new Exception'Lookup failed and/or address does not exist!');
}
} else {
throw new Exception'Lookup failed and/or address does not exist!');
}
}