<?php
/**
* draw_triangle
*
* Draw a filled triangle.
*
* @version 0.4
* @author Contributors at eXorithm
* @link /algorithm/view/draw_triangle Listing at eXorithm
* @link /algorithm/history/draw_triangle History at eXorithm
* @license /home/show/license
*
* @param array $points This should be a list of six numbers that define the points of the triange (x1, y1, x2, y2, x3, y3)
* @param string $color (hex color code) The color of the triangle
* @return resource GD image
*/
function draw_triangle($points=array(0=>'0',1=>'0',2=>'0',3=>'8',4=>'8',5=>'4'),$color='000000')
{
if (count($points)!=6) {
throw new Exception('The points must be an array of 6 integers.');
}
$image = image_create_alpha(max($points[0], $points[2], $points[4])+1, max($points[1], $points[3], $points[5])+1);
$red = hexdec(substr($color, 0, 2));
$green = hexdec(substr($color, 2, 2));
$blue = hexdec(substr($color, 4, 2));
$color = imagecolorallocatealpha($image, $red, $green, $blue, 0);
imagefilledpolygon($image, $points, 3, $color);
return $image;
}
/**
* image_create_alpha
*
* Helper function to create a new blank image with transparency.
*
* @version 0.1
* @author Contributors at eXorithm
* @link /algorithm/view/image_create_alpha Listing at eXorithm
* @link /algorithm/history/image_create_alpha History at eXorithm
* @license /home/show/license
*
* @param mixed $width
* @param mixed $height
* @return resource GD image
*/
function image_create_alpha($width='',$height='')
{
// Create a normal image and apply required settings
$img = imagecreatetruecolor($width, $height);
imagealphablending($img, false);
imagesavealpha($img, true);
// Apply the transparent background
$trans = imagecolorallocatealpha($img, 0, 0, 0, 127);
for ($x = 0; $x < $width; $x++)
{
for ($y = 0; $y < $height; $y++)
{
imagesetpixel($img, $x, $y, $trans);
}
}
return $img;
}
?>
Tag: String
Weather Forecast
<?php
/**
* weather_forecast
*
* Display the weather for a location. Look up using the Google weather API.
*
* @version 1.4
* @author Contributors at eXorithm
* @link /algorithm/view/weather_forecast Listing at eXorithm
* @link /algorithm/history/weather_forecast History at eXorithm
* @license /home/show/license
*
* @param mixed $location The location to get the weather for. Can be a city, zip code, or postal code.
* @param mixed $units The units to get the temperature in.
* @param string $header_color (hex color code) Color of the header
* @param string $day_header_color (hex color code) Color of the header for each of the days
* @param string $day_color (hex color code) Color of the cells containing the forecasts
* @return string HTML
*/
function weather_forecast($location='Sydney, Australia',$units='c',$header_color='f0f0f0',$day_header_color='d0ffd0',$day_color='f0f0f0')
{
$weather = file_get_contents('http://www.google.com/ig/api?weather='.urlencode($location));
$xml = simplexml_load_string($weather);
if (!isset($xml->weather->forecast_conditions)) {
throw new Exception('Data could not be retreived for location '.$location);
}
$location = $xml->weather->forecast_information->city['data'];
// four day outlook
for ($i = 0; $i < 4; $i++){
if ($xml->weather->forecast_conditions->$i) {
$forecast_day[] = $xml->weather->forecast_conditions->$i->day_of_week['data'];
$forecast_condition[] = $xml->weather->forecast_conditions->$i->condition['data'];
$low = $xml->weather->forecast_conditions->$i->low['data'];
if ($units=='k')
$low = round((($low - 32)*5/9) + 273, 1);
else if ($units=='c')
$low = round(($low - 32)*5/9, 1);
$forecast_low[] = $low;
$high = $xml->weather->forecast_conditions->$i->high['data'];
if ($units=='k')
$high = round((($high - 32)*5/9) + 273, 1);
else if ($units=='c')
$high = round(($high - 32)*5/9, 1);
$forecast_high[] = $high;
$forecast_icon[] = $xml->weather->forecast_conditions->$i->icon['data'];
}
}
// current
$condition = $xml->weather->current_conditions->condition['data'];
$temp = $xml->weather->current_conditions->temp_f['data'];
if ($units=='k')
$temp = round((($temp - 32)*5/9) + 273, 1);
else if ($units=='c')
$temp = round(($temp - 32)*5/9, 1);
$icon = $xml->weather->current_conditions->icon['data'];
// build the HTML
$header = "<tr><td colspan=\"".count($forecast_day)."\" bgcolor=\"#$header_color\">";
if ($icon!='')
$header .= "<img align=\"left\" src=\"http://www.google.com$icon\">";
$header .= "<b>$location</b><br>Currently <i>$condition</i> <b>$temp$units</b>";
$header .= "</td></tr>\n<tr>";
$data = "<tr>\n";
for ($i = 0; $i < count($forecast_day); $i++){
$header .= "<td width=\"130\" bgcolor=\"#$day_header_color\"><b>$forecast_day[$i]</b></td>";
$data .= "<td bgcolor=\"#$day_color\">";
$data .= "<img src=\"http://www.google.com$forecast_icon[$i]\">";
$data .= "<br><i>$forecast_condition[$i]</i>";
$data .= "<br>high <b>$forecast_high[$i]$units</b>";
$data .= "<br>low <b>$forecast_low[$i]$units</b>";
$data .= "</td>\n";
}
$header .= "</tr>";
$data .= "</tr>";
return "<table cellpadding=\"5\" cellspacing=\"3\">\n$header\n$data\n</table>";
}
?>
eXorithm – Execute Algorithm: History For Algorithm weather_forecast
weather_forecast    version 1.4    Display the weather for a location. Look up using the Google weather API.
Address Elevation
<?php
/**
* address_elevation
*
* Returns the elevation (in meters) above sea level for an address.
*
* @version 0.2
* @author Contributors at eXorithm
* @link /algorithm/view/address_elevation Listing at eXorithm
* @link /algorithm/history/address_elevation History at eXorithm
* @license /home/show/license
*
* @param mixed $address The address to get the elevation for.
* @return mixed
*/
function address_elevation($address='Denver, Colorado')
{
// get the lat/long for this address
$data = file_get_contents("http://maps.google.com/maps/geo?output=csv&q=".urlencode($address));
$arr = explode(",", $data);
if (count($arr)>=4) {
if ($arr[0]==200) {
// get the elevation for this lat/long
$data = file_get_contents("http://maps.googleapis.com/maps/api/elevation/xml?sensor=false&locations=".$arr[2].','.$arr[3]);
$obj = simplexml_load_string($data);
if ($obj instanceof SimpleXMLElement) {
$obj = (array) $obj;
$obj = $obj['result'];
if ($obj instanceof SimpleXMLElement) {
$obj = (array) $obj;
return $obj['elevation'];
} else {
throw new Exception('Elevation lookup failed');
}
} else {
throw new Exception('Elevation lookup failed');
}
} else {
throw new Exception('Address lookup failed');
}
} else {
throw new Exception('Address lookup failed');
}
}
?>
eXorithm – Execute Algorithm: History For Algorithm draw_triangle
draw_triangle    version 0.4    Draw a filled triangle.
Create Gradient
<?php
/**
* create_gradient
*
* Create an image that is a color gradient.
*
* @version 0.1
* @author Contributors at eXorithm
* @link /algorithm/view/create_gradient Listing at eXorithm
* @link /algorithm/history/create_gradient History at eXorithm
* @license /home/show/license
*
* @param string $start_color (hex color code) The start color of the gradient.
* @param string $end_color (hex color code) The end color of the gradient.
* @param number $size The length of the resulting image.
* @param number $thickness The thickness of the resulting image.
* @param mixed $orientation The orientation of the gradient.
* @return resource GD image
*/
function create_gradient($start_color='ffffff',$end_color='000000',$size=100,$thickness=5,$orientation='')
{
if ($orientation=="vertical") {
$img=imagecreatetruecolor($thickness,$size);
} else {
$img=imagecreatetruecolor($size,$thickness);
}
$start_r = hexdec(substr($start_color, 0, 2));
$start_g = hexdec(substr($start_color, 2, 2));
$start_b = hexdec(substr($start_color, 4, 2));
$end_r = hexdec(substr($end_color, 0, 2));
$end_g = hexdec(substr($end_color, 2, 2));
$end_b = hexdec(substr($end_color, 4, 2));
for ($i=0;$i<$size;$i++) {
$red = round($start_r - ($start_r-$end_r) * ($i / ($size-1)));
$green = round($start_g - ($start_g-$end_g) * ($i / ($size-1)));
$blue = round($start_b - ($start_b-$end_b) * ($i / ($size-1)));
$color = imagecolorallocate($img, $red, $green, $blue);
if ($orientation=="vertical") {
for ($k=0;$k<$thickness;$k++)
imagesetpixel($img, $k, $i, $color);
} else {
for ($k=0;$k<$thickness;$k++)
imagesetpixel($img, $i, $k, $color);
}
}
return $img;
}
?>
Duotone Image
<?php
/**
* duotone_image
*
* Change an image into a tinted grayscale.
*
* @version 0.5
* @author Contributors at eXorithm
* @link /algorithm/view/duotone_image Listing at eXorithm
* @link /algorithm/history/duotone_image History at eXorithm
* @license /home/show/license
*
* @param resource $image (GD image) The image to duotone.
* @param number $rplus Red value to increase or decrease.
* @param number $gplus Green value to increase or decrease.
* @param number $bplus Blue value to increase or decrease.
* @param bool $pcnt If checked, the values for rplus, gplus and bplus will be treated as percentages.
* @return resource GD image
*/
function duotone_image($image=null,$rplus=0,$gplus=0,$bplus=60,$pcnt=false)
{
// Adapted from http://www.tuxradar.com/practicalphp/11/2/21
$imagex = imagesx($image);
$imagey = imagesy($image);
$image2 = imagecreatetruecolor($imagex, $imagey);
imagesavealpha($image2, true);
imagealphablending($image2, false);
for ($x = 0; $x <$imagex; ++$x) {
for ($y = 0; $y <$imagey; ++$y) {
$rgb = imagecolorat($image, $x, $y);
$color = imagecolorsforindex($image, $rgb);
$grey = floor(($color['red']+$color['green']+$color['blue'])/3);
if ($pcnt) {
$red = $grey + $grey*($rplus/150);
$green = $grey + $grey*($gplus/150);
$blue = $grey + $grey*($bplus/150);
} else {
$red = $grey + $rplus;
$green = $grey + $gplus;
$blue = $grey + $bplus;
}
if ($red > 255) $red = 255;
if ($green > 255) $green = 255;
if ($blue > 255) $blue = 255;
if ($red < 0) $red = 0;
if ($green < 0) $green = 0;
if ($blue < 0) $blue = 0;
$newcol = imagecolorallocatealpha($image2, $red,$green,$blue,$color['alpha']);
imagesetpixel ($image2, $x, $y, $newcol);
}
}
return $image2;
}
?>
Make Change
<?php
/**
* make_change
*
* Calculate the number of different ways there are to make change for a given amount.
*
* @version 0.1
* @author Contributors at eXorithm
* @link /algorithm/view/make_change Listing at eXorithm
* @link /algorithm/history/make_change History at eXorithm
* @license /home/show/license
*
* @param number $amount How many cents you want to make change for.
* @param array $coins The coin denominations you have.
* @return mixed
*/
function make_change($amount=100,$coins=array(0=>'1',1=>'5',2=>'10',3=>'25'))
{
$coin_count = count($coins);
$table = array();
for ($i = -1; $i <= $amount; $i++) {
for($j = -1; $j <= $coin_count; $j++) {
// Rules
// 1: table[0,0] or table[0,x] = 1
// 2: talbe[i <= -1, x] = 0
// 3: table[x, j <= -1] = 0
$total = 0;
// first sub-problem
// count(n, m-1)
$n = $i;
$m = $j-1;
if ($n == 0) // rule 1
$total += 1;
else if ($n <= -1) // rule 2
$total += 0;
else if (($m <= 0) && ($n >= 1))
$total += 0;
else
$total += $table[$n][$m];
// second sub-problem
// count(n-S[m], m)
if (($j-1) <= -1)
$total += 0;
else {
$n = $i - $coins[$j - 1];
$m = $j;
if ($n == 0) // rule 1
$total += 1;
else if ($n <= -1) // rule 2
$total += 0;
else if (($m <= 0) && ($n >= 1)) // rule 3
$total += 0;
else
$total += $table[$n][$m];
}
$table[$i][$j] = $total;
}
}
return $table[$i-1][$j-1];
}
?>
Show Address
<?php
/**
* show_address
*
* Given an address, show the location on a map.
*
* @version 0.6
* @author Contributors at eXorithm
* @link /algorithm/view/show_address Listing at eXorithm
* @link /algorithm/history/show_address History at eXorithm
* @license /home/show/license
*
* @param mixed $address The address to find.
* @return array latitude/longitude
*/
function show_address($address='The White House
1600 Pennsylvania Ave NW
Washington, DC
20500')
{
$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!');
}
}
?>
eXorithm – Execute Algorithm: History For Algorithm draw_upc_barcode
draw_upc_barcode    version 0.3    Draw a barcode for a UPC number.
