0

I have a code that takes an equation and draws it by using points. For example, if the equation is y=ax^2+b*x+1 - than I will go over the x's from -100 to 100, find their matching Y value and hold a list of points. This is part of my code inside a loop. My window height is 800 and it's width is 800, so for the point (0,0) I put a small Elipse by setting Canvas.SetTop(e1,400) and Canvas.SetLeft(e1,500) and so on. For each point of the equation I put a small elipse on the Canvas to draw the equation.

How can draw the equation better in WPF to get smooth lines, and not a dotted graph?

 Ellipse E1 = new Ellipse();
 E1.Width = 5;
 E1.Height = 5;
 E1.Fill = Brushes.Black;
 double y;
 y = Exp1;
 y = y / 5;
 x = x / 5;
 x = x + 500.0;
 y = 400.0 - y;
 Canvas.SetTop(E1, y);
 Canvas.SetLeft(E1, x);
 Can1.Children.Add(E1);
Sheridan
  • 68,826
  • 24
  • 143
  • 183
Stack Tack Tack
  • 129
  • 2
  • 11
  • 1
    Use the [Polyline](http://msdn.microsoft.com/en-us/library/system.windows.shapes.polyline.aspx) control and add data points to its `Points` property. – Clemens Jan 20 '14 at 19:52
  • I don't think it is available in raw .NET but look for cubic spline for smooth curve. If you have enough points the Polyline should look smooth. – paparazzo Jan 20 '14 at 20:07

1 Answers1

1

You can draw any shape that you need with the Path class. That includes a variety of curves that are really quite easy to use, once you know what each parameter is for. You can find an introductory article that explains the basics in the Shapes and Basic Drawing in WPF Overview page on MSDN. You can find more drawing examples in the first linked page.

One thing to note is that you'd probably find it a lot easier to define your mathematical symbols using Blend and then use the auto generated XAML, rather than trying to compose it all in C#.

If you don't want to draw it all yourself, you can find a number of alternative solutions, such as third party libraries and controls, listed in the most popular answer to the How to render a formula in WPF or WinForms question here on StackOverflow.

Community
  • 1
  • 1
Sheridan
  • 68,826
  • 24
  • 143
  • 183