php函数,通过平面内的四个坐标点判断是否是正方形
程序员文章站
2022-03-02 10:54:30
...
1,html页提交坐标点
<form action="./a.php" method="post">
L1:x<input type="text" name="p1[]" />
y<input type="text" name="p1[]" /><br>
L2:x<input type="text" name="p2[]" />
y<input type="text" name="p2[]" /><br>
L3:x<input type="text" name="p3[]" />
y<input type="text" name="p3[]" /><br>
L4:x<input type="text" name="p4[]" />
y<input type="text" name="p4[]" /><br>
<button>提交</button>
</form>
2.PHP中对数据进行逻辑处理
function iszf($p){
$t1 = array_shift($p);
foreach ($p as $key => $v) {
if($t1['0'] == $v['0']){
$h1[] = abs($t1['1']-$v['1']);
}elseif ($t1['1'] == $v['1']) {
$h1[] = abs($t1['0']-$v['0']);
}else{
$h1['duijue'] = sqrt(pow(abs($t1['0']-$v['0']),2)+pow(abs($t1['1']-$v['1']),2));
}
}
$t2 = array_shift($p);
foreach ($p as $key => $v) {
if($t2['0'] == $v['0']){
$h2[] = abs($t2['1']-$v['1']);
}elseif ($t2['1'] == $v['1']) {
$h2[] = abs($t2['0']-$v['0']);
}else{
$h2['duijue'] = sqrt(pow(abs($t2['0']-$v['0']),2)+pow(abs($t2['1']-$v['1']),2));
}
}
$t3 = array_shift($p);
foreach ($p as $key => $v) {
if($t3['0'] == $v['0']){
$h3[] = abs($t3['1']-$v['1']);
}elseif ($t3['1'] == $v['1']) {
$h3[] = abs($t3['0']-$v['0']);
}else{
$h3['duijue'] = sqrt(pow(abs($t3['0']-$v['0']),2)+pow(abs($t3['1']-$v['1']),2));
}
}
$dd = false;
if(array_key_exists('duijue',$h2)){
if($h2['duijue'] == $h1['duijue']){
if($h1['0']== $h1['1'] && $h1['1']==$h3['0'] && $h3['0'] == $h3['1']){
$dd = true;
}
}
}
if(array_key_exists('duijue',$h3)){
if($h3['duijue'] == $h1['duijue']){
if($h1['0']== $h1['1'] && $h1['1']==$h2['0'] && $h2['0'] == $h2['1']){
$dd = true;
}
}
}
return $dd;
}
$p[] = $_POST['p1'];
$p[] = $_POST['p2'];
$p[] = $_POST['p3'];
$p[] = $_POST['p4'];
var_dump(iszf($p));
上一篇: 向量定义Vector
下一篇: 判断四个点是否构成矩形