Delphi-PRAXiS
Seite 2 von 2     12   

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Multimedia (https://www.delphipraxis.net/16-multimedia/)
-   -   Delphi Rechteck drehen? (https://www.delphipraxis.net/66872-rechteck-drehen.html)

Hawkeye219 6. Apr 2006 18:33

Re: Rechteck drehen?
 
Hallo Flare,

hier ist eine Routine, die Dir bei Deinem Problem helfen könnte:

Delphi-Quellcode:
type
  // Koordinaten einer Box
  TBox = array [0..3] of TPoint;

// Dreht die Box aBox im Uhrzeigersinn um aAngle Grad um den Punkt aPoint
function RotateRect (const aBox: TBox; const aPoint: TPoint; aAngle: Real): TBox;

  var i   : Integer;
      s, c : Real;
      P   : TPoint;

  // Hilfsroutine zum Drehen eines Punktes
  function RotatePoint (const aPoint: TPoint): TPoint;
  begin
    Result.X := Round(aPoint.X * c - aPoint.Y * s);
    Result.Y := Round(aPoint.X * s + aPoint.Y * c);
  end;

begin

  // Sinus und Cosinus des Drehwinkels berechnen
  s := Sin(aAngle * Pi / 180);
  c := Cos(aAngle * Pi / 180);

  // Alle 4 Punkte der Box drehen
  for i := 0 to 3 do
  begin
    // Der Drehpunkt muß für die Drehung im Ursprung liegen
    P.X := aBox[i].X - aPoint.X;
    P.Y := aBox[i].Y - aPoint.Y;
    // Einen Punkt drehen
    P := RotatePoint(P);
    // Verschiebung rückgängig machen
    Result[i].X := P.X + aPoint.X;
    Result[i].Y := P.Y + aPoint.Y;
  end;

end;
Wie marabu je bereits geschrieben hat, muß der Drehpunkt vor der Drehung in den Ursprung geschoben werden. Bei der Funktion RotateRect wird dies innerhalb der FOR-Schleife durchgeführt. Der Rest sollte für Dich halbwegs nachvollziehbar sein und - falls ich nicht allzu viele Fehler eingebaut habe - auch funktionieren.

Hier ein Beispielaufruf:
Delphi-Quellcode:
procedure Beispiel;

var Box   : TBox;
    Origin : TPoint;

begin

  // Box definieren
  Box[0] := Point(40, 30);
  Box[1] := Point(340, 30);
  Box[2] := Point(340, 230);
  Box[3] := Point(40, 230);

  // Drehpunkt definieren (hier Mittelpunkt der Box)
  Origin.X := (Box[0].X + Box[2].X) div 2;
  Origin.Y := (Box[0].Y + Box[2].Y) div 2;

  // Drehung der Box um 45 Grad
  Box := RotateRect(Box, Origin, 45.0);

end;
Gruß Hawkeye

Flare 6. Apr 2006 18:50

Re: Rechteck drehen?
 
Hey ho!

Danke, danke, danke!!! :thumb:


Genau das suchte ich, klappt perfekt!
:dp:


Flare


Alle Zeitangaben in WEZ +1. Es ist jetzt 18:49 Uhr.
Seite 2 von 2     12   

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