Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   GUI-Design mit VCL / FireMonkey / Common Controls (https://www.delphipraxis.net/18-gui-design-mit-vcl-firemonkey-common-controls/)
-   -   Delphi SQR Funktion / Ping Pong (https://www.delphipraxis.net/121969-sqr-funktion-ping-pong.html)

Andi28 7. Okt 2008 18:42


SQR Funktion / Ping Pong
 
Da mir geraten wurde mit dieser Funktion :

Delphi-Quellcode:
function PointInCircle(p, center:TPoint; radius:integer):Boolean;
begin
  // SQR() = Square-Funktion = quadrieren
  // Hier wird die Formel a^2 + b^2 = c^2 von Pythagoras ausgenützt
  Result := SQR(p.x-center.x) + SQR(p.y-center.y) <= SQR(radius);
end;
meinen Quelltext :
Delphi-Quellcode:
 // testen, ob der Kreis den linken Endpunkt der Linie berührt

 rq := (shape1.width div 2); rq := rq*rq; // rq = radius zum Quadrat
 ax := (shape1.Left + shape1.Width div 2) - shape2.left; // ax = abstand in x-richtung
 ay := (shape1.top + shape1.Height div 2) - shape2.top; // ay = abstand in y-richtung
 aq := ax*ax + ay*ay; // aq = Abstand zum Quadrat

 if aq > rq then
  begin
   dx := -dx;
   dy := -dy;
  end;

 ay := (shape1.top + shape1.Height div 2) - shape2.top - shape2.Height; // ay = abstand in y-richtung
 aq := ax*ax + ay*ay; // aq = Abstand zum Quadrat
 if aq > rq then
  begin
   dx := -dx;
   dy := -dy;
  end;
  // testen, ob der Kreis den rechten Endpunkt der Linie berührt

 ax := shape2.Left+shape2.width-shape1.left-shape1.width div 2; // ax = abstand in x-richtung
 aq := ax*ax + ay*ay; // aq = Abstand zum Quadrat

 if aq > rq then
  begin
   dx := -dx;
   dy := -dy;
  end;

 ax := shape2.Left+shape2.width-shape1.left-shape1.width div 2; // ax = abstand in x-richtung
 ay := (shape1.top + shape1.Height div 2) - shape2.top; // ay = abstand in y-richtung
 aq := ax*ax + ay*ay; // aq = Abstand zum Quadrat
 if aq > rq then
  begin
   dx := -dx;
   dy := -dy;
  end;


zu vereinfachen. Doch als ich mich ebend näher deamit auseinander gesetzt habe ist mir aufgeallen das ich diese Funktion an sich verstehe aber nicht ganz weiß wofür

Delphi-Quellcode:
 Result := SQR(p.x-center.x) + SQR(p.y-center.y) <= SQR(radius);
p.x-center.x und p.y-center.y steht.



Kann mir vielleicht wer dabei helfen ?!Mein eigentliches Problem was mich zu dieser Funktion brachte , war das mein Pong Spiel nicht waagerecht funktionierte , da mein Shape1 (Kugel) immer an der linken und rechten Seite durch dsa Sahpe2 durchläuft anstatt abzuprallen..

Über eine Anwtort oder ein Beispiel an einer Änderung meines Quelltextes freuen , da ich schon knapp 2 Tage an diesem Problem rästel und was mich davon abhält weiterzuarbeiten


Mfg Andi :-D

sx2008 7. Okt 2008 19:49

Re: SQR Funktion / Ping Pong
 
Na, das war dann wohl ich, der dir Funktion vorgesetzt hat.
Delphi-Quellcode:
function PointInCircle(p, center:TPoint; radius:integer):Boolean;
"p" ist der Punkt und "center" ist der Mittelpunkt des Kreises.
p.x - center.x ist der Abstand in X-Richtung zwischen den Punkt und dem Kreismittelpunkt.
Dazu muss man wissen dass TPoint ein komplexer Datentyp ist:
Delphi-Quellcode:
TPoint = record
  x: Integer;
  y: Integer;
end;
Das verstehst du recht schnell, wenn du ein oder zwei Einsteiger Tutorials durchgelesen hast.
http://www.epinasoft.com/delphikurs/

Die Y-Richtung dürfte damit auch klar sein.
Interessant ist noch, dass es eigentlich egal ist, ob man p und center vertauscht oder nicht.
Durch das Quadrieren mit SQR wird p.x - center.x auf jeden Fall positiv.
Wenn man weiter überlegt ist das eigentlich logisch:
man hat zwei Punkte auf eine Fläche. Ob man jetzt Punkt A oder Punkt B als Kreismittelpunkt annimmt ist egal.

Andi28 7. Okt 2008 20:01

Re: SQR Funktion / Ping Pong
 
Achso :) so langsam versteh ich das ^^
dann wär das in meinem Fall wenn ich mich nicht ganz täusche ja :

Delphi-Quellcode:
function PointInCircle(p, center:TPoint; radius:integer):Boolean;
begin
  // SQR() = Square-Funktion = quadrieren
  // Hier wird die Formel a^2 + b^2 = c^2 von Pythagoras ausgenützt
  Result := SQR(ax) + SQR(ay) <= SQR(radius);
end;
oder nicht ?
dann hab ich nurnoch eine Frage , wo ich die Funktion dann reinsetzen muss ?!
Mfg


Alle Zeitangaben in WEZ +1. Es ist jetzt 14:51 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz