I'm working on a numerical analysis project and I want to draw graphics and parabolas on the form. Simply I want to draw a parabola like x² - 2x - 1
. So, how can I do that?
Example code:
g = this.CreateGraphics();
p = new Pen(Brushes.WhiteSmoke,1);
s = new SolidBrush(Color.Blue);
g.DrawString("x", this.Font, s, x1, y0 + 10);
g.DrawString("y", this.Font, s, x0 - 5, y2 - 20);
g.DrawRectangle(p, 400, 100, 300, 300);
for (int i = 0; i < 300; i += 30)
{
line(400, 100 + i, 700, 100 + i);
}
public void line(int x, int y, int x1, int y1)
{
g = this.CreateGraphics();
p = new Pen(Brushes.Gray, 1);
g.DrawLine(p, x, y, x1, y1);
g.Dispose();
}
public void cizgi1(int x, int y, int x1, int y1) { g = this.CreateGraphics(); p = new Pen(Brushes.Gray, 1); g.DrawLine(p, x, y, x1, y1); g.Dispose(); - noDispName 2012-04-05 16:13
I recommend that you use the System.Windows.Forms.DataVisualization.Charting library. Its documentation is here:
http://msdn.microsoft.com/en-us/library/dd489065.aspx
Something that confused me the first time I tried to use this library: there are two versions of this library that are practically identical. One of them is designed to generate charts on a back-end server. That is, you go to a web page and say "I'd like a custom chart of this stock price for the last ten years" and the tool generates you a chart on the server and sends you a bitmap of it. There is another version that is designed to generate charts in an interactive client application. You want the second, obviously. It is very easy to accidentally end up reading the documentation for the server side one, which can be confusing, so be careful.
Note also that this chart control comes built in to .NET 4, and is a separate download for earlier versions. You might need to download the control if you are using an older version of Visual Studio.
IF you want to be able to insert the parabola formula you'll need a math parser.
I personally used muParser and i've found it very simple to use. http://www.codeproject.com/Articles/18384/Parsing-Mathematical-Expressions-with-muParser for an example.
I think there are many others but I dont know them.
Now... talking about graphs... I use MS Charts. Here : http://archive.msdn.microsoft.com/mschart
Beginning in .NET Framework 4, the Chart controls are part of the .NET Framework. So you already have it! The samples are very very good. I truly suggest to download them.
Now the link between the two: - In a loop just evaluate your function via muParser at different points. - Plot your points.