galaxy.inc.php 17.4 KB
Newer Older
Feu's avatar
Feu committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
<?php


class galaxy
{
  var $db;
  var $dbrw;
  
  var $width;
  var $height;
  
  
  function galaxy ( &$db, &$dbrw )
  {
    $this->db = $db;
    $this->dbrw = $dbrw;
  }  
  
Feu's avatar
Feu committed
19 20 21 22 23 24 25 26
  function is_ready_public()
  {
    $req = new requete($this->db,"SELECT * FROM galaxy_star WHERE rx_star IS NOT NULL AND ry_star IS NOT NULL LIMIT 1");
    if ( $req->lines != 1 )
      return false;
    return true;
  }
  
Feu's avatar
Feu committed
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 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
  
  function init ( )
  {
    new requete($this->dbrw,"TRUNCATE `galaxy_link`");
    new requete($this->dbrw,"TRUNCATE `galaxy_star`");
    
    $liens = array();
    
    // 1- Cacul du score
    
    // a- Les photos : 1pt / photo ensemble
    $req = new requete($this->db, "SELECT COUNT( * ) as c, p1.id_utilisateur as u1, p2.id_utilisateur as u2 ".
    "FROM `sas_personnes_photos` AS `p1` ".
    "JOIN `sas_personnes_photos` AS `p2` ON ( p1.id_photo = p2.id_photo ".
    "AND p1.id_utilisateur != p2.id_utilisateur ) ".
    "GROUP BY p1.id_utilisateur, p2.id_utilisateur");
    
    while ( $row = $req->get_row() )
    {
      $a = min($row['u1'],$row['u2']);
      $b = max($row['u1'],$row['u2']);
      
      $liens[$a][$b] = $row['c'];
    }  
    
    // b- Parrainage : 15pt / relation parrain-fillot
    $req = new requete($this->db, "SELECT id_utilisateur as u1, id_utilisateur_fillot as u2 ".
    "FROM `parrains` ".
    "GROUP BY id_utilisateur, id_utilisateur_fillot");
    while ( $row = $req->get_row() )
    {
      $a = min($row['u1'],$row['u2']);
      $b = max($row['u1'],$row['u2']);    
      
      if ( isset($liens[$a][$b]) )
        $liens[$a][$b] += 15;
      else
        $liens[$a][$b] = 15;
    }  
    
    // c- associations et clubs : 1pt / 75 jours ensemble / assos
    $req = new requete($this->db,"SELECT a.id_utilisateur as u1,b.id_utilisateur as u2,
  SUM(DATEDIFF(LEAST(COALESCE(a.date_fin,NOW()),COALESCE(b.date_fin,NOW())),GREATEST(a.date_debut,b.date_debut))) AS together
  FROM asso_membre AS a
  JOIN asso_membre AS b ON
  ( 
  a.id_utilisateur < b.id_utilisateur  
  AND a.id_asso = b.id_asso
  AND DATEDIFF(LEAST(COALESCE(a.date_fin,NOW()),COALESCE(b.date_fin,NOW())),GREATEST(a.date_debut,b.date_debut)) > 74
  )
  GROUP BY a.id_utilisateur,b.id_utilisateur
  ORDER BY a.id_utilisateur,b.id_utilisateur");
  
    while ( $row = $req->get_row() )
    {
      $a = min($row['u1'],$row['u2']);
      $b = max($row['u1'],$row['u2']);    
      
      if ( isset($liens[$a][$b]) )
        $liens[$a][$b] += round($row['together']/75);
      else
        $liens[$a][$b] += round($row['together']/75);
    }    
    
    // 2- On vire les liens pas significatifs
    foreach ( $liens as $a => $data )
    {
      foreach ( $data as $b => $score )
        if ( $score < 10 )
          unset($liens[$a][$b]);
    }
    
    // 3- On crée les peronnes requises
    $stars = array();
    foreach ( $liens as $a => $data )
    {
      if ( !isset($stars[$a]) )
        $stars[$a] = $a;
      
      foreach ( $data as $b => $score )
        if ( !isset($stars[$b]) )
          $stars[$b] = $b;
    }
    
    $gx=0;
    $gy=0;
    
    $width = floor(sqrt(count($stars)));
    
    foreach ( $stars as $id )
    {
      new insert($this->dbrw,"galaxy_star",array( "id_star"=>$id, "x_star" => $gx, "y_star" => $gy ));
      $gx++;
      if ( $gx > $width )
      {
        $gx=0;
        $gy++;
      }
    } 
  
    // 4- On crée les liens
    foreach ( $liens as $a => $data )
      foreach ( $data as $b => $score )
        new insert($this->dbrw,"galaxy_link",array( "id_star_a"=>$a, "id_star_b"=>$b, "tense_link" => $score ));
      
    //fixe_star
    new requete($this->dbrw, "UPDATE galaxy_star SET max_tense_star = ( SELECT MAX(tense_link) FROM galaxy_link WHERE id_star_a=id_star OR id_star_b=id_star )");
    new requete($this->dbrw, "UPDATE galaxy_star SET sum_tense_star = ( SELECT SUM(tense_link) FROM galaxy_link WHERE id_star_a=id_star OR id_star_b=id_star )");
    new requete($this->dbrw, "UPDATE galaxy_star SET nblinks_star = ( SELECT COUNT(*) FROM galaxy_link WHERE id_star_a=id_star OR id_star_b=id_star )");
Feu's avatar
test  
Feu committed
135
    new requete($this->dbrw, "UPDATE galaxy_link SET max_tense_stars_link=( SELECT AVG(max_tense_star) FROM galaxy_star WHERE id_star=id_star_a OR id_star=id_star_b )");
Feu's avatar
Feu committed
136
    
Feu's avatar
test  
Feu committed
137
        
Feu's avatar
test  
Feu committed
138
    new requete($this->dbrw, "UPDATE galaxy_link SET ideal_length_link=0.25+((1-(tense_link/max_tense_stars_link))*30)");
Feu's avatar
Feu committed
139 140 141 142 143 144 145
    new requete($this->dbrw, "DELETE FROM galaxy_star WHERE nblinks_star = 0");
  
  
  }
  
  function cycle ( $detectcollision=false )
  {
Feu's avatar
test  
Feu committed
146 147 148 149 150 151 152 153 154 155 156
     
    
    new requete($this->dbrw,"UPDATE galaxy_link, galaxy_star AS a, galaxy_star AS b SET ".
    "vx_link = b.x_star-a.x_star, ".
    "vy_link = b.y_star-a.y_star  ".
    "WHERE a.id_star = galaxy_link.id_star_a AND b.id_star = galaxy_link.id_star_b");
    new requete($this->dbrw,"UPDATE galaxy_link SET length_link = SQRT(POW(vx_link,2)+POW(vy_link,2))");
    new requete($this->dbrw,"UPDATE galaxy_link SET dx_link=vx_link/length_link, dy_link=vy_link/length_link WHERE length_link != 0");
    new requete($this->dbrw,"UPDATE galaxy_link SET dx_link=0, dy_link=0 WHERE length_link = ideal_length_link");
    new requete($this->dbrw,"UPDATE galaxy_link SET dx_link=RAND(), dy_link=RAND() WHERE length_link != ideal_length_link AND dx_link=0 AND dy_link=0");
    
Feu's avatar
test  
Feu committed
157
    $req = new requete($this->db,"SELECT MAX(length_link/ideal_length_link),AVG(length_link/ideal_length_link) FROM galaxy_link");
Feu's avatar
test  
Feu committed
158
    
Feu's avatar
test  
Feu committed
159
    $reducer=1000;
Feu's avatar
test  
Feu committed
160
    
Feu's avatar
test  
Feu committed
161 162
    if ( $req->lines > 0 )
    {
Feu's avatar
test  
Feu committed
163
      list($max,$avg) = $req->get_row();
Feu's avatar
test  
Feu committed
164 165 166 167 168
      if ( $max > 1000 )
      {
        echo "failed due to expension";
        exit();
      }
Feu's avatar
test  
Feu committed
169
      if ( !is_null($max) && $max > 0 )
Feu's avatar
test  
Feu committed
170
        $reducer = max(25,round($max)*3);  
Feu's avatar
test  
Feu committed
171
      echo $max." ".$avg." (".$reducer.") - ";
Feu's avatar
test  
Feu committed
172
    }    
Feu's avatar
test  
Feu committed
173 174
    
    new requete($this->dbrw,"UPDATE galaxy_link, galaxy_star AS a, galaxy_star AS b SET  ".
Feu's avatar
test  
Feu committed
175 176 177
    "delta_link_a=(length_link-ideal_length_link)/ideal_length_link/$reducer, ".
    "delta_link_b=(length_link-ideal_length_link)/ideal_length_link/$reducer*-1 ".
    "WHERE a.id_star = galaxy_link.id_star_a AND b.id_star = galaxy_link.id_star_b");
Feu's avatar
test  
Feu committed
178
    
Feu's avatar
Feu committed
179 180 181 182 183 184 185 186 187 188 189 190
    new requete($this->dbrw,"UPDATE galaxy_star SET ".
    "dx_star = COALESCE(( SELECT SUM( delta_link_a * dx_link ) FROM galaxy_link WHERE id_star_a = id_star ),0) + ".
      "COALESCE((SELECT SUM( delta_link_b * dx_link ) FROM galaxy_link WHERE id_star_b = id_star ),0), ".
    "dy_star = COALESCE(( SELECT SUM( delta_link_a * dy_link ) FROM galaxy_link WHERE id_star_a = id_star ),0) + ".
      "COALESCE((SELECT SUM( delta_link_b * dy_link ) FROM galaxy_link WHERE id_star_b = id_star ),0) WHERE fixe_star != 1");
    if ( $detectcollision )
    {
      new requete($this->dbrw,"UPDATE galaxy_star AS a, galaxy_star AS b SET a.dx_star=0, a.dy_star=0, b.dx_star=0, b.dy_star=0 WHERE a.id_star != b.id_star AND POW(a.x_star+a.dx_star-b.x_star-b.dx_star,2)+POW(a.y_star+a.dy_star-b.y_star-b.dy_star,2) < 0.05");
      new requete($this->dbrw,"UPDATE galaxy_star AS a, galaxy_star AS b SET a.dx_star=0, a.dy_star=0, b.dx_star=0, b.dy_star=0 WHERE a.id_star != b.id_star AND POW(a.x_star+a.dx_star-b.x_star-b.dx_star,2)+POW(a.y_star+a.dy_star-b.y_star-b.dy_star,2) < 0.05");
    }
    new requete($this->dbrw,"UPDATE galaxy_star SET x_star = x_star + dx_star, y_star = y_star + dy_star WHERE dx_star != 0 OR dy_star != 0 AND fixe_star != 1");
    
Feu's avatar
test  
Feu committed
191 192 193 194 195 196
    
  }
  
  function rand()
  {
    new requete($this->dbrw, "UPDATE `galaxy_star` SET x_star = x_star+5-( RAND( ) *10 ), y_star = y_star+5-( RAND( ) *10)");
Feu's avatar
Feu committed
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
  }
  
  function star_color ( $img, $i )
  {
    if ( $i > 800 )
      return imagecolorallocate($img, 255, 255, 255);
      
    if ( $i > 700 )
      return imagecolorallocate($img, (($i-700)*255/100), (($i-700)*255/100), 255);      
      
    if ( $i > 400 )
      return imagecolorallocate($img, 255 -(($i-400)*255/300), 255 -(($i-400)*255/300), ($i-400)*255/300);
      
    if ( $i > 35 )
      return imagecolorallocate($img, 255, ($i-35)*255/365, 0); 
    
    return imagecolorallocate($img, $i*255/36, 0, 0);   
  }
  
  function pre_render ($tx=100)
  {
    $req = new requete($this->db, "SELECT MIN(x_star), MIN(y_star), MAX(x_star), MAX(y_star) FROM  galaxy_star");
    list($top_x,$top_y,$bottom_x,$bottom_y) = $req->get_row();
    
    $top_x = floor($top_x);
    $top_y = floor($top_y);
    $bottom_x = ceil($bottom_x);
    $bottom_y = ceil($bottom_y);
      
    $this->width = ($bottom_x-$top_x)*$tx;
    $this->height = ($bottom_y-$top_y)*$tx;
    
    new requete($this->dbrw,"UPDATE galaxy_star SET rx_star = (x_star-".sprintf("%f",$top_x).") * $tx, ry_star = (y_star-".sprintf("%f",$top_y).") * $tx");    
  }
  
Feu's avatar
wip  
Feu committed
232 233 234 235 236 237 238 239
  function mini_render ( $mini_target="mini_galaxy_temp.png")
  {
    if ( empty($this->width) || empty($this->height) )
      $this->pre_render();
    
    $img = imagecreatetruecolor($this->width/100,$this->height/100);
    $bg = imagecolorallocate($img, 0, 0, 0);
    imagefill($img, 0, 0, $bg);
Feu's avatar
wip  
Feu committed
240
    $req = new requete($this->db, "SELECT FLOOR(rx_star/100),FLOOR(ry_star/100),SUM(sum_tense_star) FROM  galaxy_star GROUP BY FLOOR(rx_star/100),FLOOR(ry_star/100)");
Feu's avatar
wip  
Feu committed
241 242
    
    while ( list($x,$y,$d) = $req->get_row() )
Feu's avatar
wip  
Feu committed
243
      imagesetpixel($img,$x,$y,$this->star_color($img,round($d/2)));
Feu's avatar
wip  
Feu committed
244 245 246 247 248 249
    
    imagepng($img,$mini_target);
    imagedestroy($img); 
  }
  
  function render ($target="galaxy_temp.png") 
Feu's avatar
Feu committed
250 251 252 253 254 255 256 257 258 259 260 261 262 263
  {
    if ( empty($this->width) || empty($this->height) )
      $this->pre_render();
    
    $img = imagecreatetruecolor($this->width,$this->height);
  
    if ( $img === false )
    {
      echo "failed imagecreatetruecolor($width,$height);";
      exit();
    }
    
    $bg = imagecolorallocate($img, 0, 0, 0);
    $textcolor = imagecolorallocate($img, 255, 255, 255);
Feu's avatar
test  
Feu committed
264
    $wirecolor = imagecolorallocate($img, 32, 32, 32);
Feu's avatar
Feu committed
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
    
    imagefill($img, 0, 0, $bg);
    
    imagestring($img, 1, 0, 0, "AE R&D - GALAXY", $textcolor);
    
    for($i=0;$i<820;$i++)
    {
      imageline($img,$i,10,$i,20,$this->star_color($img,$i));
      
      if ( $i %100 == 0)
        imagestring($img, 1, $i, 22, $i, $textcolor);
    }
    
    $req = new requete($this->db, "SELECT ABS(length_link-ideal_length_link) as ex, ".
    "a.rx_star as x1, a.ry_star as y1, b.rx_star as x2, b.ry_star as y2 ".
    "FROM  galaxy_link ".
    "INNER JOIN galaxy_star AS a ON (a.id_star=galaxy_link.id_star_a) ".
    "INNER JOIN galaxy_star AS b ON (b.id_star=galaxy_link.id_star_b)");
    
    while ( $row = $req->get_row() )
    {
      imageline ($img, $row['x1'], $row['y1'], $row['x2'], $row['y2'], $wirecolor );    
    } 
  
    $req = new requete($this->db, "SELECT ".
    "rx_star, ry_star, sum_tense_star  ".
    "FROM  galaxy_star");
    
    while ( $row = $req->get_row() )
    {
      imagefilledellipse ($img, $row['rx_star'], $row['ry_star'], 5, 5, $this->star_color($img,$row['sum_tense_star']) ); 
    }
    
    $req = new requete($this->db, "SELECT ".
    "rx_star, ry_star, COALESCE(alias_utl,CONCAT(prenom_utl,' ',nom_utl)) AS nom ".
    "FROM  galaxy_star ".
    "INNER JOIN utilisateurs ON (utilisateurs.id_utilisateur=galaxy_star.id_star)");  
    
    while ( $row = $req->get_row() )
    {
      imagestring($img, 1, $row['rx_star']+5, $row['ry_star']-3,  utf8_decode($row['nom']), $textcolor);
    }
    
    if ( is_null($target) )
      imagepng($img);
    else
      imagepng($img,$target);
Feu's avatar
wip  
Feu committed
312

Feu's avatar
Feu committed
313 314 315 316
    imagedestroy($img);
    
  }
  
Feu's avatar
Feu committed
317
  function render_area ( $tx, $ty, $w, $h, $target=null, $highlight=null )
Feu's avatar
Feu committed
318 319 320
  {
    $st = microtime(true);
  
Feu's avatar
Feu committed
321
    $x1 = $tx-100; // Pour les textes
Feu's avatar
Feu committed
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
    $y1 = $ty-3;
    $x2 = $tx+$w+3;
    $y2 = $ty+$h+3;
  
    $img = imagecreatetruecolor($w,$h);
     
    $bg = imagecolorallocate($img, 0, 0, 0);
    $textcolor = imagecolorallocate($img, 255, 255, 255);
    $wirecolor = imagecolorallocate($img, 32, 32, 32);
    
    imagefill($img, 0, 0, $bg);  
    
    $req = new requete($this->db, "SELECT ABS(length_link-ideal_length_link) as ex, ".
    "a.rx_star as x1, a.ry_star as y1, b.rx_star as x2, b.ry_star as y2 ".
    "FROM  galaxy_link ".
    "INNER JOIN galaxy_star AS a ON (a.id_star=galaxy_link.id_star_a) ".
    "INNER JOIN galaxy_star AS b ON (b.id_star=galaxy_link.id_star_b)");
    
    while ( $row = $req->get_row() )
    {
      imageline ($img, $row['x1']-$tx, $row['y1']-$ty, $row['x2']-$tx, $row['y2']-$ty, $wirecolor );    
    } 
  
Feu's avatar
Feu committed
345
    if ( is_null($highlight ) ) // Normal render
Feu's avatar
Feu committed
346
    {
Feu's avatar
Feu committed
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363
      $req = new requete($this->db, "SELECT ".
      "rx_star, ry_star, sum_tense_star  ".
      "FROM  galaxy_star ".
      "WHERE rx_star >= $x1 AND rx_star <= $x2 AND ry_star >= $y1 AND ry_star <= $y2");
      
      while ( $row = $req->get_row() )
        imagefilledellipse ($img, $row['rx_star']-$tx, $row['ry_star']-$ty, 5, 5, $this->star_color($img,$row['sum_tense_star']) ); 
      
      $req = new requete($this->db, "SELECT ".
      "rx_star, ry_star, COALESCE(alias_utl,CONCAT(prenom_utl,' ',nom_utl)) AS nom ".
      "FROM  galaxy_star ".
      "INNER JOIN utilisateurs ON (utilisateurs.id_utilisateur=galaxy_star.id_star) ".
      "WHERE rx_star >= $x1 AND rx_star <= $x2 AND ry_star >= $y1 AND ry_star <= $y2" );  
      
      while ( $row = $req->get_row() )
        imagestring($img, 1, $row['rx_star']+5-$tx, $row['ry_star']-3-$ty,  utf8_decode($row['nom']), $textcolor);
      
Feu's avatar
Feu committed
364
    }
Feu's avatar
Feu committed
365
    else
Feu's avatar
Feu committed
366
    {
Feu's avatar
Feu committed
367
      $ids = implode(",",$highlight);
Feu's avatar
Feu committed
368
      
Feu's avatar
test  
Feu committed
369
      $wirecolor = imagecolorallocate($img, 64, 64, 64);
Feu's avatar
Feu committed
370 371 372 373 374 375

      $req = new requete($this->db, "SELECT ABS(length_link-ideal_length_link) as ex, ".
      "a.rx_star as x1, a.ry_star as y1, b.rx_star as x2, b.ry_star as y2 ".
      "FROM  galaxy_link ".
      "INNER JOIN galaxy_star AS a ON (a.id_star=galaxy_link.id_star_a) ".
      "INNER JOIN galaxy_star AS b ON (b.id_star=galaxy_link.id_star_b) ".
Feu's avatar
test  
Feu committed
376
      "WHERE a.id_star = ".$highlight[0]." OR b.id_star = ".$highlight[0]."");
Feu's avatar
Feu committed
377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418
      
      while ( $row = $req->get_row() )
        imageline ($img, $row['x1']-$tx, $row['y1']-$ty, $row['x2']-$tx, $row['y2']-$ty, $wirecolor );    
      
      $textcolor = imagecolorallocate($img, 128, 128, 128);
      
       $req = new requete($this->db, "SELECT ".
      "rx_star, ry_star, sum_tense_star  ".
      "FROM  galaxy_star ".
      "WHERE id_star NOT IN ($ids) AND rx_star >= $x1 AND rx_star <= $x2 AND ry_star >= $y1 AND ry_star <= $y2");
      
      while ( $row = $req->get_row() )
        imagefilledellipse ($img, $row['rx_star']-$tx, $row['ry_star']-$ty, 5, 5, $this->star_color($img,$row['sum_tense_star']) ); 
      
      $req = new requete($this->db, "SELECT ".
      "rx_star, ry_star, COALESCE(alias_utl,CONCAT(prenom_utl,' ',nom_utl)) AS nom ".
      "FROM  galaxy_star ".
      "INNER JOIN utilisateurs ON (utilisateurs.id_utilisateur=galaxy_star.id_star) ".
      "WHERE id_star NOT IN ($ids) AND rx_star >= $x1 AND rx_star <= $x2 AND ry_star >= $y1 AND ry_star <= $y2" );  
      
      while ( $row = $req->get_row() )
        imagestring($img, 1, $row['rx_star']+5-$tx, $row['ry_star']-3-$ty,  utf8_decode($row['nom']), $textcolor);
      
      $textcolor = imagecolorallocate($img, 255, 255, 255);
      
       $req = new requete($this->db, "SELECT ".
      "rx_star, ry_star, sum_tense_star  ".
      "FROM  galaxy_star ".
      "WHERE id_star IN ($ids) AND rx_star >= $x1 AND rx_star <= $x2 AND ry_star >= $y1 AND ry_star <= $y2");
      
      while ( $row = $req->get_row() )
        imagefilledellipse ($img, $row['rx_star']-$tx, $row['ry_star']-$ty, 5, 5, $this->star_color($img,$row['sum_tense_star']) ); 
      
      $req = new requete($this->db, "SELECT ".
      "rx_star, ry_star, COALESCE(alias_utl,CONCAT(prenom_utl,' ',nom_utl)) AS nom ".
      "FROM  galaxy_star ".
      "INNER JOIN utilisateurs ON (utilisateurs.id_utilisateur=galaxy_star.id_star) ".
      "WHERE id_star IN ($ids) AND rx_star >= $x1 AND rx_star <= $x2 AND ry_star >= $y1 AND ry_star <= $y2" );  
      
      while ( $row = $req->get_row() )
        imagestring($img, 1, $row['rx_star']+5-$tx, $row['ry_star']-3-$ty,  utf8_decode($row['nom']), $textcolor);
            
Feu's avatar
Feu committed
419
    }
Feu's avatar
Feu committed
420

Feu's avatar
Feu committed
421 422 423 424
    if ( is_null($target) )
      imagepng($img);
    else
      imagepng($img,$target);
Feu's avatar
wip  
Feu committed
425
      
Feu's avatar
Feu committed
426 427 428 429
    imagedestroy($img);  
        
  }

Feu's avatar
Feu committed
430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447
  function get_density ( $x1, $y1, $x2, $y2, $except=null )
  {
    $x1 = sprintf("%.f",$x1);
    $y1 = sprintf("%.f",$y1);
    $x2 = sprintf("%.f",$x2);
    $y2 = sprintf("%.f",$y2);
    
    echo "get_density($x1,$y1,$x2,$y2) = ";
    
    if (is_null($except) )
      $req = new requete($this->db, "SELECT ".
        "COUNT(*)  ".
        "FROM  galaxy_star ".
        "WHERE x_star >= $x1 AND x_star < $x2 AND y_star >= $y1 AND y_star < $y2");
    else
      $req = new requete($this->db, "SELECT ".
        "COUNT(*)  ".
        "FROM  galaxy_star ".
Feu's avatar
Feu committed
448
        "WHERE id_star NOT IN (".$except.") AND x_star >= $x1 AND x_star < $x2 AND y_star >= $y1 AND y_star < $y2");
Feu's avatar
Feu committed
449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469
        
    list($count) = $req->get_row();
    
    echo $count."<br/>\n";
    
    return $count;
  }

  function find_low_density_point ( $x, $y, $s, $except=null )
  {
    $ld = null;
    $lx = null;
    $ly = null;
    
    $cx = $x;
    $cy = $y;
    
    for($cx=$x;$cx<$x+$s;$cx+=$s/3)
    {
      for($cy=$y;$cy<$y+$s;$cy+=$s/3)
      {
Feu's avatar
Feu committed
470
        $d = $this->get_density($cx,$cy,$cx+($s/3),$cy+($s/3), $except);
Feu's avatar
Feu committed
471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487
        if ( $d == 0 )
          return array($cx+($s/6),$cy+($s/6));  
        
        if ( is_null($ld) || $ld > $d )
        {
          $lx = $cx;  
          $ly = $cy;
          $ld = $d;
        }
      }
    }
    
    if ( $s < 0.001 )
      return array($lx+($s/6),$ly+($s/6)); 
    
    return $this->find_low_density_point($lx,$ly,$s/3, $except);
  }
Feu's avatar
Feu committed
488
  
Feu's avatar
Feu committed
489 490 491 492 493
  function limits()
  {
    $req = new requete($this->db, "SELECT MIN(x_star), MIN(y_star), MAX(x_star), MAX(y_star) FROM  galaxy_star");
    return $req->get_row();
  }
Feu's avatar
Feu committed
494 495 496 497 498 499 500
  
}




?>