Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class box{
- public $w = 768;
- public $h = 512;
- public $sx = 101;
- public $sy = 62;
- public $bounce = [];
- public $bounceX0 = [];
- public $bounceY0 = [];
- public $bounceX1 = [];
- public $bounceY1 = [];
- const BL = 'BL', BR = 'BR', UL = 'UL', UR = 'UR';
- function w(){
- return $this->w - $this->sx;
- }
- function h(){
- return $this->h - $this->sy;
- }
- function crossX($prevX, $prevY, $pd, $y){
- // ax + b = y
- // a = 1
- $a = ($pd == box::UL || $pd == box::BR)?1:-1;
- $b = $prevY - $a * $prevX;
- return ($y - $b)/$a;
- }
- function crossY($prevX, $prevY, $pd, $x){
- // ax + b = y
- // a = 1
- $a = ($pd == box::UL || $pd == box::BR)?1:-1;
- $b = $prevY - $a * $prevX;
- return ($a*$x + $b);
- }
- function xInRange($x){
- return $this->w() >= $x && $x >= 0;
- }
- function yInRange($y){
- return $this->h() >= $y && $y >= 0;
- }
- function inCorner($x, $y){
- return ($this->h() == $y || $y == 0) && ($this->w() == $x || $x == 0);
- }
- function prevBounce($px, $py, $pd = null){
- $x = null;
- $y = null;
- $crossX = false;
- $w = $this->w();
- $h = $this->h();
- if ($px == 0 && $py == 0){
- $pd = box::UL;
- }
- if ($pd == box::UL || $pd == box::UR){
- $y = $h;
- $d = ($pd == box::UL)?box::BL:box::BR;
- $x = $this->crossX($px, $py, $pd, $y);
- }
- if ($pd == box::BL || $pd == box::BR){
- $y = 0;
- $d = ($pd == box::BL)?box::UL:box::UR;
- $x = $this->crossX($px, $py, $pd, $y);
- }
- $k = $x.'_'.$y.'_'.$d;
- //var_dump($pd.'<-'.$x.'x'.$y.'_'.$d.' '.$w.'x'.$h.($this->xInRange($x)?'t':'f').($this->yInRange($y)?'t':'f'));
- if (!$this->xInRange($x) || !$this->yInRange($y)){
- $crossX = false;
- if ($pd == box::UL || $pd == box::BL){ // from right to left
- $x = $w;
- $d = ($pd == box::UL)?box::UR:box::BR;
- $y = $this->crossY($px, $py, $pd, $x);
- }
- if ($pd == box::UR || $pd == box::BR){ // from left to right
- $x = 0;
- $d = ($pd == box::UR)?box::UL:box::BL;
- $y = $this->crossY($px, $py, $pd, $x);
- }
- // var_dump($pd.'<-'.$x.'x'.$y.'_'.$d.' '.$w.'x'.$h.($this->xInRange($x)?'t':'f').($this->yInRange($y)?'t':'f'));
- }
- $k = $x.'_'.$y.'_'.$d;
- //echo $k."\r\n";
- return [$x, $y, $d];
- }
- function multiBounce($px, $py, $pd = null, $n = 0){
- $k = $px.'_'.$py.'_'.$pd;
- echo $n.' '.$k."\r\n";
- if ($n > 0 && $this->inCorner($px, $py)){
- echo 'corner'."\r\n";
- //return [$this->bounceX0, $this->bounceY0, $this->bounceX1, $this->bounceY1];
- }
- if (isset($this->bounce[$k])){
- echo 'cycle'."\r\n";
- return [$this->bounceX0, $this->bounceY0, $this->bounceX1, $this->bounceY1];
- //$this->bounce,
- }
- $this->bounce[$k] = $n;
- if ($px == 0){
- $this->bounceX0[$py][] = $k;
- }
- if ($py == 0){
- $this->bounceY0[$px][] = $k;
- }
- if ($px == $this->w()){
- $this->bounceX1[$py][] = $k;
- }
- if ($py == $this->h()){
- $this->bounceY1[$px][] = $k;
- }
- list($x, $y, $d) = $this->prevBounce($px, $py, $pd);
- return $this->multiBounce($x, $y, $d, $n+1);
- }
- }
- $b = new box();
- $b->multiBounce(404, 256, box::BR);
- //var_dump($b->w()*2+$b->h()*2, );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement