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.