Einzelnen Beitrag anzeigen

mimi

Registriert seit: 1. Dez 2002
Ort: Oldenburg(Oldenburg)
2.008 Beiträge
 
FreePascal / Lazarus
 
#1

Rechtecke überlagern sich beim zufälligen erstellen ?

  Alt 29. Jan 2006, 14:53
Hallo,
ich habe es mit meinem eignen code nicht und mit einem code aus disem forum auch nicht hinbekommen.
Ich möchte gerne das meine bilder zufällig auf ein TBitMap verteielt werden und dabei ist zu berücksichtigen:
Es darf kein andres Objekt auf der Position sein
Und es darf nicht auserhalb des angeben Rechteckes sein.
ich habe hier die proceduren und funktionen die dafür verantwortlich sind rein kopiert:
Delphi-Quellcode:
// Diese hier habe ich aus dem forum
function RectIntersect(A, B: TRect; Offset: Integer): Boolean;
begin
  result := not((A.Right + Offset <= B.Left) or
                (A.Bottom + Offset <= B.Top) or
                (A.Top - Offset >= B.Bottom) or
                (A.Left - Offset >= B.Right));
end;
// -----------------------

function TForm1.Kollision(rect:TRect):Boolean;
var
  i:Integer;
  r1:TRect;
begin
  result:=False;
  for i := 0 to High(Items) do begin
    r1.Left:=Items[i].x; r1.Top:=Items[i].y;
    r1.Right:=Items[i].w; r1.Bottom:=Items[i].h;
    if RectIntersect(r1,rect, 100) then begin
      result:=True;
     // break;
    end;
  end;
end;
// hier kommt die anwendung
function TForm1.Zufall(px,py,w,h,index:Integer):TPoint;
var
  x,y,z:Integer;
  i,r:Boolean;
begin

  x:=px; y:=py; z:=0;
// px:=px*w; py:=py*h;

  i:=CheckCollision(px,py,w,h,index);//CheckCollision(x*w,y*h,w,h,index);
  Label3.Caption:=BoolToStr(i,True);

  if (i = False) or (x < 0) or (y <0) or (x > 640) or (y >240) then begin
    repeat
      x:=random(640-w);
      y:=random(280-h);
      inc(z);
      r:=CheckCollision(x,y,w,h,index);
    until (r = False) or (z = 5);
  end;
  ListBox1.Items.Add(IntToStr(Index)+ '\'+ IntToStr(x) + '\' + IntToStr(y));
  result.X:=x;
  result.Y:=y;
end;
// und hier noch mal meine eigne:
function TForm1.GetItemIndex(px,py,pw,ph,Index:Integer):Integer;
var
  i:Integer;
  t:Integer;
  g:TRect;
begin
  ListBox2.items.Clear;
  t:=-1;
  Label1.Caption:='';
  for i:=0 to HIGH(Items) do begin
    with Items[i] do begin
         ListBox2.Items.Add(IntTostr(x) + '\' + IntToStr(y));

      if (typ <> 4) and (typ <> 1) and (PlayerIndex <> i) then begin
// if PtInRect(Rect(x,y,w,h),Point(px,py)) = True then begin
         if IntersectRect(g,rect(x,y,w,h),rect(px,py,pw,ph)) = True then begin
                 Label1.Caption:=InttoStr(x)+'\'+IntToStr(y) + '\'+ IntToStr(Random(4));
         // if (px >=x) and ( py>=y) and (px <=x+pw) and (py <=y+ph)then begin
// if (x >=px) and (y >=py) and (x <=px+pw) and (y <=py+ph) and (PlayerIndex <> i) then begin

          t:=i;
          break;
        end;
      end;
    end;
  end;
  result:=t;
end;
das problem dabei ist bei diesem code überlagen sich teilweise die Grafiken übereinander und ich weiß einfach nicht warum. Habe hier im forum gesucht letzte woche saß ich stunden vor diesem Problem, habe aber leider keine lösung gefunden !
Michael Springwald
MFG
Michael Springwald,
Bitte nur Deutsche Links angeben Danke (benutzte überwiegend Lazarus)
  Mit Zitat antworten Zitat