任意的四个点,判断是不是矩形

<?php
/*
功能:求随意四个点是否能组成四边形

给你四个坐标点,判断它们能不能组成一个矩形,如判断([0,0],[0,1],[1,1],[1,0])能组成一个矩形。

我们分析这道题, 给4个标点,判断是否矩形

高中知识,矩形有4条边,两两相等, 矩形两条对角线相等, 矩形的长短边与对角线满足勾股定理。

故解题思路为,根据坐标点,

  列出所有的两点组合边长的数组,去重,看是不是只剩 3个长度(注意正方形2个长度)

  判断是否满足勾股定理

  调优一下,先判断有没有重复的点,有的话肯定不是矩形

*/

代码如下:

function isRectangle($point1, $point2, $point3, $point4){
if ($point1 == $point2 || $point1 == $point3 || $point1 == $point4 || $point2 == $point3 || $point2 == $point4 || $point3 == $point4) {
return false;
}
$lengthArr = [];
$lengthArr[] = getLengthSquare($point1, $point2);
$lengthArr[] = getLengthSquare($point1, $point3);
$lengthArr[] = getLengthSquare($point1, $point4);
$lengthArr[] = getLengthSquare($point2, $point3);
$lengthArr[] = getLengthSquare($point2, $point4);
$lengthArr[] = getLengthSquare($point3, $point4);

$lengthArr = array_unique($lengthArr);
$lengthCount = count($lengthArr);
if ($lengthCount == 3 || $lengthCount == 2 ) {
if ($lengthCount == 2) {
return(max($lengthArr) == 2*min($lengthArr));
} else {
$maxLength = max($lengthArr);
$minLength = min($lengthArr);
$otherLength = array_diff($lengthArr, [$maxLength, $minLength]);
return($minLength + $otherLength == $maxLength);
}
} else {
return false;
}
}

function getLengthSquare($point1, $point2){
$res = pow($point1[0]-$point2[0], 2)+pow($point1[1]-$point2[1], 2);
return $res;
}

var_dump(isRectangle([0,0],[0,1],[1,1],[1,0]));

感谢https://www.cnblogs.com/jwcrxs/p/8986120.html此博客的分享

posted @ 2018-09-11 11:20  Typical_me  阅读(2173)  评论(0编辑  收藏  举报