<?php
/**
* 判断4个点是否组成矩形
* @param [type] $list [description]
* @return boolean [description]
*/
function isJuXing($list){
$v = [];
$distance = [];
for ($i=0; $i < 3 ; $i++) {
for ($j=$i+1; $j <=3; $j++) {
//TODO 判断节点是否一致
if($list[$i] == $list[$j]){
return false;
}
//TODO 向量赋值
$v[$i.'-'.$j] = [$list[$j][0] - $list[$i][0], $list[$j][1] - $list[$i][1]];
//距离赋值
$temp = getDistanceNoSqrt($v[$i.'-'.$j]);
$distance[$temp][] = $i.'-'.$j;
}
}
$length = count($distance);
if( !in_array($length, [2,3]) ){
return false;
}
ksort($distance);
$keys = array_keys($distance);
if($length == 2){
if(2 * $keys[0] != $keys[1]){
return false;
}
//TODO 判读$v两两相乘为0
$temp = $distance[$keys[0]];
for($i=0; $i<3; $i++){
for($j=$i+1; $j<4; $j++){
$key1 = $temp[$i];
$key2 = $temp[$j];
$res = $v[$key1][0] * $temp[$key2][0] + $temp[$key1][1] * $temp[$key2][1];
if($res != 0){
return false;
}
}
}
} else {
if($keys[0] + $keys[1] != $keys[2]){
return false;
}
$v1 = $distance[$keys[0]];
$v2 = $distance[$keys[1]];
foreach ($v1 as $key => $value) {
foreach ($v2 as $k => $val) {
if( !isVertices($v[$value], $v[$val]) ){
return false;
}
}
}
}
return true;
}
$list = [[0,0], [0,1], [2,1], [2,0]];
if( isJuXing($list) ){
echo "right";
} else {
echo "error";
}