Delphi-PRAXiS
Seite 3 von 8     123 45     Letzte »    

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   GUI-Design mit VCL / FireMonkey / Common Controls (https://www.delphipraxis.net/18-gui-design-mit-vcl-firemonkey-common-controls/)
-   -   Probleme mit Sinus Darstellung (https://www.delphipraxis.net/184076-probleme-mit-sinus-darstellung.html)

Bjoerk 27. Feb 2015 09:28

AW: Probleme mit Sinus Darstellung
 
Eine stehende Welle könnte man in den Code in etwa so implementieren (hab den Code von Blup jetzt aber nicht probiert).

Delphi-Quellcode:
function FX1(const X: double): double;
begin
  Result := 2 * Sin(X + DeltaX);
end;

function FX2(const X: double): double;
begin
  Result := 2 * Sin(DeltaX - X + Pi);
end;

function FX3(const X: double): double;
begin
  Result := FX1(X) + FX2(X);
end;

procedure TFTest.StartDrawButtonClick(Sender: TObject);
begin
  DeltaX := 0;
  DrawTimer.Interval := 50;
  DrawTimer.Enabled := true;
end;

procedure TFTest.StopDrawButtonClick(Sender: TObject);
begin
  DrawTimer.Enabled := false;
end;

procedure TFTest.DrawTimerTimer(Sender: TObject);
begin
  DrawTimer.Enabled := false;
  try
    PaintBox1.Invalidate;
    DeltaX := DeltaX + PI / 18;
    Application.ProcessMessages;
  finally
    DrawTimer.Enabled := true;
  end;
end;


procedure TFTest.PaintBox1Paint(Sender: TObject);
..
    P := BerechnePunkteDarstellung(FX1, R, x0, y0, dx, dy);
    Canvas.Pen.Color := clBlue;
    ZeichnePunkteDarstellung(Canvas, R, P);

    P := BerechnePunkteDarstellung(FX2, R, x0, y0, dx, dy);
    Canvas.Pen.Color := clRed;
    ZeichnePunkteDarstellung(Canvas, R, P);

    P := BerechnePunkteDarstellung(FX3, R, x0, y0, dx, dy);
    Canvas.Pen.Color := clLime;
    ZeichnePunkteDarstellung(Canvas, R, P);
  end;
end;

Chris211183 2. Mär 2015 10:56

AW: Probleme mit Sinus Darstellung
 
Zitat:

Zitat von Blup (Beitrag 1291639)
P := BerechnePunkteDarstellung(sin, R, x0, y0, dx, dy);

[/delphi]

hier meckert er, in dieser Zeile !

( erwartet aber , gefunden ???:gruebel:

habe das so probiert

Delphi-Quellcode:
P := CalculatePointView(sin(r,x0, y0, dx, dy));
dann kommt inkompatible Typen....

hat mein Compiler heute Montag oder was ist daran falsch ???

DeddyH 2. Mär 2015 11:04

AW: Probleme mit Sinus Darstellung
 
Die Typdeklarationen hast Du aber mit kopiert?
Zitat:

Delphi-Quellcode:
type
  TFxFuntion = function(const x: Extended): Extended;
  TPointDynArray = array of TPoint;


Chris211183 2. Mär 2015 11:11

AW: Probleme mit Sinus Darstellung
 
na logo, alles wie beschrieben und auf mein Code umgesetzt ! das ist auch das Einzige, was rumzickt

Chris211183 2. Mär 2015 11:32

AW: Probleme mit Sinus Darstellung
 
Delphi-Quellcode:
type
   TFxFunction = function(const x: Extended): Extended;
   TPointDynArray = array of TPoint;




function CalculatePointView
(AFunc: TFxFunction; const ARect: TRect; x0, y0, dx, dy: double): TPointDynArray;
var
  x, y: double;
  i : integer;
begin                                                                          // für jede Spalte einen Punkt
   SetLength(Result, ARect.Right - ARect.Left +1);                             // Punkte berechnen
   x := x0;
   for i := Low(Result) to High(Result) do
   begin
   y := AFunc(x);
   y := -y;                                                                    // Canvas Nullpunkt obere linke Ecke mit Y- Achse nach unten !!!
   y := y0 + y;                                                                // oberen Rand Addieren
   y := y / dy;                                                                // Skalieren
   Result[i].x := ARect.Left +1;
   Result[i].Y := ARect.Top + Round(y);                                        // runden
   x := x + dx;
   end;                                                                        // nächster Punkt
end;

procedure DrawPointView
(ACanvas: TCanvas; const ARect: TRect; const APoints : TPointDynArray);
var
  h : Thandle;
   begin
   h:= SaveDC(ACanvas.Handle);
   try
   IntersectClipRect(ACanvas.Handle, ARect.Left, ARect.Top, ARect.Right, ARect.Bottom);
   Polyline(ACanvas.Handle, APoints[0], Length(APoints));
   finally
      RestoreDC(ACanvas.Handle, h);
   end;
end;

procedure THixHistoGraph.DrawFunction;
var
  R :TRect;
  x0, y0, dx, dy :double;
  P: TPointDynArray;
  begin
   R := Rect (GapLeft,
              GapTop,
              Width - GapRight + 2,
              Height - GapBottom);
   Canvas.Brush.Color := FHistoBkColor;
   Canvas.Pen.Color  := FHistoBkColor;
   Canvas.Pen.Style  := psSolid;
   Canvas.FillRect(R);
   InflateRect(R, -1, -1);
   x0 := 0;
   y0 := 3;
   dx := 0.05;
   dy := 0.05;
   P := CalculatePointView(sin, R, x0, y0, dx, dy));
   Canvas.Pen.Color := cllime;
   CalculatePointView(Canvas, R, P);
end;

Blup 2. Mär 2015 11:46

AW: Probleme mit Sinus Darstellung
 
TFxFunction wurde mit Extendet-Parameter und -Rückgabewert deklariert, da die sin-Funktion in Unit Math so deklariert ist.

Entweder du stellst die Definition auf Double um oder die Parameter und Rückgabewerte der neuen Funktionen auf Extendet.

Chris211183 2. Mär 2015 11:51

AW: Probleme mit Sinus Darstellung
 
Ahhhh, jetzt ja ! Danke !

Ihr seid immer wieder klasse ! Da lernt man richtig was !

Chris211183 2. Mär 2015 12:42

AW: Probleme mit Sinus Darstellung
 
Okay, hab jetzt alle als Extended deklariert, er macht glaube ich mucken mit dem Rectangle, da dieses ja Rückgabewerte vom Typ Integer hat.

habs mit Trunc und Round probiert, meckert er weiter, vonwegen inkompatible Typen...

DeddyH 2. Mär 2015 12:47

AW: Probleme mit Sinus Darstellung
 
Siehe Kommentar
Zitat:

Delphi-Quellcode:
P := CalculatePointView(sin, R, x0, y0, dx, dy));
   Canvas.Pen.Color := cllime;
   CalculatePointView(Canvas, R, P); //<-- Sollte das nicht eher DrawPointView heißen?


Chris211183 2. Mär 2015 13:01

AW: Probleme mit Sinus Darstellung
 
Stimmt, denn och ändert das die Typeninkompatibilität nicht....


Hier nochmal mein Code mit den Änderungen

Delphi-Quellcode:
TFxFunction = function(const x: Extended): Extended;
TPointDynArray = array of TPoint;



function CalculatePointView
(AFunc: TFxFunction; const ARect: TRect; x0, y0, dx, dy: Extended): TPointDynArray;
var
  x, y: Extended;
  i : integer;
begin                                                                          // für jede Spalte einen Punkt
   SetLength(Result, ARect.Right - ARect.Left +1);                             // Punkte berechnen
   x := x0;
   for i := Low(Result) to High(Result) do
   begin
   y := AFunc(x);
   y := -y;                                                                    // Canvas Nullpunkt obere linke Ecke mit Y- Achse nach unten !!!
   y := y0 + y;                                                                // oberen Rand Addieren
   y := y / dy;                                                                // Skalieren
   Result[i].x := ARect.Left +1;
   Result[i].Y := ARect.Top + Round(y);                                        // runden
   x := x + dx;
   end;                                                                        // nächster Punkt
end;

procedure DrawPointView
(ACanvas: TCanvas; const ARect: TRect; const APoints : TPointDynArray);
var
   h : Thandle;
   begin
   h:= SaveDC(ACanvas.Handle);
   try
   IntersectClipRect(ACanvas.Handle, ARect.Left, ARect.Top, ARect.Right, ARect.Bottom);
   Polyline(ACanvas.Handle, APoints[0], Length(APoints));
   finally
      RestoreDC(ACanvas.Handle, h);
   end;
end;

procedure THixHistoGraph.DrawFunction;
var
  R :TRect;
  x0, y0, dx, dy :Extended;
  P: TPointDynArray;
  begin
   R := Rect (trunc(FGapLeft),
              trunc(FGapTop),
              trunc(Width - FGapRight + 2),
              trunc(Height - FGapBottom));

   Canvas.Brush.Color := FHistoBkColor;
   Canvas.Pen.Color  := FHistoBkColor;
   Canvas.Pen.Style  := psSolid;
   Canvas.FillRect(R);
   InflateRect(R, -1, -1);
   x0 := 0.0;
   y0 := 3.0;
   dx := 0.05;
   dy := 0.05;
   P := CalculatePointView(sin, R, x0, y0, dx, dy));
   Canvas.Pen.Color := cllime;
   DrawPointView(Canvas, R, P);
end;


Alle Zeitangaben in WEZ +1. Es ist jetzt 01:55 Uhr.
Seite 3 von 8     123 45     Letzte »    

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