熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Delphi編程 >> 正文

Delphi實現正弦曲線的繪制

2022-06-13   來源: Delphi編程 
    在Delphi中沒有直接正弦曲線的函數但是在科學研究中正弦曲線的應用是非常廣泛的本實例介紹如何繪制正弦曲線

    本實例要在窗體上直接繪制正弦曲線為了防止窗口切換的過程中會輸出不完整的圖形因此繪制圖形的關鍵代碼都放在了窗體的OnPaint過程中代碼如下

    procedure TFormFormPaint(Sender: TObject);
    var
    x: Integer;
    ya: Double;
    begin
    CanvasPenWidth:=;
    CanvasMoveTo(Trunc(selfClientHeight/));
    for x := to selfClientWidth do
    begin
    a := (x/selfClientWidth) * * Pi;
    y := Sin(a);
    y := (y)*selfClientHeight/;
    CanvasLineTo(Trunc(x) Trunc(y));
    end;
    end;

    程序首先設置了窗體的Canvas對象中畫筆的寬度並且把繪圖的起點移動到了窗體中(Trunc(selfClientHeight/))的位置然後通過一個循環中的a:= (x/selfClientWidth)**Pi語句將一個正弦周期內的角度值轉換為弧度值並且把正弦計算後的結果存儲在變量y中最後循環中的CanvasLineTo(Trunc(x) Trunc(y))語句就會在窗體上繪制出連續的正弦曲線

    程序代碼如下

    unit Unit;
    interface
    uses
    Windows Messages SysUtils Variants Classes Graphics Controls Forms
    Dialogs ExtCtrls StdCtrls;
    type
    TForm = class(TForm)
    procedure FormPaint(Sender: TObject);
    procedure FormResize(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    private
    { Private declarations }
    public
    { Public declarations }
    end;
    var
    Form: TForm;
    implementation
    {$R *dfm}
    procedure TFormFormPaint(Sender: TObject);
    var
    x: Integer;
    ya: Double;
    begin
    CanvasPenWidth:=;
    CanvasMoveTo(Trunc(selfClientHeight/));
    for x := to selfClientWidth do
    begin
    a := (x/selfClientWidth) * * Pi;
    y := Sin(a);
    y := (y)*selfClientHeight/;
    CanvasLineTo(Trunc(x) Trunc(y));
    end;
    end;
    procedure TFormFormResize(Sender: TObject);
    begin
    Refresh;
    end;
    procedure TFormFormCreate(Sender: TObject);
    begin
    selfDoubleBuffered:=true;
    //防止圖形閃爍
    end;
    end

    保存文件然後按F鍵運行程序在程序運行過程中會自動在窗體上繪制正弦曲線運行結果如圖所示

程序運行結果

    通過本程序的學習不但可以繪制正弦曲線也可以繪制余弦曲線正切曲線等各式各樣的曲線方便科學研究


From:http://tw.wingwit.com/Article/program/Delphi/201311/24751.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.