![]() |
SQR Funktion / Ping Pong
Da mir geraten wurde mit dieser Funktion :
Delphi-Quellcode:
meinen Quelltext :
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;
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:
p.x-center.x und p.y-center.y steht.
Result := SQR(p.x-center.x) + SQR(p.y-center.y) <= SQR(radius);
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 |
Re: SQR Funktion / Ping Pong
Na, das war dann wohl ich, der dir Funktion vorgesetzt hat.
Delphi-Quellcode:
"p" ist der Punkt und "center" ist der Mittelpunkt des Kreises.
function PointInCircle(p, center:TPoint; radius:integer):Boolean;
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:
Das verstehst du recht schnell, wenn du ein oder zwei Einsteiger Tutorials durchgelesen hast.
TPoint = record
x: Integer; y: Integer; end; ![]() 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. |
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:
oder nicht ?
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; dann hab ich nurnoch eine Frage , wo ich die Funktion dann reinsetzen muss ?! Mfg |
Alle Zeitangaben in WEZ +1. Es ist jetzt 00:04 Uhr. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz