Questions about using C # To draw function graphics

Source: Internet
Author: User

Reference http://www.cnblogs.com/zhangjun1130/archive/2010/01/04/1638874.html

C # program for drawing function graphics, and a pathological function (zz)

As mentioned in this article, you cannot use custom formulas. I use the COM control method, but the speed is relatively slow.

You need to add the Microsoft. scriptcontrol control. The formula can only be written in Javascript format.

 

Using system;
Using system. Collections. Generic;
Using system. Windows. forms;
Using system. drawing;

Namespace plotfunctionform
{
Static class Program
{

Sealed class plotform: Form
{

Static void main ()
{
Application. enablevisualstyles ();
Application. setcompatibletextrenderingdefault (false );
Application. Run (New plotform ());
}

// The function to be drawn. It is better to enter the function expression in textbox.
Double Function (Double X)
{

// Use the Microsoft scriptcontrol COM control
Msscriptcontrol. scriptcontrol SC = new msscriptcontrol. scriptcontrolclass ();
SC. Language = "JavaScript ";
String express = textbox1.text;
Express = Express. Replace ("X", X. tostring ());
Try
{
Double Y = double. parse (SC. eval (Express). tostring ());
Return y;
}
Catch (exception ex)
{MessageBox. Show (ex. Message); Return-1 ;}

// Double Z = math. Sin (X );
// Return Z;

// Return math. Sin (X );
// Double U = math. Pi-X;
// Double Pi2 = math. Pi * Math. Pi;
// Return 3 * x + math. Log (u * u)/Pi2/Pi2 + 1;
}

// Only used for display on the screen
String functionstring ()
{
Return textbox1.text;

// Return "f (x) = 3 * x ^ 2 + PI ^-4 * ln [(Pi-x) ^ 2] + 1 ";
}

Const int ybase = 24; // The height of the screen retaining Area

Textbox tbxx0, tbxx1; // value range of the function Independent Variable

Textbox textbox1;

Label label1 = new label ();
Label label2 = new label ();

Plotform ()
{
Suspendlayout ();

Button btnsubmit = new button ();
Btnsubmit. Text = "refresh ";
Btnsubmit. Location = new point (0, 0 );
Btnsubmit. size = new size (48, 24 );
Btnsubmit. Click + = new eventhandler (btnsubmit_click );

Label1.autosize = true;
Label1.location = new system. Drawing. Point (55, 7 );
Label1.name = "label1 ";
Label1.size = new system. Drawing. Size (40, 12 );
Label1.tabindex = 0;
Label1.text = "value range ";

Tbxx0 = new Textbox ();
Tbxx0.text = "-3 ";
Tbxx0.location = new point (110, 3 );
Tbxx0.size = new size (50, 20 );

Tbxx1 = new Textbox ();
Tbxx1.text = "3 ";
Tbxx1.location = new point (165, 3 );
Tbxx1.size = new size (50, 20 );

Label2.text = "formula ";
Label2.location = new point (220, 7 );
Label2.size = new size (30, 20 );

Textbox1 = new Textbox ();
Textbox1.text = "math. Sin (x )";
Textbox1.location = new point (255, 3 );
Textbox1.size = new size (200, 20 );

Controls. addrange (new control [] {btnsubmit, tbxx0, tbxx1, textbox1, label1, label2 });
TEXT = "plot ";
Backcolor = color. White;
Clientsize = new size (600,600 + ybase );
// Windowstate = formwindowstate. maximized;

Resumelayout (false );
}

Void btnsubmit_click (Object sender, eventargs E)
{
Invalidate ();
}

Protected override void onsizechanged (eventargs E)
{
Invalidate ();
Base. onsizechanged (E );
}

Protected override void onpaint (painteventargs E)
{
Double X0 = double. parse (tbxx0.text );
Double X1 = double. parse (tbxx1.text );
Graphics GC = E. graphics;
Size size = clientsize;
Int I0 = 0;
Int I1 = size. Width-1;
Int J0 = ybase;
Int J1 = size. Height-1;
Pen = new pen (color. Black, 1 );
GC. drawline (pen, I0, J0, i1, J0); // The boundary between the drawing area and the reserved area
Double RX = (x1-X0)/(I1-I0 );
Double Y0 = 0, Y1 = 1;
Getfunctionvaluerange (x0, RX, I0, i1, out y0, out Y1 );
Double ry = (Y1-y0)/(J1-J0 );
Out (GC, 0, "clientsize: {0} X {1}", I1-I0 + 1, J1-J0 + 1 );
Out (GC, 1, functionstring ());
Out (GC, 2, "X: [{0}, {1}] range: {2}", x0, X1, x1-X0 );
Out (GC, 3, "Y: [{0}, {1}] range: {2}", y0, Y1, Y1-y0 );
Out (GC, 4, "RX: {0}", 1/RX); // number of pixels for each unit value of the function Independent Variable
Out (GC, 5, "ry: {0}", 1/ry); // The number of pixels used to represent the function value per unit
Out (GC, 6, "R: {0}", Rx/ry); // if the value is smaller than 1, the image is squashed vertically, and vice versa.
Pen. Color = color. Green;
Int J = J1 + (INT) (Y0/ry );
If (j> = J0 & J <= J1) GC. drawline (pen, I0, J, i1, J); // X coordinate axis
Int I = I0-(INT) (x0/RX );
If (I> = I0 & I <= I1) GC. drawline (pen, I, J0, I, J1); // y axis
Pen. Color = color. Red;
For (I = I0; I <= I1; I ++)
{
Double X = x0 + (I-I0) * RX;
Double Y = function (X );
If (double. isinfinity (y) | double. isnan (y) continue;
J = J1-(INT) (Y-y0)/ry );
If (j> J1 | j <J0) continue;
GC. drawline (pen, I, J, I + 1, J); // plot the function.
}
Base. onpaint (E );
}

// Function value range
Void getfunctionvaluerange (double x0, double RX, int I0, int I1, out double y0, out double Y1)
{
Y0 = double. maxvalue;
Y1 = double. minvalue;
For (INT I = I0; I <= I1; I ++)
{
Double X = x0 + (I-I0) * RX;
Double Y = function (X );
If (double. isinfinity (y) | double. isnan (y) continue;
If (y0> Y) Y0 = y;
If (Y1 <Y) Y1 = y;
}
}

// Write the string at the specified position
Void out (Graphics GC, int line, string FMT, Params object [] ARGs)
{
GC. drawstring (string. format (FMT, argS), new font ("Courier New", 10), brushes. blue, new pointf (5, ybase + 15 * Line ));
}

}
}
}

Related Article

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.