function area_circle ($radius
{
return $radius$radiuspi();
}
Category: Algorithm
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: History For Algorithm magic8ball
eXorithm – Execute Algorithm: Algorithms Beginning with D
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: Algorithms Beginning with F
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
}