Advertisement
olamedia

box

Dec 30th, 2018
512
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.70 KB | None | 0 0
  1. <?php
  2.  
  3. class box{
  4.     public $w = 768;
  5.     public $h = 512;
  6.     public $sx = 101;
  7.     public $sy = 62;
  8.     public $bounce = [];
  9.     public $bounceX0 = [];
  10.     public $bounceY0 = [];
  11.     public $bounceX1 = [];
  12.     public $bounceY1 = [];
  13.     const BL = 'BL', BR = 'BR', UL = 'UL', UR = 'UR';
  14.     function w(){
  15.         return $this->w - $this->sx;
  16.     }
  17.     function h(){
  18.         return $this->h - $this->sy;
  19.     }
  20.     function crossX($prevX, $prevY, $pd, $y){
  21.         // ax + b = y
  22.         // a = 1
  23.         $a = ($pd == box::UL || $pd == box::BR)?1:-1;
  24.         $b = $prevY - $a * $prevX;
  25.         return ($y - $b)/$a;
  26.     }
  27.     function crossY($prevX, $prevY, $pd, $x){
  28.         // ax + b = y
  29.         // a = 1
  30.         $a = ($pd == box::UL || $pd == box::BR)?1:-1;
  31.         $b = $prevY - $a * $prevX;
  32.         return ($a*$x + $b);
  33.     }
  34.     function xInRange($x){
  35.         return $this->w() >= $x && $x >= 0;
  36.     }
  37.     function yInRange($y){
  38.         return $this->h() >= $y && $y >= 0;
  39.     }
  40.     function inCorner($x, $y){
  41.         return ($this->h() == $y || $y == 0) && ($this->w() == $x || $x == 0);
  42.     }
  43.     function prevBounce($px, $py, $pd = null){
  44.         $x = null;
  45.         $y = null;
  46.         $crossX = false;
  47.         $w = $this->w();
  48.         $h = $this->h();
  49.         if ($px == 0 && $py == 0){
  50.             $pd = box::UL;
  51.         }
  52.         if ($pd == box::UL || $pd == box::UR){
  53.             $y = $h;
  54.             $d = ($pd == box::UL)?box::BL:box::BR;
  55.             $x = $this->crossX($px, $py, $pd, $y);
  56.         }
  57.         if ($pd == box::BL || $pd == box::BR){
  58.             $y = 0;
  59.             $d = ($pd == box::BL)?box::UL:box::UR;
  60.             $x = $this->crossX($px, $py, $pd, $y);
  61.         }
  62.         $k = $x.'_'.$y.'_'.$d;
  63.         //var_dump($pd.'<-'.$x.'x'.$y.'_'.$d.' '.$w.'x'.$h.($this->xInRange($x)?'t':'f').($this->yInRange($y)?'t':'f'));
  64.         if (!$this->xInRange($x) || !$this->yInRange($y)){
  65.             $crossX = false;
  66.             if ($pd == box::UL || $pd == box::BL){ // from right to left
  67.                 $x = $w;
  68.                 $d = ($pd == box::UL)?box::UR:box::BR;
  69.                 $y = $this->crossY($px, $py, $pd, $x);
  70.             }
  71.             if ($pd == box::UR || $pd == box::BR){ // from left to right
  72.                 $x = 0;
  73.                 $d = ($pd == box::UR)?box::UL:box::BL;
  74.                 $y = $this->crossY($px, $py, $pd, $x);
  75.             }
  76.            // var_dump($pd.'<-'.$x.'x'.$y.'_'.$d.' '.$w.'x'.$h.($this->xInRange($x)?'t':'f').($this->yInRange($y)?'t':'f'));
  77.         }
  78.         $k = $x.'_'.$y.'_'.$d;
  79.         //echo $k."\r\n";
  80.         return [$x, $y, $d];
  81.     }
  82.     function multiBounce($px, $py, $pd = null, $n = 0){
  83.        
  84.         $k = $px.'_'.$py.'_'.$pd;
  85.         echo $n.' '.$k."\r\n";
  86.         if ($n > 0 && $this->inCorner($px, $py)){
  87.             echo 'corner'."\r\n";
  88.             //return [$this->bounceX0, $this->bounceY0, $this->bounceX1, $this->bounceY1];
  89.         }
  90.         if (isset($this->bounce[$k])){
  91.             echo 'cycle'."\r\n";
  92.             return [$this->bounceX0, $this->bounceY0, $this->bounceX1, $this->bounceY1];
  93.             //$this->bounce,
  94.         }
  95.         $this->bounce[$k] = $n;
  96.         if ($px == 0){
  97.             $this->bounceX0[$py][] = $k;
  98.         }
  99.         if ($py == 0){
  100.             $this->bounceY0[$px][] = $k;
  101.         }
  102.         if ($px == $this->w()){
  103.             $this->bounceX1[$py][] = $k;
  104.         }
  105.         if ($py == $this->h()){
  106.             $this->bounceY1[$px][] = $k;
  107.         }
  108.         list($x, $y, $d) = $this->prevBounce($px, $py, $pd);
  109.         return $this->multiBounce($x, $y, $d, $n+1);
  110.     }
  111. }
  112. $b = new box();
  113. $b->multiBounce(404, 256, box::BR);
  114. //var_dump($b->w()*2+$b->h()*2, );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement