The luxuries we now have in the amount of pixels on a screen to draw some amazing graphs with, were lacking in the early days of computing. Most of the time you had to resort to rude printed output. It wasn’t that bad always. Sometimes it was and still is sufficient; just to have a quick preview of what a function would look like.
Here I present some code that does just that. It plots a function to the console. I included a screen dump(which passed through Paint) just to show what it looks like. You could also use the SetOut method of the Console class to write to a text file instead, and then print it out on paper.
Just start a new Console application in VS2010 and fill in the code. Enjoy!
Highlights:
Use of constants and constant calculations.
Use of delegates.
Use of named parameters.
Plotting a function in a Console window in C#
using System;
namespace ConsolePlotting
{
class Program
{
const char BLANK = ' ';
const char DOT = '.';
const char X = 'x';
const int cMaxLineChars = 79;
const int cHalf = cMaxLineChars / 2;
static char[] LINE = new char[cMaxLineChars];
delegate double FUNC(double X);
static void Main(string[] args)
{
DoThePlot(Math.Sin);
Console.WriteLine("For another plot, press any key to continue...");
Console.ReadKey();
Console.Clear();
DoThePlot(Sinc);
Console.ReadKey();
}
static void DoThePlot(FUNC TheDelegate)
{
fillUp(LINE, WithChar: DOT); // line of dots for "vertical" axis
Console.WriteLine(LINE);
fillUp(LINE, WithChar: BLANK); // clear the line
PlotFunc(TheDelegate);
}
// just another function to show that this delegate points to functions with same signature
static double Sinc(double x)
{
return Math.Sin(x) / x;
}
static void fillUp(char[] line, char WithChar = '\0')
{
for (int i = 0; i < line.Length; i++)
{
line[i] = WithChar;
}
}
static void PlotFunc(FUNC f)
{
double maxval = 9.0; //arbitrary values
double delta = 0.2; //size of iteration steps
int loc;
LINE[cHalf] = DOT; // for "horizontal" axis
for (double x = 0.0001; x < maxval; x += delta) //0.0001 to avoid DIV/0 error
{
loc = (int)Math.Round(f(x) * cHalf) + cHalf;
LINE[loc] = X;
Console.WriteLine(LINE);
fillUp(LINE, WithChar: BLANK); // blank the line, remove X point
LINE[cHalf] = DOT; // for horizontal axis
}
}
}
}
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.