AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Thema durchsuchen
Ansicht
Themen-Optionen

Objektrotation in Abhängigkeit

Ein Thema von -=ZGD=- · begonnen am 16. Mai 2014 · letzter Beitrag vom 16. Mai 2014
Antwort Antwort
Dejan Vu
(Gast)

n/a Beiträge
 
#1

AW: Objektrotation in Abhängigkeit

  Alt 16. Mai 2014, 09:59
Aber dann ist z.B. das rote Ding immer noch oben, zwar um 90 Grad gedreht, aber immer noch oben. Es muss dann aber rechts sein (rein von der Orientierung). Ich würde also den Referenzpunkt des Objekts gleich mit drehen. D.h. nach der Rotation addierst Du nicht XR und YR, sondern XR' und YR' wobei PR' dann PR'=PR*R ist

Oder? Bin aber Mathelaie...
  Mit Zitat antworten Zitat
-=ZGD=-

Registriert seit: 25. Apr 2006
Ort: Bad Aibling
105 Beiträge
 
Delphi 10.1 Berlin Professional
 
#2

AW: Objektrotation in Abhängigkeit

  Alt 16. Mai 2014, 10:35
Richtig, die rote Markierung sollte dann rechts sein.
Ich habe den Rotationspunkt jetzt ins Zentrum des T-Stücks gelegt und lasse darum rotieren.


Das habe ich jetzt, aber es ist noch der Wurm drin.

Delphi-Quellcode:
unit unit_main;

interface

uses
   Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
   System.Classes, Vcl.Graphics,
   Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.Imaging.jpeg, Vcl.ExtCtrls,
   Vcl.Imaging.pngimage, Vcl.StdCtrls, Generics.Collections, System.Math;

type
   RMarker = record
      Top: Integer;
      Left: Integer;
      Width: Integer;
      Height: Integer;
   end;

type
   TMarker = TDictionary<TImage, RMarker>;

type
   Tform_main = class(TForm)
      Image1: TImage;
      img1: TImage;
      Button1: TButton;
      Image2: TImage;
      shape_mittelpunkt: TShape;
      procedure Button1Click(Sender: TObject);
   private
      { Private-Deklarationen }
      fRotationCount: Integer;

      fMarker: TMarker;

      fMarker1Left, fMarker1Top, fMarker1Width, fMarker1Height: Integer;
   public
      { Public-Deklarationen }
   end;

var
   form_main: Tform_main;

implementation

{$R *.dfm}

procedure DreheBmp(Grad: Word; SourceBmp, DestBmp: TBitmap);
var
   Points: array [0 .. 2] of TPoint;
   Winkel: Double;
   X1, X2, Y1, Y2: Integer;
begin
   if Grad <= 360 then
   begin
      Winkel := (Grad - Grad div 90 * 90) / 180 * pi;
      X1 := Round(SourceBmp.Width * sin(Winkel));
      X2 := Round(SourceBmp.Width * cos(Winkel));
      Y2 := Round(SourceBmp.Height * sin(Winkel));
      Y1 := Round(SourceBmp.Height * cos(Winkel));
      Case Grad of
         0 .. 89, 360:
            begin
               Points[1] := Point(X2, 0); // rechts oben
               Points[0] := Point(0, X1); // links oben
               Points[2] := Point(Y2, Y1 + X1); // links unten
               DestBmp.Width := X2 + Y2;
               DestBmp.Height := Y1 + X1;
            end;
         90 .. 179:
            begin
               Points[1] := Point(0, Y2); // rechts oben
               Points[0] := Point(X1, Y2 + X2); // links oben
               Points[2] := Point(X1 + Y1, X2); // links unten
               DestBmp.Width := Y1 + X1;
               DestBmp.Height := X2 + Y2;
            end;
         180 .. 269:
            begin
               Points[1] := Point(Y2, X1 + Y1); // rechts oben
               Points[0] := Point(Y2 + X2, Y1); // links oben
               Points[2] := Point(X2, 0); // links unten
               DestBmp.Width := X2 + Y2;
               DestBmp.Height := Y1 + X1;
            end;
         270 .. 359:
            begin
               Points[1] := Point(X1 + Y1, X2); // rechts oben
               Points[0] := Point(Y1, 0); // links oben
               Points[2] := Point(0, Y2); // links unten
               DestBmp.Width := Y1 + X1;
               DestBmp.Height := X2 + Y2;
            end;
      end;
      PlgBlt(DestBmp.Canvas.Handle, Points, SourceBmp.Canvas.Handle, 0, 0,
         SourceBmp.Width, SourceBmp.Height, 0, 0, 0);
   end;
end;

procedure Tform_main.Button1Click(Sender: TObject);
var
   bmp1, bmp2: TBitmap;

   xp, yp: Integer;
   X1, Y1: Integer;
   X2, Y2: Integer;
   xh, yh: Integer;
begin
   bmp1 := TBitmap.Create;
   bmp1.Assign(Image1.Picture.Graphic);
   bmp2 := TBitmap.Create;

   inc(fRotationCount);

   // Mittelpunkt des T-Stücks
   xp := (Image1.Left + Image1.Width) - (Image1.Width div 2);
   yp := (Image1.Top + Image1.Height) - (Image1.Height div 2);

   // Drehpunkt TOP/LEFT T-Stück (DEBUG)
   // xp := Image1.Left;
   // yp := Image1.Top;

   // TOP/LEFT des Markers
   X1 := img1.Left;
   Y1 := img1.Top;

   // Differenz zum Mittelpunkt
   xh := X1 - xp;
   yh := Y1 - yp;

   X2 := +Round((xh * cos(DegToRad(90))) + (yh * sin(DegToRad(90))));
   Y2 := -Round((xh * sin(DegToRad(90))) + (yh * cos(DegToRad(90))));

   X1 := X2 + xp;
   Y1 := Y2 + yp;

   X2 := Y1 - xp;
   Y2 := -X1 + xp;
   X1 := X2 + xp;
   Y1 := Y2 + yp;

   // Zuweisung neuer Koordinaten
   img1.Left := X1;
   img1.Top := Y2;

   // Drehe T-Stück
   // DreheMarker(img1);
   DreheBmp(90, bmp1, bmp2);
   Image1.Picture.Assign(bmp2);

   // Mittelpunkt nach Drehung holen (DEBUG)
   xp := (Image1.Left + Image1.Width) - (Image1.Width div 2);
   yp := (Image1.Top + Image1.Height) - (Image1.Height div 2);

   // Objektmittelpunkt anzeigen (DEBUG)
   shape_mittelpunkt.Left := xp - shape_mittelpunkt.Width div 2;
   shape_mittelpunkt.Top := yp - shape_mittelpunkt.Height div 2;

end;

end.
Angehängte Dateien
Dateityp: zip MarkerRotation.zip (305,1 KB, 5x aufgerufen)
Stefan Michalk

Geändert von -=ZGD=- (16. Mai 2014 um 11:00 Uhr)
  Mit Zitat antworten Zitat
Benutzerbild von sx2008
sx2008

Registriert seit: 15. Feb 2008
Ort: Baden-Württemberg
2.332 Beiträge
 
Delphi 2007 Professional
 
#3

AW: Objektrotation in Abhängigkeit

  Alt 16. Mai 2014, 11:38
Ich würde Dir empfehlen folgende Funktionen zu benützen:
http://www.delphipraxis.net/33175-dr...m-2d-raum.html
Vorteile:
* Berechnungen werden zentraler Stelle ausgeführt
* durch Zusammenfassung von x- und y-Koordinaten zu einem TPoint wird eine weitere Fehlerquelle ausgeschaltet
fork me on Github
  Mit Zitat antworten Zitat
Blup

Registriert seit: 7. Aug 2008
Ort: Brandenburg
1.493 Beiträge
 
Delphi 12 Athens
 
#4

AW: Objektrotation in Abhängigkeit

  Alt 16. Mai 2014, 13:30
Das Problem lässt sich in zwei Teile untergliedern.
1. Das Steuerelement (TImage) muss an seine neue Position verschoben werden.
Für die Drehung um 90 und 270 Grad müssen Höhe und Breite getauscht werden.
2. Die enthaltene Grafik (TBitmap) muss gedreht werden.
Delphi-Quellcode:
function RotatePoint(APoint, ARotPoint: TPoint; AValue: Word): TPoint;
var
  w, cosw, sinw: Double;
begin
  APoint.x := APoint.x - ARotPoint.x;
  APoint.y := APoint.y - ARotPoint.y;

  w := DegToRad(AValue);
  sinw := sin(w);
  cosw := cos(w);
  Result.x := Round((APoint.x * +cosw) + (APoint.y * sinw));
  Result.y := Round((APoint.x * -sinw) + (APoint.y * cosw));

  Result.x := Result.x + ARotPoint.x;
  Result.y := Result.y + ARotPoint.y;
end;

procedure RotateWinCtrl(AWinCtrl: TWinControl; ARotPoint: TPoint; AValue: Word);
var
  lR: TRect;
  lP: TPoint;
  dx, dy: Integer;
begin
  AValue := AValue mod 360;
  {den Eckpunkt bestimmen, der nach der Rotation links-oben ist}
  lR := AWinCtrl.BoundsRect;
  case AValue div 90 of
    0: {keine Drehung}
      begin
        Exit;
      end;
    1: {90 Grad}
      begin
        lP.x := lR.Left;
        lP.y := lR.Bottom;
        dx := lR.Bottom - lR.Top;
        dy := lR.Right - lR.Left;
      end
    2: {180 Grad}
      begin
        lP.x := lR.Right;
        lP.y := lR.Bottom;
        dx := lR.Right - lR.Left;
        dy := lR.Bottom - lR.Top;
      end
    3: {270 Grad}
      begin
        lP.x := lR.Right;
        lP.y := lR.Top;
        dx := lR.Bottom - lR.Top;
        dy := lR.Right - lR.Left;
      end;
  end;

  lP := RotatePoint(lP, ARotPoint, AValue);
  
  AWinCtrl.Left := lP.x;
  AWinCtrl.Top := lP.y;
  AWinCtrl.Width := dx;
  AWinCtrl.Height := dy;

  if AWinCtrl is TImage then
  begin
    bmp1 := TBitmap.Create;
    bmp2 := TBitmap.Create;
    try
      bmp1.Assign(TImage(AWinCtrl).Picture.Graphic);
      DreheBmp(AValue, bmp1, bmp2);
      TImage(AWinCtrl).Picture.Assign(bmp2);
    finally
      bmp1.Free;
      bmp2.Free;
    end;
  end;
end;

procedure Tform_main.Button1Click(Sender: TObject);
var
  lRotRoint: TPoint;
begin
  // Mittelpunkt des T-Stücks
  lRotRoint.x := Image1.Left + (Image1.Width div 2);
  lRotRoint.y := Image1.Top + (Image1.Height div 2);

  RotateWinCtrl(Image1, lRotRoint, 90);
end;

Geändert von Blup (16. Mai 2014 um 13:35 Uhr)
  Mit Zitat antworten Zitat
Antwort Antwort


Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 08:26 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