Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Multimedia (https://www.delphipraxis.net/16-multimedia/)
-   -   Delphi Linie mit bestimmter Länge und Winkel zeichnen (https://www.delphipraxis.net/180162-linie-mit-bestimmter-laenge-und-winkel-zeichnen.html)

milos 26. Apr 2014 21:23

Linie mit bestimmter Länge und Winkel zeichnen
 
Liste der Anhänge anzeigen (Anzahl: 1)
Hallo,

ich müsste ein Isometrische Ansicht zeichnen können.

Dafür habe ich folgende Funktion geschrieben um eine Linie zeichnen zu können, die einen bestimmte Länge und einen bestimmten Winkel hat.

Hab bei Wikipedia diesen Artikel gefunden, aber wenn ich diese Formel benutze kommt irgendwas ganz komisches dabei raus :/

Entweder mach ich was völlig falsch oder ich mach was völlig falsch xD

Hier mein Code:
Delphi-Quellcode:
unit Main;

interface

uses
  System.Types, Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Direct2D, Vcl.StdCtrls, Vcl.ExtCtrls;

type
  TForm1 = class(TForm)
    Label1: TLabel;
    Timer1: TTimer;
    Label2: TLabel;
    procedure FormPaint(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
  private
    { Private-Deklarationen }
    FD2DCanvas : TDirect2DCanvas;
    FAngle : integer;
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}


// Funktion um die Endkoordinaten auszurechnen
function Polar(X, Y, Length, Angle: integer): TPoint;
begin
  Result := Point(Round(cos(Angle)*Length+X), Round(sin(Angle)*Length+Y));
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  FAngle := 0;
end;

procedure TForm1.FormPaint(Sender: TObject);
begin
  FD2DCanvas := TDirect2DCanvas.Create(Canvas, ClientRect);

  FD2DCanvas.BeginDraw;

  try
    FD2DCanvas.DrawLine(Point(100,100), Polar(100,100,50,FAngle));
  finally
    FD2DCanvas.EndDraw;
    FD2DCanvas.Free;
  end;
end;

procedure TForm1.Timer1Timer(Sender: TObject);
begin
  inc(FAngle);

  if FAngle = 360 then
    FAngle := 0;

  Label1.Caption := inttostr(FAngle);
  Invalidate;
end;

end.
Im Anhang ist das Projekt mit Kompilat noch beigefügt.

MfG

Volker Z. 26. Apr 2014 22:18

AW: Linie mit bestimmter Länge und Winkel zeichnen
 
Hallo,

Zitat:

[...] Entweder mach ich was völlig falsch oder ich mach was völlig falsch xD
schau mal in die OH was die Funktionen Cos / Sin an Parameter erwarten (Stichwort: Bogenmaß). Du übergibst aber ein Gradmaß. Mit DegToRad kannst Du einen Winkel in Grad ins Bogenmaß umrechnen.

Gruß

Sir Rufo 26. Apr 2014 22:24

AW: Linie mit bestimmter Länge und Winkel zeichnen
 
Hmmm, XE5 also, hmmm, das hat doch so schöne Vector-Records an Board mit Gedöns und Geraffel, da braucht man sich nur bequem bedienen.

Delphi-Referenz durchsuchenSystem.Types.TVector

Popov 26. Apr 2014 22:51

AW: Linie mit bestimmter Länge und Winkel zeichnen
 
@milos

Was hast du denn alles in die Exe gepackt. Du hast paar Zeilen drin und das macht 2,2 MB Exe?

Wenn ich das richtig verstehe willst du eine Linie Isometrisch zeichnen. Wenn nicht, kannst du den Rest überlesen, ansonsten habe ich mal Just4Fun eine Funktion zum Zeichnen einer IsoLine entwickelt:
Delphi-Quellcode:
type
  TIsoOrientation = (ioTopLeft, ioTopRight, ioBottomLeft, ioBottomRight);

procedure DrawIsoLinie(Canvas: TCanvas; Orientation: TIsoOrientation; x1, y1, cLength: Integer; var x2, y2: Integer);
var
  a, b,
  alpha: Double;
begin
  a := cLength * Sin(DegToRad(30));
  b := Sqrt(Sqr(cLength) - Sqr(a));

  case Orientation of
    ioTopLeft    : begin
                      x2 := x1 - Round(b);
                      y2 := y1 - Round(a);
                    end;
    ioTopRight   : begin
                      x2 := x1 + Round(b);
                      y2 := y1 - Round(a);
                    end;
    ioBottomLeft : begin
                      x2 := x1 - Round(b);
                      y2 := y1 + Round(a);
                    end;
    ioBottomRight : begin
                      x2 := x1 + Round(b);
                      y2 := y1 + Round(a);
                    end;
  end;

  Canvas.MoveTo(x1, y1);
  Canvas.LineTo(x2, y2);
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  x1, y1, x2, y2, Laenge: Integer;
  Richtung: TIsoOrientation;
  //ioTopLeft für linksoben, ioTopRight für rechtsoben, ioBottomLeft für untenlinks, ioBottomRight für untenrechts
begin
  x1 := 100;
  y1 := 100;
  Laenge := 141;
  Richtung := ioTopRight;
  DrawIsoLinie(Canvas, Richtung, x1, x1, Laenge, x2, y2);

  ShowMessage(Format('Linie beginnt bei x1 = %d; y1 = %d und endet bei x2 = %d; y2 = %d',
    [x1, y1, x2, y2]));
end;
Ich hab sie nicht geprüft ob sie richtig zeichnet. Mit Orientation sagst du ob es nach obenlinks, untenlinks, obenrechts oder untenrechts geht, also die Richtung, cLength die Länge, x1 und x2 die Startposition. x2 und y2 sind Rückgabewerte, also nichts angeben, sondern nur Variablen setzten. Das ist die Position wo die Linie aufgehört.

//Edit:

Ich sehe gerade einen kleinen Fehler. Es gibt noch die Richtung rauf und runter, das habe ich vergessen. Sollte aber kein Problem sein das zu erweitern, sind ja nur gerade Linien.

Popov 26. Apr 2014 23:52

AW: Linie mit bestimmter Länge und Winkel zeichnen
 
Ich hab eine neue Funktion erstellt, dieses Mal mit sechs Richtungen. Die Bezeichnungen sind auch anders:
Delphi-Quellcode:
type
  TIsoOrientation2 = (ioL, ioR, ioV, ioH, ioO, ioU);

procedure DrawIsoLinie2(Canvas: TCanvas; Orientation: TIsoOrientation2; x1, y1, cLength: Integer; var x2, y2: Integer);
var
  a, b: Double;
begin
  a := cLength * Sin(DegToRad(30));
  b := Sqrt(Sqr(cLength) - Sqr(a));

  case Orientation of
    ioH : begin
            x2 := x1 - Round(b);
            y2 := y1 - Round(a);
          end;
    ioV : begin
            x2 := x1 + Round(b);
            y2 := y1 + Round(a);
          end;
    ioR : begin
            x2 := x1 + Round(b);
            y2 := y1 - Round(a);
          end;
    ioL : begin
            x2 := x1 - Round(b);
            y2 := y1 + Round(a);
          end;
    ioO : begin
            x2 := x1;
            y2 := y1 - cLength;
          end;
    ioU : begin
            x2 := x1;
            y2 := y1 + cLength;
          end;
  end;

  Canvas.MoveTo(x1, y1);
  Canvas.LineTo(x2, y2);
end;

procedure TForm1.Button2Click(Sender: TObject);
var
  x, y, x1, y1, x2, y2, x3, y3, Laenge: Integer;
begin
  x := 300;
  y := 300;
  Laenge := 100;

  //Ein Karton
  DrawIsoLinie2(Canvas, ioR, x, y, Laenge, x, y);
  DrawIsoLinie2(Canvas, ioO, x, y, Laenge, x, y);
  DrawIsoLinie2(Canvas, ioL, x, y, Laenge, x, y);
  DrawIsoLinie2(Canvas, ioU, x, y, Laenge, x, y);
  DrawIsoLinie2(Canvas, ioH, x, y, Laenge + 20, x, y);
  DrawIsoLinie2(Canvas, ioR, x, y, Laenge, x, y);
  x1 := x;
  y1 := y;
  DrawIsoLinie2(Canvas, ioO, x, y, Laenge, x, y);
  x2 := x;
  y2 := y;
  DrawIsoLinie2(Canvas, ioL, x, y, Laenge, x, y);
  x3 := x;
  y3 := y;
  DrawIsoLinie2(Canvas, ioU, x, y, Laenge, x, y);
  DrawIsoLinie2(Canvas, ioV, x1, y1, Laenge + 20, x, y);
  DrawIsoLinie2(Canvas, ioV, x2, y2, Laenge + 20, x, y);
  DrawIsoLinie2(Canvas, ioV, x3, y3, Laenge + 20, x, y);
end;

milos 26. Apr 2014 23:59

AW: Linie mit bestimmter Länge und Winkel zeichnen
 
Wow, war gerade dabei deine Funktion anzupassen, und dann kommt eine Lösung. :D
Vielen dank dafür ^^

Wegen der Dateigrösse. Hab sogar absichtlich auf "Release" gestellt damit die exe datei kleiner wird, sind 2,2mb dafür zu gross? Ich meine ja, es ist immerhin ne VCL Anwendung.

MfG


Alle Zeitangaben in WEZ +1. Es ist jetzt 03: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