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); ?>
相关文章推荐
- 一个关于if else容易迷惑的问题
- PHP5.2.*防止Hash冲突拒绝服务攻击的Patch
- 深入理解PHP之匿名函数
- JSP/PHP基于Ajax的分页功能实现
- 关于PHP通过PDO用中文条件查询MySQL的问题。
- 什么是设计模式
- PHP数据库长连接mysql_pconnect的细节
- Php Installing An Expansion
- Laravel5.3 表单验证中文报错提示语言包
- rem : web app适配的秘密武器
- jquery高级应用之Deferred对象
- 关于浮动与清除浮动,你应该知道的
- 数组reduce方法的高级技巧
- php7 读取php.ini[4]
- 用批处理解决数学问题的代码第1/4页
- PHP+Apache在Windows 9x下的安装和配置
- IIS 6 的 PHP 最佳配置方法
- 安装Apache和PHP的一些补充