ポリゴンが整列していない場合は、整列する必要があります。これを行うには、多角形の中心 (X の平均、Y の平均) を見つけてから、行列変換によって多角形を段階的に回転させ、点を軸の 1 つに投影し、最小 stdev の角度を使用して形状を揃えます (主成分を使用することもできます)。交点を見つけるための単純なアルゴリズムは、点のグリッドを定義することです。各ポイントについて、1 つのポリゴン、または他のポリゴン、またはその両方 (ユニオン) 内のポイントの数を維持します (これには、たとえばhttp://wiki.unity3d.com/index.php?title=PolyContainsPointなどのシンプルで高速なアルゴリズムがあります。)。ポリゴン 1 とポリゴン 2 のポイントをカウントし、ポリゴン 1 またはポリゴン 2 のポイントの量で割ると、(グリッド サンプリングに応じて) ポリゴンのオーバーラップの割合が概算されます。交差領域は、AND 演算に対応する点によって与えられます。
例えば。
function get_polygon_intersection($arr, $user_array)
{
$maxx = -999; // choose sensible limits for your application
$maxy = -999;
$minx = 999;
$miny = 999;
$intersection_count = 0;
$not_intersected = 0;
$sampling = 20;
// find min, max values of polygon (min/max variables passed as reference)
get_array_extent($arr, $maxx, $maxy, $minx, $miny);
get_array_extent($user_array, $maxx, $maxy, $minx, $miny);
$inc_x = $maxx-$minx/$sampling;
$inc_y = $maxy-$miny/$sampling;
// see if x,y is within poly1 and poly2 and count
for($i=$minx; $i<=$maxx; $i+= $inc_x)
{
for($j=$miny; $j<=$maxy; $j+= $inc_y)
{
$in_arr = pt_in_poly_array($arr, $i, $j);
$in_user_arr = pt_in_poly_array($user_array, $i, $j);
if($in_arr && $in_user_arr)
{
$intersection_count++;
}
else
{
$not_intersected++;
}
}
}
// return score as percentage intersection
return 100.0 * $intersection_count/($not_intersected+$intersection_count);
}