下面是一个 div混合的 代码,现在的代码显示200*20, 100*10, 100*20,100*10(空白)四种形式的DIV,随机显示在一个1000*200的DIV里。如何修改三元运算符,添加一个300*10的形式?使得100*10的显示20个,100*20的显示15个,200*20的显示10个,300*10的显示10个,300*20的显示5个。(上面的背景色随意设置,主要为了区分不同的DIV)剩余的均显示100*10的背景色白色(30个),谢谢。<style>
*,p{padding:0;margin:0;}
</style>
<?php
function divHtml($dtype, $x, $y) 
{ $ww =($dtype ==3) ?'200px':'100px'; $hh =($dtype==2)?'40px':'20px'; $bgcols =array('#999', '#ccc', '#eee');
  $xx =($x*100) .'px'; $yy =($y*20) .'px'; $bgc =$bgcols[$dtype-1];
  return "<p style='position:absolute; width:$ww;height:$hh; left:$xx;top:$yy; background:$bgc;'> $dtype </p>";
}
$divs =array(); 
$cells =array(); 
for ($i=0; $i<60; $i++) $cells[$i] =0;  function reserve($dtype, $x, $y) 
{ global $cells, $divs; if ($y*10+$x >59 ||($cells[$y*10+$x])) return false;
  switch ($dtype)
  { case 2: if ($y ==5 || $cells[$y*10+$x] ||$cells[($y+1)*10+$x]) return false; $cells[($y+1)*10+$x] =1; break;
  case 3: if ($x ==9 || $cells[$y*10+$x] ||$cells[$y*10+$x+1]) return false; $cells[$y*10+$x+1] =1; break;
  }
  $cells[$y*10+$x] =1; $divs[] =divHtml($dtype, $x, $y); return true;
} for($i = 0; $i < 10; $i ++) while (!reserve(2, rand(0,9), rand(0,5))) ;
for($i = 0; $i < 10; $i ++) while (!reserve(3, rand(0,9), rand(0,5))) ;
for($i = 0; $i < 60; $i ++) if (!$cells[$i]) reserve(1, $i%10, floor($i/10)) ; echo '<div style="position:absolute; width:1000px;height:200px;">';
  foreach ($divs as $single) echo $single;
echo '</div>';
?>

解决方案 »

  1.   

    当然有兴趣,连发三个百分贴了。自己也研究了很长时间,找了很多相关的实例,反复修改测试,但始终无法得到满意的效果。随机数组我会,但是现在每个 P 或 DIV 要结合CSS的position:absolute进行各自位置的不重复排列,超过了我的知识范围。如果有代码可以达到我要的要求,帮我解决这个难题,我当然欢迎,谢谢。我的需求是:200*20, 100*10, 100*20,300*10, 300*20 五种形式的 P 或 DIV 以各种不同的数量,随机不重叠的显示在一个1000*200的DIV里,剩余面积显示为空白(这里为了简单处理,可以将剩余部分,看做若干个100*10的空白 P 或 DIV)。其实简单点,可以看做100*10为一个基本单位,五种形式的 P 或 DIV 的长或宽都是这个基本单位的倍数。最终显示效果有点像拼图或俄罗斯方块。每次刷新,所有的  P 或 DIV 位置会重新计算各自位置,并排列。
      

  2.   

    你当我糊弄你呀?
    这个代码提供了三种充填算法,你自己去看看
    //Filled rectangle
    //充填矩形
    class fillrect {
      public $width;
      public $height;
      public $minstep = 10;
      public $d = array();
      public $frame = '<div style="position:relative;margin:10px;padding:0px;width:%dpx;height:%dpx;border:1px blue dotted;font-size:10px">';
      public $fmt = '<p style="position:absolute;padding:0px;margin:0px;top:%dpx;left:%dpx;width:%dpx;height:%dpx;border:1 red solid;text-align:center;line-height:%dpx">%s</p>';
    public $buf = array();
      function __construct($w, $h, $f='') {
        $this->width = $w;
        $this->height = $h;
        if($f) $this->fmt = $f;
      }
      function add($w, $h, $v) {
        do {
          $l = rand(0, floor(($this->width - $w)/$this->minstep)) * $this->minstep;
          $t = rand(0, floor(($this->height - $h)/$this->minstep)) * $this->minstep;
          $p = array('t' => $t, 'l' => $l, 'w' => $w, 'h' => $h, 'v' => $v);
    //do {
    //$this->Swing($p);
          $r = array();
          for($x=0; $x<$w; $x+=$this->minstep)
            for($y=0;$y<$h; $y+=$this->minstep)
              $r[] = sprintf('%04d%04d', $l+$x, $t+$y);
          $c = array_intersect($r, $this->buf);
        }while($c);
        $this->buf = array_merge($this->buf, $r);
        return $this->d[] = $p;
      }//Place 摆放
      function Place($w, $h, $v) {
        $p = func_get_args();
        if(count($p) == 5) {
          list($l, $t, $w, $h, $v) = $p;
        }else {
          list($w, $h, $v) = array_slice($p, -3);
          $l = rand(0, floor(($this->width - $w)/$this->minstep)) * $this->minstep;
          $t = rand(0, floor(($this->height - $h)/$this->minstep)) * $this->minstep;
        }
        $t = array('t' => $t, 'l' => $l, 'w' => $w, 'h' => $h, 'v' => $v);
        if( $this->Swing($t) != false ) return $this->d[] = $t;
        if( $this->probe($t) ) return $this->d[] = $t;
        return $this->Place($w, $h, $v);
      }
    //Swing 摆动
      function Swing(&$t) {
        for($x=0; $x<$t['w']; $x+=$this->minstep) {
          $fx = rand() < 0.5 ? 1 : -1;
          $kx = 1;
          do {
            $fx = -$fx;
            for($y=0; $y<$t['h']; $y+=$this->minstep) {
              $fy = rand()<0.5 ? 1 : -1;
              $ky = 1;
              do {
                $fy = -$fy;
                $c = $t;
                $c['l'] += $fy * $x;
                $c['h'] += $fy * $y;
                if( $this->probe($c) ) return $t = $c;
              }while($ky--);
            }
          }while($kx--);
        }
        return false;
      }
    //Magnetic 磁性
      function Magnetic($w, $h, $v) {
        if(count($this->d)==0)
          return $this->d[] = array('t' => 0, 'l' => 0, 'w' => $w, 'h' => $h, 'v' => $v);
        $p = count($this->d) - 1;
        $t = $this->d[$p];
        if($t['l'] + $t['w'] + $w <= $this->width)
          $t = array('t' => $t['t'], 'l' => $t['l'] + $t['w'], 'w' => $w, 'h' => $h, 'v' => $v);
        else {
          foreach($this->d as $q) if($q['l'] == 0) $p = $q;
          $t = array('t' => min($t['t']+0, $p['t']) + min($t['h'], $p['h'], 20), 'l' => 0, 'w' => $w, 'h' => $h,  'v' => "$v");
        }
        $len = count($this->d);
        $p =& $this->d;
        $et = 0;
        for($i=0; $i<$len; $i++) {
          $v = $p[$i];
          if($v['l'] == 0) $o = $i;
          if( $this->isintersect($t, $v) ) {
            $t['l'] = $v['l'] + $v['w'];
            if($t['l']+$t['w']>$this->width) {
              $t['l'] = 0;
              $t['t'] += $this->minstep;
              $t['v'] = "$t[v]";
            }
            $i = -1;
          }
        }
        do { $t['t'] -= $this->minstep; }while($this->probe($t));
        $t['t'] += $this->minstep;
        do { $t['l'] -= $this->minstep; }while($this->probe($t));
        $t['l'] += $this->minstep;
        return $this->d[] = $t;
      }
    //探测
      function probe($t) {
        if($t['t']<0 || $t['l']<0) return 0;
        foreach($this->d as $v) if($this->isintersect($t, $v)) return 0;
        return 1;
      }
      function isintersect($t, $v) {
        $p = $t['t']<0 || $t['t']>$this->height || $t['l']<0 || $t['l']+$t['w']>$this->width;
        if( $p ) return $p;
        $minx = max($t['l'], $v['l']);
        $miny = max($t['t'], $v['t']);
        $maxx = min($t['l']+$t['w'], $v['l']+$v['w']);
        $maxy = min($t['t']+$t['h'], $v['t']+$v['h']);
        if( $minx>$maxx || $miny>$maxy) return 0;
        return ($maxx - $minx) * ($maxy - $miny);
      }
      function display($fmt='') {
        if(! $fmt) $fmt = $this->fmt;
        printf($this->frame, $this->width, $this->height+2);
        foreach($this->d as $r) printf($fmt, $r['t'], $r['l'], $r['w'], $r['h'], $r['h']-2, $r['v']);
        echo preg_replace('/<(\w+).+/', '</$1>', $this->frame);
      }      
    }$dict = array(
      array('w' =>40, 'h' =>20),
      array('w' =>80, 'h' =>20),
      array('w' =>120, 'h' =>20),
      array('w' =>40, 'h' =>40),
    );
    $dict = array(
      array('w' =>100, 'h' =>20),
      array('w' =>50, 'h' =>10),
      array('w' =>50, 'h' =>20),
      array('w' =>150, 'h' =>10),
      array('w' =>150, 'h' =>20),
    );
    $p = new fillrect(500, 200);
    $e = count($dict) - 1;
    for($t=0; $i<30; $i++) {
      $t = $dict[rand(0, $e)];
    //    $p->Magnetic($t['w'], $t['h'], $i+1);
    //    $p->Place($t['w'], $t['h'], $i+1);
      $p->add($t['w'], $t['h'], $i+1);
    }
    $p->display();
      

  3.   

    你没看到测试例中用的四 rand 吗?
    不随机你就指定就是了
      

  4.   

    哪个rand,最底下的那个? $t = $dict[rand(0, $e)]; 我还是想指定数量,如何设定呢?
    另外有2个$dict = array,是不是重复了?上面一个不要的。
      

  5.   

    //100*10的显示20个,100*20的显示15个,200*20的显示10个,300*10的显示10个,300*20的显示5个?
    $p = new fillrect(1000, 200);
    for($i=0; $i<10; $i++) $p->Place(300, 10, $i);
    for($i=0; $i<5; $i++) $p->Place(300, 20, $i);
    for($i=0; $i<20; $i++) $p->Place(100, 10, $i);
    for($i=0; $i<15; $i++) $p->Place(100, 20, $i);
    for($i=0; $i<10; $i++) $p->Place(200, 20, $i);
    $p->display();