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();
}
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.