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');
}
}
Tag: Tag
eXorithm – Execute Algorithm: View / Run Algorithm invert_image
function invert_image ($image
{
$image_width = imagesx$image);
$image_height = imagesy$image);
// if the image is not true color, make it so
if (!imageistruecolor$image)) {
$image2 = imagecreatetruecolor$image_width, $image_height);
imagecopy$image2$image,0,0,0,0,$image_width$image_height);
$image = $image2
}
// loop through all the pixels
for ($h = 0; $h < $image_height; $h++) {
for ($w = 0; $w < $image_width; $w++) {
// get the color at this pixel
$color = imagecolorsforindex$image, imagecolorat$image, $w, $h));
// invert the color
$color'red'] = 255 - $color'red'];
$color'green'] = 255 - $color'green'];
$color'blue'] = 255 - $color'blue'];
// create the new color
$new_color = imagecolorallocate$image, $color'red'], $color'green'], $color'blue']);
// set the color
imagesetpixel$image, $w, $h, $new_color);
}
}
return $image
}
eXorithm – Execute Algorithm: View / Run Algorithm lathe_shape
function lathe_shape ($equation, $start_x, $end_x, $detail, $degree_x, $degree_y, $degree_z, $face_color, $vertex_color, $scale, $image_size
{
$vdist = 20;
$dist = 20;
$sides = lathe_polygons$equation, $start_x, $end_x, $detail);
// project each of the polygons
for ($i=0; $icount$sides); $i++) {
$points[] = project_polygon$sides$i], $degree_x, $degree_y, $degree_z, ($end_x$start_x)/2, 0, 0, 20, 20, true);
}
// scale the image somewhat
$scale = $image_size$scale/($end_x$start_x);
return render_polygons$points, $vertex_color, $face_color, false, false, $image_size, $scale);
}
eXorithm – Execute Algorithm: View / Run Algorithm kmp_search
function kmp_search ($x, $y
{
// see http://www-igm.univ-mlv.fr/~lecroq/string/node8.html
// set-up phase
$i = 0;
$j = -1;
$kmpNext = array();
while ($i < $m) {
while ($j > -1 && $x[i] != $x[j])
$j = $kmpNext$j];
$i++;
$j++;
if ($x$i] == $x$j])
$kmpNext$i] = $kmpNext$j];
else
$kmpNext$i] = $j
}
// search phase
$i = 0;
$j = 0;
$m = strlen$x);
$n = strlen$y);
$results = array();
while ($j < $n) {
while ($i > -1 && $x$i] != $y$j])
$i = $kmpNext$i];
$i++;
$j++;
if ($i >= $m) {
$results[] = $j$i+1;
$i = $kmpNext$i];
}
}
return $results
}
eXorithm – Execute Algorithm: View / Run Algorithm magic8ball
function magic8ball ()
{
$answers =array'It is certain', 'It is decidedly so', 'Without a doubt'
'Yes – definitely', 'You may rely on it', 'As I see it, yes'
'Most likely', 'Outlook good', 'Signs point to yes', 'Yes'
'Reply hazy, try again', 'Ask again later'
'Better not tell you now', 'Cannot predict now'
'Concentrate and ask again', 'Don't bet on it'
'My reply is no', 'My sources say no', 'Outlook not so good'
'Very doubtful' );
$index = rand(0, count$answers));
return ($answers$index]);
}
eXorithm – Execute Algorithm: View / Run Algorithm isbn_validate
function isbn_validate ($isbn, $type
{
$okay = false;
$isbn = str_replacearray'-'' '), '', $isbn);
// check ISBN 13
if ($type!=2)
{
if (strlen$isbn)==13)
{
$sum=0;
$charsokay=true;
for ($i=0;$i<12;$i++)
{
if (($i%2)==1)
$weight=3;
else
$weight=1;
if (is_numeric$isbn$i]))
$sum += $weight$isbn$i];
else
$charsokay=false;
}
if ($charsokay
if ($sum>0)
if ((10-($sum % 10)) % 10==$isbn[12])
$okay = true;
}
}
// check ISBN 10
if ($type!=1)
{
if (strlen$isbn)==10)
{
$sum=0;
$charsokay=true;
for ($i=0;$i<10;$i++)
{
if (is_numeric$isbn$i]))
$sum += ($i+1)*$isbn$i];
else
{
if ((strtolower$isbn$i])=='x') && ($i==9))
$sum += 100;
else
$charsokay=false;
}
}
if ($charsokay
if ($sum>0)
if (($sum % 11)==0)
$okay = true;
}
}
return $okay
}
eXorithm – Execute Algorithm: View / Run Algorithm isbn_validator
function isbn_validator ($isbn
{
$isbnpreg_replace'/[^dX]/'''$isbn); //remove all non-numeric or X chars
$casestrlen$isbn);
switch ($case
{
case 10:
if(!isbn_validate$isbn, 2))
return 0; //Failure - not valid ISBN
else
$isbnconv_isbn$isbn);
break
case 13:
$isbnpreg_replace'/[^d]/'''$isbn); //remove all non-numeric chars
if(!isbn_validate$isbn, 1))
return 0; //Failure - not valid ISBN
break
default
return 0;
}
$isbnisbn_hyphenate$isbn);
return $isbn
}
eXorithm – Execute Algorithm: View / Run Algorithm morse
function morse ($data, $direction
{
$codes = array
'a' => '.-'
'b' => '-...'
'c' => '-.-.'
'd' => '-...'
'e' => '.'
'f' => '..-.'
'g' => '--.'
'h' => '....'
'i' => '..'
'j' => '.---'
'k' => '-.-'
'l' => '.-..'
'm' => '--'
'n' => '-.'
'o' => '---'
'p' => '.--.'
'q' => '--.-'
'r' => '.-.'
's' => '...'
't' => '-'
'u' => '..-'
'v' => '...-'
'w' => '.--'
'x' => '-..-'
'y' => '-.--'
'z' => '--..'
'0' => '-----'
'1' => '.----'
'2' => '..---'
'3' => '...--'
'4' => '....-'
'5' => '.....'
'6' => '-....'
'7' => '--...'
'8' => '---..'
'9' => '----.'
',' => '--..--'
'.' => '.-.-.-'
'?' => '..--..'
);
$output = ''
if ($direction=='text') {
$codes = array_flip$codes);
$elements = explode' ', $data);
foreach ($elements as $element) {
if (array_key_exists$element, $codes))
$output .= $codes$element];
}
} else {
$data = strtolower$data);
$elements = str_split$data);
foreach ($elements as $element) {
if (array_key_exists$element, $codes))
$output .= $codes$element].' '
}
$output = trim$output);
}
return $output
}
eXorithm – Execute Algorithm: View / Run Algorithm number2roman
function number2roman ($num
{
// http://www.go4expert.com/forums/showthread.php?t=4948
// Make sure that we only use the integer portion of the value
$n = intval$num);
$result = ''
// Declare a lookup array that we will use to traverse the number:
$lookup = array'M' => 1000, 'CM' => 900, 'D' => 500, 'CD' => 400,
'C' => 100, 'XC' => 90, 'L' => 50, 'XL' => 40,
'X' => 10, 'IX' => 9, 'V' => 5, 'IV' => 4, 'I' => 1);
foreach ($lookup as $roman => $value)
{
// Determine the number of matches
$matches = intval$n / $value);
// Store that many characters
$result .= str_repeat$roman, $matches);
// Substract that from the number
$n = $n % $value
}
// The Roman numeral should be built, return it
return $result
}
eXorithm – Execute Algorithm: View / Run Algorithm lathe_polygons
function lathe_polygons ($equation, $start_x, $end_x, $detail
{
$degrees = 360/$detail
$step = ($end_x$start_x)/$detail
$max_y = 0;
for ($ii=0;$ii<=$detail$ii++) {
$x = $start_x+($ii$step);
$y0 = evaluate_for_x$equation, $x);
if (!is_nan$y0) && !is_infinite$y0)) {
if ($y0 > $max_y) $max_y = $y0
for ($jj=0;$jj$detail$jj++) {
$y = $y0 * sindeg2rad$degrees$jj));
$z = $y0 * cosdeg2rad$degrees$jj));
// the top of the shape
/*if ($ii==0) {
$sides[-1][] = $x;
$sides[-1][] = $y;
$sides[-1][] = $z;
}*/
// the bottom of the shape
/*if ($ii==$detail) {
$sides[-2][] = $x;
$sides[-2][] = $y;
$sides[-2][] = $z;
}*/
// the sides of the shape
if ($ii$detail) {
$poly = $ii$detail$jj+1;
$sides$poly][6] = $x
$sides$poly][7] = $y
$sides$poly][8] = $z
$poly = $ii$detail+(($jj$detail-1) % $detail)+1;
$sides$poly][9] = $x
$sides$poly][10] = $y
$sides$poly][11] = $z
}
// the sides of the shape
if ($ii>0) {
$poly = ($ii-1)*$detail$jj+1;
$sides$poly][3] = $x
$sides$poly][4] = $y
$sides$poly][5] = $z
$poly = ($ii-1)*$detail+(($jj$detail-1) % $detail)+1;
$sides$poly][0] = $x
$sides$poly][1] = $y
$sides$poly][2] = $z
}
}
}
}
$sides = array_values$sides);
return $sides
}