<?php
require_once 'PHP_Compat-1.3.0/PHP_Compat-1.3.0/Compat/Function/array_udiff.php';
$coord_bag_1 = array(array("x"=>1.2, "y"=>1.3), array("x"=>2.4, "y"=>2.6),
array("x"=>3.6, "y"=>3.9), array("x"=>4.8, "y"=>5.2));
$coord_bag_2 = array(array("x"=>2.4, "y"=>2.6), array("x"=>3.6, "y"=>3.9),
array("x"=>4.8, "y"=>5.2), array("x"=>1.2, "y"=>1.2));
global $g_tolerance;
$g_tolerance = 0.00001;
/*
------------------------------------------------------------------
A comparison function for two floating point numbers
that returns 0 if the numbers are equal given a tolerance value,
-1 if the second number is below the tolerance range
and +1 if the number is above the tolerance range.
*/
function compare_float($in_float_1, $in_float_2)
{
global $g_tolerance;
$delta = (real)($g_tolerance);
$middle = (real)($in_float_1);
$upper = $middle + $delta;
$lower = $middle - $delta;
$candidate_value = (real)($in_float_2);
if($candidate_value < $lower )
{
return -1;
}
else if( $upper < $candidate_value)
{
return 1;
}
else
{
return 0;
}
}
/*
------------------------------------------------------------------
A comparison function for two two-dimensional cartesian coordinates
that returns 0 if the coordinates are equal given a tolerance value,
-1 if the second number is below the tolerance range
and +1 if the number is above the tolerance range.
A little fiddle is done if one component of a coordinate
is inside the range while the other component is outside the range.
*/
function compare_coord_2D($in_coord_1, $in_coord_2)
{
$comparison_x = compare_float($in_coord_1["x"], $in_coord_2["x"]);
$comparison_y = compare_float($in_coord_1["y"], $in_coord_2["y"]);
if($comparison_x == 0 && $comparison_y == 0)
{
return 0;
}
else if($comparison_x == 0)
{
return $comparison_y;
}
else if($comparison_y == 0)
{
return $comparison_x;
}
else
{
$product = $comparison_x * $comparison_y;
return $product;
}
return 0;
}
/*
------------------------------------------------------------------
This function compares the two sets (bags) of coordinates.
If the number of coordinates in each set is not equal the
sets are not considered to be equal.
*/
function are_coord_bags_equal($in_coord_bag_1, $in_coord_bag_2)
{
if(count($in_coord_bag_1) == count($in_coord_bag_2))
{
$diff_array = array_udiff($in_coord_bag_2, $in_coord_bag_1,
"compare_coord_2D");
//var_dump($difference_array);
return (count($difference_array) == 0);
}
else
{
return false;
}
}
/*------------------------------------------------------------------
The test procedure.
Test 100 times.
Shuffle one of the input arrays
and compare the shuffled array with the original
array each time.
The test should return true irrespective of the
order of the elements in the two arrays.
*/
function test()
{
global $coord_bag_1;
global $coord_bag_2;
$count = 100;
for($index = 0; $index < $count; $index++)
{
shuffle($coord_bag_2);
var_dump($coord_bag_2);
echo("<BR>\n");
$is_equal = are_coord_bags_equal($coord_bag_1, $coord_bag_2);
echo("Equal: " . ($is_equal ? "TRUE" : "FALSE"));
echo("<BR?>\n");
echo(" \n");
echo("<BR>\n");
}
}
?>
|