Einzelnen Beitrag anzeigen

Chris211183

Registriert seit: 19. Sep 2013
Ort: Braunschweig
204 Beiträge
 
Delphi 6 Professional
 
#62

AW: Probleme mit Sinus Darstellung

  Alt 3. Mär 2015, 13:23
Ja gerne...



Delphi-Quellcode:
unit HixHistograph;

interface

uses
  Windows, SysUtils, Classes, Controls, Graphics, StdCtrls, Variants, Forms,
  Dialogs, Math, ExtCtrls, Types, Scales;

type
   TFxFunction = function(const x: Extended): Extended;
   TPointDynArray = array of TPoint;

   THixHistoGraph = class(TCustomPanel)


procedure DrawFunction;
procedure SetColorZeroLine(const Value: TColor);
procedure DrawPointView(ACanvas: TCanvas; const ARect: TRect; const APoints : TPointDynArray);
function CalculatePointView(AFunc: TFxFunction; const ARect: TRect; x0, y0, dx, dy: Extended): TPointDynArray;



procedure THixHistoGraph.Paint;
begin
  inherited;
  FBmp.Width := Width;
  FBmp.Height := Height;

  with FBmp.Canvas do
  begin
  DrawComponent; // zeichnet Komponente mit Hintergrund
  DrawGrid; // zeichnet Hintergrundraster
  //DrawZeroLine; // Nullliniendurchgang
  DrawFunction;
  end;
end;


function THixHistoGraph.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 THixHistoGraph.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 (FGapLeft,
              FGapTop,
              Width - FGapRight + 2,
              Height - FGapBottom);

   Canvas.Brush.Color := FHistoBkColor;
   Canvas.Pen.Color := FHistoBkColor;
   Canvas.Pen.Style := psSolid;
   Canvas.FillRect(R);
   InflateRect(R, -1, -1);
   x0 := FXScale.ValMin;
   y0 := FYScale.ValMax;
   dx := 0.05;
   dy := 0.05;
   P := CalculatePointView(@System.sin, R, x0, y0, dx, dy);
   Canvas.Pen.Color := cllime;
   DrawPointView(Canvas, R, P);
end;

end.
Christian
  Mit Zitat antworten Zitat