universe['size'] = 99; $this->universe['nb_sector'] = 150; $this->universe['min_range'] = 5; } public function sectorTooClose($sector, $array_sector) { $quad = $this->universe['min_range'] * $this->universe['min_range']; foreach ($array_sector as $sector_temp) { if ($sector['id'] == $sector_temp['id']) { continue; } if ((($sector['pos_x'] - $sector_temp['pos_x']) * ($sector['pos_x'] - $sector_temp['pos_x']) + ($sector['pos_y'] - $sector_temp['pos_y']) * ($sector['pos_y'] - $sector_temp['pos_y'])) < $quad) { return true; } } return false; } public function makeUniverse() { $id = 1; $tab = array(); $query = "INSERT INTO sector (idSector, pos_x, pos_y, name) VALUES "; while ($id <= $this->universe['nb_sector']) { $x = array(); $y = array(); $pos_x = mt_rand(0, $this->universe['size']); $pos_y = mt_rand(0, $this->universe['size']); if ($x == NULL && $y == NULL) { $sector = array('id' => $id, 'pos_x'=>$pos_x, 'pos_y'=>$pos_y, 'name'=> 'secteur'.$pos_x.$pos_y); if($this->sectorTooClose($sector, $tab)) continue; $temp[$pos_x][$pos_y] = TRUE; $tab [$id] = $sector; if($id <> 1) $query .= ", "; $query .= "('".$id."', '".$pos_x."', '".$pos_y."', 'secteur".$pos_x.$pos_y."')"; $id++; } } return $query; } } $test = new Universe(); echo $test->MakeUniverse(); ?>