Delphi sine curve

Source: Internet
Author: User

In this example, a sine curve must be drawn directly on the form. To prevent incomplete graphics from being output during window switching, the key to drawing images is Code 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 ();

Y: = (1-y) * Self. clientheight/2;

Canvas. lineto (trunc (x), trunc (y ));

End;

End;

ProgramFirst, set the width of the paint brush in the canvas object of the form, and move the starting point of the drawing to the position (0, trunc (self. clientheight/2) of the form. Then a: = (x/self. clientwidth) * 2 * the PI statement converts the angle value in a sine period to the radian value, and stores the result of the sine calculation in the Y variable. Finally, the canvas. lineto (trunc (X) and trunc (y) statements in the loop will 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 ();

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 image flashing

End;

End.

Save the file and press F9 to run the program. When the program is running, the sine curve is automatically drawn on the form, as shown in result 1.

Figure 1 program running result

Through the study of this program, we can not only draw a sine curve, but also draw a cosine curve, tangent curve and other kinds of curves, convenient 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.