您的位置:首页 > 编程语言 > Delphi

Delphi实例之绘制正弦函数图像

2014-07-19 13:47 429 查看

Delphi实例之绘制正弦函数图像



unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls;

type
TForm1 = class(TForm)
Image1: TImage;
Button1: TButton;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
Label5: TLabel;
Label6: TLabel;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
x,l:Integer;
y,a:double;
begin
Image1.Picture.Bitmap:=TBitMap.Create;
Image1.Picture.Bitmap.Width:=Image1.Width;
Image1.Picture.Bitmap.Height:=Image1.Height;
l:=Image1.Picture.Bitmap.Width;
{下面的语句用于绘制直角坐标系}
Image1.Canvas.MoveTo(0,(Image1.Height div 2));
Image1.Canvas.Pen.Color:=clBlue;
Image1.Canvas.LineTo(Image1.Width,(Image1.Height div 2));{绘制函数图像X轴}
Image1.Canvas.MoveTo((Image1.Width div 2),Image1.Height);
Image1.Canvas.LineTo((Image1.Width div 2),0);{绘制函数图像Y轴}
for x:=0 to l do
begin
a:=(x/l)*2*Pi;{角度化弧度}
y:=sin(2*a);{为了加强美观效果,这里讲振幅设为2}
y:=y*(Image1.Picture.Bitmap.Height/2);
y:=y+(Image1.Picture.Bitmap.Height/2);
Image1.Picture.Bitmap.Canvas.Brush.Style:=bsSolid;
Image1.Picture.Bitmap.Canvas.Pixels[Trunc(x),Trunc(y)]:=clRed;{当然也可以用LineTo过程来实现,但是要注意设置Pen.Width到合适的值}
end;
label1.Visible:=true;
Label2.Visible:=true;
label3.Visible:=true;
Label4.Visible:=true;
label5.Visible:=true;
Label6.Visible:=true;
end;

end.


注意,这里我用了四个标签,分别用于显示X,Y,0,f(x)=sinx,并事先将它们的Visible属性设为False,当图像绘制完成后在将Visible改为True。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: