1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
| <?php
class Matrice {
      private $_tab = array();
      private $_iCpt = 0;
      private $_iNbLignes = -1;
      private $_tabChaine = array();
      public function ajouterLigne ($ligne, $x1, $x2, $x3, $x4) {
            $this->_tab[$ligne]['x1'] = $x1;
            $this->_tab[$ligne]['x2'] = $x2;
            $this->_tab[$ligne]['x3'] = $x3;
            $this->_tab[$ligne]['x4'] = $x4;
      }
      public function trouverCoordonnees($x1 = 0, $x2 = 0, $x3 = 0, $x4 = 0, $coord_a = true, $coord_b = true) {
            // Si le nombre de ligne n'a pas été calculé
            if ($this->_iNbLignes == -1) {
                  $this->_iNbLignes = count($this->_tab);
            }
            // Si il y a qu'une ligne, on sort !
            if ($this->_iNbLignes == 1) {
                  return;
            }
            // Si pour la premiere ligne
            if (count($this->_tabChaine) == 0) {
                  $this->_tabChaine[] = $this->_tab[0];
                  $this->_iCpt++;
                  $this->trouverCoordonnees(      $this->_tabChaine[0]['x1'],
                                                            $this->_tabChaine[0]['x2'],
                                                            $this->_tabChaine[0]['x3'],
                                                            $this->_tabChaine[0]['x4']);
                  return $this->_tabChaine;
            }
            // Pour toutes les lignes du tableau original
            for ( ; $this->_iCpt < $this->_iNbLignes ; $this->_iCpt++) {
                  // Pour la premiere coordonnée
                  if ((($coord_a === true) && ($this->_tab[$this->_iCpt]['x1'] == $x1) && ($this->_tab[$this->_iCpt]['x2'] == $x2))
                        ||
                        (($coord_b === true) && ($this->_tab[$this->_iCpt]['x1'] == $x3) && ($this->_tab[$this->_iCpt]['x2'] == $x4))) {
                        // On copie
                        $this->_tabChaine[] = $this->_tab[$this->_iCpt];
                        $a = $this->_tab[$this->_iCpt]['x3'];
                        $b = $this->_tab[$this->_iCpt]['x4'];
                        $this->_iCpt++;
                        // On recherche l'autre coordonnée
                        $this->trouverCoordonnees(0, 0, $a, $b, false, true);
                  }
                  // Pour la seconde
                  elseif ((($coord_a === true) && ($this->_tab[$this->_iCpt]['x3'] == $x1) && ($this->_tab[$this->_iCpt]['x4'] == $x2))
                              ||
                              (($coord_b === true) && ($this->_tab[$this->_iCpt]['x3'] == $x3) && ($this->_tab[$this->_iCpt]['x4'] == $x4))) {
                        // On copie
                        $this->_tabChaine[] = $this->_tab[$this->_iCpt];
                        $a = $this->_tab[$this->_iCpt]['x1'];
                        $b = $this->_tab[$this->_iCpt]['x2'];
                        $this->_iCpt++;
                        // On recherche l'autre coordonnée
                        $this->trouverCoordonnees($a, $b, 0, 0, true, false);
                  }
            }
      }
}
?> |
Partager