Realization of the sine curve in Delphi drawing

Source: Internet
Author: User
Tags sin

There is no direct sine wave function in Delphi. But in scientific research, the sine wave application is very extensive, this example describes how to draw a sine curve.

For this example to draw a sine curve directly on the form, in order to prevent the window from switching the process will output incomplete graphics, so the key to draw the graphics are placed in the form of the OnPaint process, the code is as follows:

procedure TForm1.FormPaint(Sender: TObject);
var
 x: Integer;
 y,a: Double;
begin
 Canvas.Pen.Width:=3;
 Canvas.MoveTo(0,Trunc(self.ClientHeight/2));
 for x := 0 to self.ClientWidth do
 begin
  a := (x/self.ClientWidth) * 2 * Pi;
  y := Sin(a);
  y := (1-y)*self.ClientHeight/2;
  Canvas.LineTo(Trunc(x), Trunc(y));
 end;
end;

The program first sets the width of the brush in the form's canvas object, and moves the drawing's starting point to the form (0,trunc self. CLIENTHEIGHT/2)) is located. And then through a loop of a:= (x/self. clientwidth) The *2*PI statement converts the angular value in a sinusoidal period to radians and stores the result of the sine calculation in the variable Y. Finally, the Canvas.lineto (Trunc (x), Trunc (y)) statements in the loop draw a continuous sine curve on the form.

The program code is as follows:

unit Unit1;
Interface
uses
Windows, Messages, sysutils, variants, Classes, Graphics, Controls, Forms,
Dialogs, Extctrls, Stdctrls;
Type
TForm1 = Class (Tform)
Procedure Formpaint (sender:tobject);
Procedure formresize (sender:tobject);
Procedure formcreate (sender:tobject);
Private
{private declarations}
Public
{public declarations}
end;
var
form1:tform1;
Implementation
{$R *.DFM}
Procedure Tform1.formpaint (sender:tobject);
var
x:integer;
Y,a:double;
 Begin
Canvas.pen.width:=3;
Canvas.moveto (0,trunc) (self. CLIENTHEIGHT/2));
for x: = 0 to self. ClientWidth do
Begin
A: = (x/self. clientwidth) * 2 * Pi;
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
Procedure Tform1.formcreate (sender:tobject);
Begin
Self. Doublebuffered:=truE
//Prevent graphics from flashing
end;
End.

Save the file, and then press the F9 key to run the program. As the program runs, it automatically draws a sine curve on the form, and the results are shown in Figure 1.




Figure 1 Program run results

Learning through this program, not only can draw a sine curve, but also can draw cosine curves, tangent curves and other various curves, to facilitate scientific research.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.