Delphi Code



  • Hallo,

    http://www.swissdelphicenter.ch/de/showcode.php?id=2256

    kann ich diesen Code irgendwie im cBuilder ausführen lassen ?

    Andreas



  • Das umschreiben ist doch in diesem Falle wirklich einfach und in knapp 1min fertig.
    Ich finde die Funktionen in Funktionen in Delphi cool.. hat was einer Klasse und zeigt sofort deren Zugehörigkeit.



  • Ja ok. ArcTan kriege ich aber nicht hin. math.h hab ich included. Von daher kann ich es nicht testen, den Rest hab ich schon versucht umzusetzen.



  • Heisst in C nur anders: atan()



  • Hallo,

    ich hab noch ne bessere Variante gefunden. Mit dem Aufruf wie im Code unten geht es, jedoch bei z. B. ZeichnePfeil(15, 50, false, 100, 100, 300, 140); nicht mehr, und bei einem Y-Wert von z. B. 40 als ZielY auch nicht. Kann mir jemand helfen, was ich falsch umgesetzt habe ?

    Mein Code, darunter das Original aus Delphi.

    double GetDEG(int Winkel)
    {
       return (Winkel * 2 * 3.14159265) / 360;
    }
    //---------------------------------------------------------------------------
    double GetRAD(int Winkel)
    {
       return (Winkel * 360) / (2 * 3.14159265);
    }
    //---------------------------------------------------------------------------
    void ZeichnePfeil(int sLange, int Beta, bool Filled, int P1X, int P1Y, int P2X, int P2Y)
    {
      int Alpha;
      int AlphaZ;
    
      frmPfeil->Canvas->MoveTo(P1X, P1Y);
      frmPfeil->Canvas->LineTo(P2X, P2Y);
    
      int P1SX = P2X;
      int P1SY = P2Y;
    
      Alpha = 0;
      if (P2X == P1X)
      {
          AlphaZ = 0;
      }
      else
      {
          AlphaZ = GetRAD(atan((P2Y - P1Y) / (P2X - P1X)));
      }
    
      if ((P2X > P1X) && (P2Y == P1Y))
      {
       Alpha = 0;
      }
      else if ((P2X > P1X) && (P2Y < P1Y))
      {
          Alpha = 0 - AlphaZ;
      }
      else if ((P2X = P1X) && (P2Y < P1Y))
      {
          Alpha = 90;
      }
      else if ((P2X < P1X) && (P2Y < P1Y))
      {
          Alpha = 180 - AlphaZ;
      }
      else if ((P2X < P1X) && (P2Y = P1Y))
      {
          Alpha = 180;
      }
      else if ((P2X < P1X) && (P2Y > P1Y))
      {
          Alpha = 180 - AlphaZ;
      }
      else if ((P2X = P1X) && (P2Y > P1Y))
      {
          Alpha = 270;
      }
      else if ((P2X > P1X) && (P2Y > P1Y))
      {
          Alpha = 360 - AlphaZ;
      }
    
      int iP1X = P2X - sLange * cos(GetDEG(Alpha - Beta / 2));
      int iP1Y = P2Y + sLange * sin(GetDEG(Alpha - Beta / 2));
    
      int iP2X = P2X - sLange * cos(GetDEG(Alpha + Beta / 2));
      int iP2Y = P2Y + sLange * sin(GetDEG(Alpha + Beta / 2));
    
      frmPfeil->Canvas->MoveTo(P1SX, P1SY);
      frmPfeil->Canvas->LineTo(iP1X, iP1Y);
      frmPfeil->Canvas->MoveTo(P1SX, P1SY);
      frmPfeil->Canvas->LineTo(iP2X, iP2Y);
    }
    
    void __fastcall TfrmPfeil::Button1Click(TObject *Sender)
    {
       ZeichnePfeil(15, 50, false, 100, 100, 300, 100);
    }
    //---------------------------------------------------------------------------
    
    procedure ZeichnePfeil(Can: TCanvas; Col : TColor; SLange,Beta : Byte; Filled : Boolean; P1, P2: TPoint);
    //created by Christof Urbaczek
    
      function GetDEG(Winkel: Extended): Extended; // Winkel ins Gradmaß
      begin
        Result := (Winkel * 2 * Pi) / 360;
      end;
    
      function GetRAD(Winkel: Extended): Extended; // Winkel im Winkelmaß
      begin
        Result := (Winkel * 360) / (2 * Pi);
      end;
    
    var
      Punkte: array [0..2] of TPoint; // Array für die Punkte der Pfeilspitze
      Alpha, AlphaZ: Extended;        // Winkel zur horizontalen Achse durch P1
    
    begin
    
      //Farben einstellen
      Can.Brush.Color := Col;
      Can.Pen.Color   := Col;
    
      //Linie zeichnen
      Can.Pen.Style   := psSolid;
      Can.MoveTo(P1.X, P1.Y);
      Can.LineTo(P2.X, P2.Y);
    
      //Pfeilspitze (1.Punkt)
      Punkte[0].X := P2.X;
      Punkte[0].Y := P2.Y;
    
      //Winkel ermitteln
      Alpha := 0;
      if P2.X = P1.X then
        AlphaZ := 0
      else
        AlphaZ := GetRAD(ArcTan((P2.Y - P1.Y) / (P2.X - P1.X)));
    
      if (P2.X > P1.X) and (P2.Y = P1.Y) then Alpha := 0
      else if (P2.X > P1.X) and (P2.Y < P1.Y) then Alpha := 0 - AlphaZ
      else if (P2.X = P1.X) and (P2.Y < P1.Y) then Alpha := 90
      else if (P2.X < P1.X) and (P2.Y < P1.Y) then Alpha := 180 - AlphaZ
      else if (P2.X < P1.X) and (P2.Y = P1.Y) then Alpha := 180
      else if (P2.X < P1.X) and (P2.Y > P1.Y) then Alpha := 180 - AlphaZ
      else if (P2.X = P1.X) and (P2.Y > P1.Y) then Alpha := 270
      else if (P2.X > P1.X) and (P2.Y > P1.Y) then Alpha := 360 - AlphaZ;
    
      //2.Punkt
      Punkte[1].X := round(P2.X - sLange * cos(GetDEG(Alpha - Beta div 2)));
      Punkte[1].Y := round(P2.Y + sLange * sin(GetDEG(Alpha - Beta div 2)));
    
      //3.Punkt
      Punkte[2].X := round(P2.X - sLange * cos(GetDEG(Alpha + Beta div 2)));
      Punkte[2].Y := round(P2.Y + sLange * sin(GetDEG(Alpha + Beta div 2)));
    
      //Pfeil zeichnen
      if Filled then Can.Polygon(Punkte) else begin
        Can.MoveTo(Punkte[0].X, Punkte[0].Y);
        Can.LineTo(Punkte[1].X, Punkte[1].Y);
        Can.MoveTo(Punkte[0].X, Punkte[0].Y);
        Can.LineTo(Punkte[2].X, Punkte[2].Y);
      end;
    end;
    
    procedure TForm1.Button1Click(Sender: TObject);
    begin
      ZeichnePfeil(Image1.Canvas,clBlue,10,50,FALSE,Point(50,50),Point(100,100));
      ZeichnePfeil(Image1.Canvas,clRed ,10,50,TRUE ,Point(50,50),Point(100, 20));
    end;
    


  • Bei den if hab ich schon auf == geändert. Hat aber keine positive Auswirkung ...



  • AlphaZ = GetRAD(atan((P2Y - P1Y) / (P2X - P1X)));

    Meinste nicht dass die Integerdivision hier zu ungenau wird? Ist hier nicht casten nach double vor der Division besser? Probier mal.


Anmelden zum Antworten