## Draw Triangle

```<?php

/**
* draw_triangle
*
* Draw a filled triangle.
*
* @version 0.4
* @author Contributors at eXorithm
* @link https://www.exorithm.com/algorithm/view/draw_triangle Listing at eXorithm
* @link https://www.exorithm.com/algorithm/history/draw_triangle History at eXorithm
*
* @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 https://www.exorithm.com/algorithm/view/image_create_alpha Listing at eXorithm
* @link https://www.exorithm.com/algorithm/history/image_create_alpha History at eXorithm
*
* @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;
}

?>
```

## 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 https://www.exorithm.com/algorithm/view/weather_forecast Listing at eXorithm
* @link https://www.exorithm.com/algorithm/history/weather_forecast History at eXorithm
*
* @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 \$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
*/
{

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
if (\$icon!='')
\$data = "<tr>\n";

for (\$i = 0; \$i < count(\$forecast_day); \$i++){
\$data .= "<td bgcolor=\"#\$day_color\">";
\$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";
}

\$data .= "</tr>";

}

?>
```

## 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.

Version Note Created 1.4 [edit Reverted from version 1.1 Apr 23, 2012 09:40 am by Mike Campbell 1.3 [revert Mar 22, 2012 03:35 pm by jessiie 1.2 [revert Mar 22, 2012 03:24 pm by jessiie 1.1 [revert support bth celsius and kelvin Nov 23, 2011 01:43 am by Mike Campbell 1.0 [revert Celsius is now Kelvin Nov 20, 2011 11:32 pm by ekajjake 0.5 [revert Nov 20, 2011 11:32 pm by ekajjake 0.4 [revert Nov 20, 2011 11:31 pm by ekajjake 0.3 [revert Nov 20, 2011 11:30 pm by ekajjake 0.2 [revert Dec 1, 2010 02:34 pm by Mike Campbell 0.1 [revert Oct 27, 2010 02:02 pm by Mike Campbell

```<?php

/**
*
* Returns the elevation (in meters) above sea level for an address.
*
* @version 0.2
* @author Contributors at eXorithm
*
* @return mixed
*/
{
// get the lat/long for this address
\$arr = explode(",", \$data);
if (count(\$arr)>=4) {
if (\$arr[0]==200) {
// get the elevation for this lat/long
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 {
}
} else {
}
}

?>
```

## eXorithm – Execute Algorithm: History For Algorithm draw_triangle

draw_triangle Â Â Â  version 0.4 Â Â Â  Draw a filled triangle.

Version Note 0.4 [edit Dec 1, 2012 12:38 am by Mike Campbell 0.3 [revert Nov 28, 2012 06:37 am by harrison7042 0.2 [revert Sep 30, 2010 01:27 am by Mike Campbell 0.1 [revert Sep 29, 2010 01:56 pm by Mike Campbell

```<?php

/**
*
* Create an image that is a color gradient.
*
* @version 0.1
* @author Contributors at eXorithm
*
* @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
*/
{
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 https://www.exorithm.com/algorithm/view/duotone_image Listing at eXorithm
* @link https://www.exorithm.com/algorithm/history/duotone_image History at eXorithm
*
* @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)
{

\$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 https://www.exorithm.com/algorithm/view/make_change Listing at eXorithm
* @link https://www.exorithm.com/algorithm/history/make_change History at eXorithm
*
* @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];
}

?>
```

```<?php

/**
*
* Given an address, show the location on a map.
*
* @version 0.6
* @author Contributors at eXorithm
*
* @return array latitude/longitude
*/
1600 Pennsylvania Ave NW
Washington, DC
20500')
{
\$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.

Version Note Created 0.3 [edit Reverted from version 0.1 Jun 21, 2013 01:17 pm by Mike Campbell 0.2 [revert Jun 15, 2013 02:57 pm by wayneHat 0.1 [revert Jan 17, 2011 02:42 pm by Mike Campbell