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
}
Category: View
eXorithm – Execute Algorithm: View / Run Algorithm skew_image
function skew_image ($image, $skew_value
{
// note: this function was original posted by etienne on php.net.
$width = imagesx$image);
$height = imagesy$image);
$height2 = $height + ($width * $skew_value);
$imgdest = image_create_alpha$width, $height2);
// Process the image
for$x = 0, $level = 0; $x < $width - 1; $x++)
{
$floor = floor$level);
// To go faster, some lines are being copied at once
if ($level == $floor
imagecopy$imgdest, $image, $x, $level, $x, 0, 1, $height - 1);
else
{
$temp = $level - $floor
// The first pixel of the line
// We get the color then apply a fade on it depending on the level
$color1 = imagecolorsforindex$image, imagecolorat$img, $x, 0));
$alpha = $color1'alpha'] + ($temp * 127);
if ($alpha < 127)
{
$color = imagecolorallocatealpha$imgdest, $color1'red'], $color1'green'], $color1'blue'], $alpha);
imagesetpixel$imgdest, $x, $floor, $color);
}
// The rest of the line
for$y = 1; $y < $height - 1; $y++)
{
// Merge this pixel and the upper one
$color2 = imagecolorsforindex$image, imagecolorat$image, $x, $y));
$alpha = ($color1'alpha'] * $temp) + ($color2'alpha'] * (1 - $temp));
if ($alpha < 127)
{
$red = ($color1'red'] * $temp) + ($color2'red'] * (1 - $temp));
$green = ($color1'green'] * $temp) + ($color2'green'] * (1 - $temp));
$blue = ($color1'blue'] * $temp) + ($color2'blue'] * (1 - $temp));
$color = imagecolorallocatealpha$imgdest, $red, $green, $blue, $alpha);
imagesetpixel$imgdest, $x, $floor + $y, $color);
}
$color1 = $color2
}
// The last pixel of the line
$color1 = imagecolorsforindex$image, imagecolorat$image, $x, $height - 1));
$alpha = $color1'alpha'] + ((1 - $temp) * 127);
if ($alpha < 127)
{
$color = imagecolorallocatealpha$imgdest, $color1'red'], $color1'green'], $color1'blue'], $alpha);
imagesetpixel$imgdest, $x, $floor + $height - 1, $color);
}
}
// The line is finished, the next line will be lower
$level += $skew_value
}
// Finished processing, return the skewed image
return $imgdest
}
eXorithm – Execute Algorithm: View / Run Algorithm google_api_key
function google_api_key ()
{
// please note:
// This key was registered by eXorithm. It might change at any time.
// It cannot be used from a server other than exorithm.com.
// If you are using this function on your server, you may need to get your own
// key from Google.
return "AIzaSyBqKZSzmn_TMnHaIHVyEi3JS8a1mTVOLRE"
}
eXorithm – Execute Algorithm: View / Run Algorithm simple_download
function simple_download ()
{
$data = 'iVBORw0KGgoAAAANSUhEUgAAABwAAAASCAMAAAB/2U7WAAAABl'
. 'BMVEUAAAD///+l2Z/dAAAASUlEQVR4XqWQUQoAIAxC2/0vXZDr'
. 'EX4IJTRkb7lobNUStXsB0jIXIAMSsQnWlsV+wULF4Avk9fLq2r'
. '8a5HSE35Q3eO2XP1A1wQkZSgETvDtKdQAAAABJRU5ErkJggg=='
$data = base64_decode$data);
return array'extension'=>'png', 'name'=>'phprules', 'data'=>$data);
}
eXorithm – Execute Algorithm: View / Run Algorithm evaluate_equation
function evaluate_equation ($equation, $values
{
if (is_array$equation))
{
$operator = array_shift$equation);
switch ($operator
{
case '+'
return evaluate_equation$equation[0], $values)+evaluate_equation$equation[1], $values);
case '-'
return evaluate_equation$equation[0], $values)-evaluate_equation$equation[1], $values);
case '*'
return evaluate_equation$equation[0], $values)*evaluate_equation$equation[1], $values);
case '/'
return evaluate_equation$equation[0], $values)/evaluate_equation$equation[1], $values);
case '%'
return evaluate_equation$equation[0], $values)%evaluate_equation$equation[1], $values);
case '^'
return powevaluate_equation$equation[0], $values), evaluate_equation$equation[1], $values));
case 'neg'
return evaluate_equation$equation[0], $values)*-1;
case 'abs'
return absevaluate_equation$equation[0], $values));
case 'sqrt'
return sqrtevaluate_equation$equation[0], $values));
case 'log'
return logevaluate_equation$equation[0], $values), evaluate_equation$equation[1], $values));
case 'ln'
return logevaluate_equation$equation[0], $values));
case 'exp'
return expevaluate_equation$equation[0], $values));
case '!'
return 0;
case 'root'
return powevaluate_equation$equation[0], $values), 1/evaluate_equation$equation[1], $values));
// trig
case 'sin'
return sinevaluate_equation$equation[0], $values));
case 'cos'
return cosevaluate_equation$equation[0], $values));
case 'tan'
return tanevaluate_equation$equation[0], $values));
case 'sec'
return 1/cosevaluate_equation$equation[0], $values));
case 'csc'
return 1/sinevaluate_equation$equation[0], $values));
case 'cot'
return 1/tanevaluate_equation$equation[0], $values));
// hyperbolic trig
case 'sinh'
return sinhevaluate_equation$equation[0], $values));
case 'cosh'
return coshevaluate_equation$equation[0], $values));
case 'tanh'
return tanhevaluate_equation$equation[0], $values));
case 'sech'
return 1/coshevaluate_equation$equation[0], $values));
case 'csch'
return 1/sinhevaluate_equation$equation[0], $values));
case 'coth'
return 1/tanhevaluate_equation$equation[0], $values));
// arc trig
case 'arcsin'
return asinevaluate_equation$equation[0], $values));
case 'arccos'
return acosevaluate_equation$equation[0], $values));
case 'arctan'
return atanevaluate_equation$equation[0], $values));
// inverse hyperbolic trig
case 'arsinh'
return asinhevaluate_equation$equation[0], $values));
case 'arcosh'
return acoshevaluate_equation$equation[0], $values));
case 'artanh'
return atanhevaluate_equation$equation[0], $values));
default
throw new Exception'usupported operator '$operator);
}
}
else
{
if (ctype_alpha$equation))
{
if (isset$values$equation]))
{
return $values$equation];
}
else
{
throw new Exception'equation contains an unknown variable ''$equation''!');
}
}
else
{
return $equation
}
}
}
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
}
eXorithm – Execute Algorithm: View / Run Algorithm markup_urls
function markup_urls ($text
{
// split the text into words
$words = preg_split'/([snr]+)/', $text, -1, PREG_SPLIT_DELIM_CAPTURE);
$text = ""
// iterate through the words
foreach$words as $word) {
// chopword = the portion of the word that will be replaced
$chopword = $word
$chopword = preg_replace'/^[^A-Za-z0-9]*/', '', $chopword);
if ($chopword <> '') {
// linkword = the text that will replace chopword in the word
$linkword''
// does it start with http://abc. ?
if (preg_match'/^(http://)[a-zA-Z0-9_]{2,}.*/', $chopword)) {
$chopword = preg_replace'/[^A-Za-z0-9/]*$/', '', $chopword);
$linkword = '<a href="'$chopword'" target="blank">'$chopword'</a>'
// does it equal abc.def.ghi ?
} else if (preg_match'/^[a-zA-Z]{2,}.([a-zA-Z0-9_]+.)+[a-zA-Z]{2,}(/.*)?/', $chopword)) {
$chopword = preg_replace'/[^A-Za-z0-9/]*$/', '', $chopword);
$linkword = '<a href="http://'$chopword'" target="blank">'$chopword'</a>'
// does it start with [email protected] ?
} else if (preg_match'/^[a-zA-Z0-9_.]+@([a-zA-Z0-9_]{2,}.)+[a-zA-Z]{2,}.*/', $chopword)) {
$chopword = preg_replace'/[^A-Za-z0-9]*$/', '', $chopword);
$linkword = '<a href="mailto:'$chopword'">'$chopword'</a>'
}
// replace chopword with linkword in word (if linkword was set)
if ($linkword <> '') {
$word = str_replace$chopword, $linkword, $word);
}
}
// append the word
$text = $text$word
}
return $text
}
eXorithm – Execute Algorithm: View / Run Algorithm projectile_distance
function projectile_distance ($inital_velocity, $angle, $initial_height, $g
{
$angle = deg2rad$angle);
$distance = ($inital_velocitycos$angle)/$g
* ($inital_velocitysin$angle)
+ sqrtpow$inital_velocitysin$angle),2)+2*$g$initial_height));
return $distance
}
eXorithm – Execute Algorithm: View / Run Algorithm set_unix_time
function set_unix_time ($year$month$day$hour$minute$second
{
return mktime$hour$minute$second$month$day$year);
}
eXorithm – Execute Algorithm: View / Run Algorithm hailstone
function hailstone ($number
{
$result = array();
while ($number > 1) {
$result[] = $number
if ($number & 1)
$number = 3 * $number + 1;
else
$number = $number / 2;
}
$result[] = $number
return $result
}