主要代码在FormPaint上。
代码如下:
[code=delphi]
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs;
type
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
procedure FormPaint(Sender: TObject);
procedure FormResize(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
var
x,t: Integer;
y,a: Double;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
self.DoubleBuffered:=true;
//设置双缓冲 防止图形闪烁
end;
procedure TForm1.FormPaint(Sender: TObject);
begin
t:=1; //绘制周期数
Canvas.Pen.Width:=1;
Canvas.MoveTo(0,Trunc(self.ClientHeight/2)); //找到高度的中心线准备画图
for x := 0 to self.ClientWidth do
begin
a := (x/self.ClientWidth) * t * 2 * Pi;
//y := Cos(a); //余弦
y:=Sin(a); //正弦
y := (1-y)*self.ClientHeight/2; //振幅为二分之一的高度
Canvas.LineTo(Trunc(x), Trunc(y));
end;
end;
procedure TForm1.FormResize(Sender: TObject);
begin
Refresh;
end;
end.[/code]
转载请注明:鸟儿博客 » delphi绘制正弦和余弦函数的方法