How to implement a grapher in C#
Posted
by
iansinke
on Stack Overflow
See other posts from Stack Overflow
or by iansinke
Published on 2011-01-15T13:08:58Z
Indexed on
2011/01/15
13:53 UTC
Read the original article
Hit count: 197
So I'm writing a graphing calculator. So far I have a semi-functional grapher, however, I'm having a hard time getting a good balance between accurate graphs and smooth looking curves.
The current implementation (semi-pseudo-code) looks something like this:
for (float i = GraphXMin; i <= GraphXMax; i++)
{
PointF P = new PointF(i, EvaluateFunction(Function, i)
ListOfPoints.Add(P)
}
Graphics.DrawCurve(ListOfPoints)
The problem with this is since it only adds a point at every integer value, graphs end up distorted when their turning points don't fall on integers (e.g. sin(x)^2).
I tried incrementing i by something smaller (like 0.1), which works, but the graph looks very rough.
I am using C# and GDI+. I have SmoothingMethod set to AntiAlias, so that's not the problem, as you can see from the first graph. Is there some sort of issue with drawing curves with a lot of points? Should the points perhaps be positioned exactly on pixels?
I'm sure some of you have worked on something very similar before, so any suggestions? While you're at it, do you have any suggestions for graphing functions with asymptotes? e.g. 1/x^2
P.S. I'm not looking for a library that does all this - I want to write it myself.
© Stack Overflow or respective owner