您的位置:首页 > 编程语言 > PHP开发

php中实现矩阵的碰撞检测

2016-09-13 21:52 501 查看
<?php
class Rect{
private $x;
private $y;
private $width;
private $height;
function __construct($x,$y,$width,$height){
$this->x = $x;
$this->y = $y;
$this->width = $width;
$this->height = $height;
}
function panDuan($px,$py){
$x1 = $this->x;
$x2 = $this->x+$this->width;
$y1 = $this->y;
$y2 = $this->y+$this->height;
if (($px>=$x1) && ($px<=$x2) && ($py>=$y1) && ($py<= $y2)) {
echo "in";
}else{
echo "over";
}
}

function rectArc($dx1,$dx2){
//第一个矩形的左上角的x,y,width,height;
$x1 = $dx1->x;
$y1 = $dx1->y;
$w1 = $dx1->width;
$h1 = $dx1->height;

//第二个矩形的左上角的x,y,width,height;
$x2 = $dx2->x;
$y2 = $dx2->y;
$w2 = $dx2->width;
$h2 = $dx2->height;

//两个矩形的宽度的一半
$heng = $w1/2+$w2/2;

//两个矩形的高度的一半
$shu = $h1/2+$h2/2;

//第一个矩形的中心的x,y值
$jxx1 = $x1+$w1/2;
$jxy1 = $y1+$h1/2;

//第二个矩形的中心的x,y值
$jxx2 = $x2+$w2/2;
$jxy2 = $y2+$h2/2;

//判断是否横碰撞
$bool1 = abs($jxx1-$jxx2)<=$heng;
//判断是否竖碰撞
$bool2 = abs($jxy1-$jxy2)<=$shu;
if ($bool1&&$bool2) {
echo "碰撞";
}else{
echo "不碰撞";
}
}

}
$r1 = new Rect(1,1,2,2);
$r2 = new Rect(4,4,1,1);
$r1->rectArc($r1,$r2);
?>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息