Einzelnen Beitrag anzeigen

Blubbel

Registriert seit: 6. Okt 2011
125 Beiträge
 
Delphi 2010 Professional
 
#23

AW: Canvas- Problem mit Parabel zeichnen

  Alt 2. Nov 2011, 15:59
Delphi-Quellcode:
unit main;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, StdCtrls, Printers, Jpeg, phpUtils, ComCtrls, math;

type
  TForm1 = class(TForm)
    edtEingabe: TEdit;
    lblFX: TLabel;
    lbl_X: TLabel;
    btn_Zeichne: TButton;
    edt_Bx: TEdit;
    lbl_bx: TLabel;
    edt_C: TEdit;
    lbl_C: TLabel;
    Label2: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    Label7: TLabel;
    edtFx: TEdit;
    edt_X: TEdit;
    edt_B: TEdit;
    edt_Cx: TEdit;
    btn_Zeichne1: TButton;
    btn_neu: TButton;
    btn_close: TButton;
    Label1: TLabel;
    Label3: TLabel;
    btnPrint: TButton;
    btnSave: TButton;
    SaveDialog1: TSaveDialog;
    PrintDialog1: TPrintDialog;
    TrackBar1: TTrackBar;
    PaintBox1: TPaintBox;
    Button1: TButton;
    procedure FormCreate(Sender: TObject);
    procedure btn_ZeichneClick(Sender: TObject);
    procedure btn_Zeichne1Click(Sender: TObject);
    procedure btn_closeClick(Sender: TObject);
    procedure btn_neuClick(Sender: TObject);
    procedure btnSaveClick(Sender: TObject);
    procedure PaintBox1Paint(Sender: TObject);
    procedure FormResize(Sender: TObject);
    procedure TrackBar1Change(Sender: TObject);
    procedure btnPrintClick(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    FFuncData: TphFuncData;
    procedure Draw(ACanvas: TCanvas; AScaleFactor: Integer; AFuncData: TphFuncData);
    procedure Reset;
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;
  a,b,c: Real;

implementation

uses
  Types;

{$R *.dfm}

procedure TForm1.btnPrintClick(Sender: TObject);
var x,y:integer;
begin
  if PrintDialog1.Execute then
  begin
    Printer.BeginDoc;
    for x:=0 to Paintbox1.Height-1 do
    for y:=0 to Paintbox1.Width-1 do
    Printer.Canvas.Pixels[5*x,5*y]:=Paintbox1.Canvas.Pixels[x,y];
    Printer.EndDoc;
  end;
end;

procedure TForm1.btnSaveClick(Sender: TObject);
var
  Bitmap : TBitmap;
begin
  if SaveDialog1.Execute then
  begin
    Bitmap:=TBitmap.Create;
    Try
      Bitmap.Width:=PaintBox1.ClientWidth;
      Bitmap.Height:=PaintBox1.ClientHeight;
      BitBlt(Bitmap.Canvas.Handle,0,0,Bitmap.Width,Bitmap.Height, PaintBox1.Canvas.Handle,0,0,SRCCOPY);
      Bitmap.SaveToFile(SaveDialog1.FileName + '.bmp');
    Finally
      Bitmap.Free;
    end;
  end;
end;

procedure TForm1.Draw(ACanvas: TCanvas; AScaleFactor: Integer; AFuncData: TphFuncData);
var
  lPoint: TPoint;
  lCount, lScale, i, j, y, x: Integer;
  lIndex: Integer;
begin
  ACanvas.Brush.Style:= bsSolid;
  ACanvas.Brush.Color:= clWhite;
  ACanvas.FillRect(ACanvas.ClipRect);
                //X-Achse
  ACanvas.TextOut(0,0, RectToStr(ACanvas.ClipRect));
  ACanvas.MoveTo(0, GetHeight(ACanvas.ClipRect) div 2); //Graph
  ACanvas.LineTo(GetWidth(ACanvas.ClipRect), GetHeight(ACanvas.ClipRect) div 2);
  //Mittelpunkt ausrechnen
  lPoint:= GetCenter(ACanvas.ClipRect);
  //Scalierungswert festlegen

  //Mittelpunkt bis Rechts - X-Achse
  i:= lPoint.X;
  lCount:= 0;
  lScale:= AScaleFactor;
  while i < ACanvas.ClipRect.Right do
  begin
    inc(i,lScale);
    inc(lCount,1);

    DrawGridCaption(ACanvas, Point(i, lPoint.Y), 0, IntToStr(lCount));
  end;

  //Mittelpunkt bis Links der X-Achse
  i:= lPoint.X;
  lScale:= AScaleFactor;
  lCount:= 0;
  while i > ACanvas.ClipRect.Left do
  begin
    dec(i, lScale);
    dec(lCount, 1);

    DrawGridCaption(ACanvas, Point(i, lPoint.Y), 2, IntToStr(lCount));
  end;

  //Y-Achse Von Mittelpunkt nach unten
  lPoint:= GetCenter(ACanvas.Cliprect);
  lScale:= AScaleFactor;
  i:=lPoint.Y;
  lCount:= 0;
  while i < ACanvas.ClipRect.Bottom do
  begin
    inc(i, lScale);
    inc(lCount, -1);

    DrawGridCaption(ACanvas, Point(lPoint.X, i), 1, IntToStr(lCount));
  end;

  //Y-Achse von Mittelpunkt nach oben
  lPoint:= GetCenter(ACanvas.Cliprect);
  lScale:= AScaleFactor;
  i:=lPoint.Y;
  lCount:= 0;
  while i > ACanvas.ClipRect.Top do
  begin
    dec(i, lScale);
    dec(lCount, -1);

    DrawGridCaption(ACanvas, Point(lPoint.X, i), 3, IntToStr(lCount));
  end;
       //Y-Achse
  ACanvas.MoveTo(GetWidth(ACanvas.ClipRect) div 2, 0); //Graph
  ACanvas.LineTo(GetWidth(ACanvas.ClipRect) div 2, GetHeight(ACanvas.ClipRect));

  for lIndex := 0 to high(AFuncData) do
  begin
    case round(AFuncData[lIndex][0]) of
      0:
      begin
        for i := 0 to GetWidth(ACanvas.ClipRect) do
        begin
          x:=(i - (GetWidth(ACanvas.ClipRect) div 2)) div GetScaleValue(TrackBar1.Position);
          y:=round(CalcX2(x));
          j:=round(GetHeight(ACanvas.ClipRect) div 2) - y * GetScaleValue(TrackBar1.Position);
          ACanvas.Pixels[i, j]:=clblue;

          OutputDebugString(PChar(IntToStr(x) +'x'+ IntToStr(y)));
        end;
      end;
      1:
      begin
// for i := 0 to high - 1 do //?????????????????????????
        begin
          //CalcX2(a * sqr (x) + b * x + c)
          ACanvas.Pixels[i,j]:=clblue;
        end;
      end;
      2:
      begin
// for i := 0 to high -1 do //????????????????????????
        begin
          //CalcX2(a * sqr (x + b) + c);
          ACanvas.Pixels[i,j]:=clblue;
        end;
      end;
    end;
  end;
end;

procedure TForm1.Reset;
begin
  SetLength(Self.FFuncData, 0);
  Draw(PaintBox1.Canvas, GetScaleValue(TrackBar1.Position), Self.FFuncData);

  TrackBar1.Position := 0;
  edtEingabe.Text := '';
  edt_Bx.Text := '';
  edt_C.Text := '';
  edtFx.Text := '';
  edt_X.Text := '';
  edt_B.Text := '';
  edt_Cx.Text := '';
end;

procedure TForm1.btn_closeClick(Sender: TObject);
begin
 Form1.Close;
end;

procedure TForm1.btn_ZeichneClick(Sender: TObject);
var
  i,j:Integer;
  x,y: Real;
begin
  a:= StrToFloatDef(edtEingabe.Text, 0);
  b:= StrToFloatDef(edt_Bx.Text, 0);
  c:= StrToFloatDef(edt_C.Text, 0);
  i:=0;

  for i := 0 to GetWidth(PaintBox1.Canvas.ClipRect) do
  begin
    x:=(i - (GetWidth(PaintBox1.Canvas.ClipRect) div 2) / GetScaleValue(TrackBar1.Position));
    y:= (a * sqr (x) + (b * x) + c); //???????????????????????????????????????????
    j:=round((GetHeight(PaintBox1.Canvas.ClipRect) div 2) - y * GetScaleValue(TrackBar1.Position));
    PaintBox1.Canvas.Pixels[i,j]:=clblue;
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  SetLength(Self.FFuncData, high(FFuncData) +2);
  SetLength(Self.FFuncData[high(FFuncData)], 1);

  Self.FFuncData[high(FFuncData)][0] := 0;

  Draw(PaintBox1.Canvas, GetScaleValue(TrackBar1.Position), Self.FFuncData);
end;

procedure TForm1.btn_neuClick(Sender: TObject);
begin
  Paintbox1.Canvas.Rectangle(0,0,width,height);
  Reset;
end;

procedure TForm1.btn_Zeichne1Click(Sender: TObject);
var
  i,j:Integer;
  x,y: Real;
begin
  a:= StrToFloatDef(edt_X.Text, 0);
  b:= StrToFloatDef(edt_B.Text, 0);
  c:= StrToFloatDef(edt_Cx.Text, 0);
  i:=0;

  for i := 0 to GetWidth(PaintBox1.Canvas.ClipRect) do
  begin
    x:= (i-(GetWidth(PaintBox1.Canvas.ClipRect) div 2)) / GetScaleValue(TrackBar1.Position);
    y:= (a * sqr (x + b) + c);
    j:= round((GetHeight(PaintBox1.Canvas.ClipRect) div 2) - y * GetScaleValue(TrackBar1.Position));
    PaintBox1.Canvas.Pixels[i,j]:=clred;
  end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  self.Reset;

  Draw(PaintBox1.Canvas, GetScaleValue(TrackBar1.Position), Self.FFuncData);
end;

procedure TForm1.FormResize(Sender: TObject);
begin
  Draw(PaintBox1.Canvas, GetScaleValue(TrackBar1.Position), Self.FFuncData);
end;

procedure TForm1.PaintBox1Paint(Sender: TObject);
begin
  Draw(PaintBox1.Canvas, GetScaleValue(TrackBar1.Position), Self.FFuncData);
end;

procedure TForm1.TrackBar1Change(Sender: TObject);
begin
  Draw(PaintBox1.Canvas, GetScaleValue(TrackBar1.Position), Self.FFuncData);
end;

end.




unit phpUtils;

interface

uses
  Windows, SysUtils,Types, Graphics, Dialogs, math;

type
  TphFuncParams = array of real;
  TphFuncData = array of TphFuncParams;


  function GetWidth(AValue: TRect): Integer;
  function GetHeight(AValue: TRect): Integer;
  function RectToStr(AValue: TRect): String;
  function GetCenter(AValue: TRect): TPoint;
  function DrawGridCaption(ACanvas: TCanvas; APoint: TPoint; APos: Integer; AText: String): Boolean;
  function GetScaleValue(APos: Integer): Integer;

  function CalcX2(AXValue: real): real;

implementation

  function CalcX2(AXValue: real): real;
  begin
    result:= Power(AXValue, 2)
  end;


  function GetScaleValue(APos: Integer): Integer;
  begin
    Result:= ((APos + 10) + 1) * 10;
  end;

  function GetWidth(AValue: TRect): Integer;
  begin
    Result:= (AValue.Right - AValue.Left);
  end;

  function GetHeight(AValue: TRect): Integer;
  begin
    Result:= (AValue.Bottom - AValue.Top);
  end;

  function RectToStr(AValue: TRect): String;
  begin
    Result:= IntToStr(GetWidth(AValue)) + 'x' + IntToStr(GetHeight(AValue));
  end;

  function GetCenter(AValue: TRect): TPoint;
  begin
    Result.X := (GetWidth(AValue) div 2);
    Result.Y := (GetHeight(AValue) div 2);
  end;

  function DrawGridCaption(ACanvas: TCanvas; APoint: TPoint; APos: Integer; AText: String): Boolean;
  begin
    Result:= false;

    if Assigned(ACanvas) and (AText <> '') then
    begin
      case APos of
        0:
        begin
          ACanvas.TextOut(APoint.X-3, APoint.Y+5, AText);

          ACanvas.MoveTo(APoint.X, APoint.Y - 5);
          ACanvas.LineTo(APoint.X, APoint.Y + 5);

          Result:= true;
        end;
        1:
        begin
          ACanvas.TextOut(APoint.X+5, APoint.Y-3, AText);

          ACanvas.MoveTo(APoint.X - 5, APoint.Y);
          ACanvas.LineTo(APoint.X + 5, APoint.Y);

          Result:= true;
        end;
        2:
        begin
          ACanvas.TextOut(APoint.X-3, APoint.Y+5, AText);

          ACanvas.MoveTo(APoint.X, APoint.Y - 5);
          ACanvas.LineTo(APoint.X, APoint.Y + 5);

          Result:= true;
        end;
        3:
        begin
          ACanvas.TextOut(APoint.X+5, APoint.Y-3, AText);

          ACanvas.MoveTo(APoint.X - 5, APoint.Y);
          ACanvas.LineTo(APoint.X + 5, APoint.Y);

          Result:= true;
        end;
      end;
    end;
  end;

end.
  Mit Zitat antworten Zitat