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.
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 sine, but also can draw cosine curve, tangent curve and other kinds of curves, to facilitate scientific research
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.