How to draw parabolas in C#?

Go To StackoverFlow.com

0

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();
}
2012-04-05 15:57
by noDispName
How about you say, plot it - leppie 2012-04-05 15:59
I'm sure you tried something and it did not work, right?. - dasblinkenlight 2012-04-05 15:59
@leppie yes I mean when I write the equation x^2 - 2x -1 it will appear on the from application - noDispName 2012-04-05 16:06
@dasblinkenlight i tried but I just can draw linear ones - noDispName 2012-04-05 16:07
@KutluhanMetin That's not a problem - show us your attempt that drew lines instead of parabolas, and someone will quickly show you how to fix it - dasblinkenlight 2012-04-05 16:09
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 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

Put the code in the actual question so that it is readable. Make sure to indent it by four spaces and it will automatically syntax-colourize - Eric Lippert 2012-04-05 16:15
If it is your intention to learn basic graphics programming then by all means, build your own charting system out of basic parts like pens and graphics contexts. If it is your intention to display a chart then use the chart control that is designed for that purpose; it will be much easier - Eric Lippert 2012-04-05 16:16


5

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.

2012-04-05 16:12
by Eric Lippert


1

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.

2012-04-05 18:01
by user1012750
Ads