1

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.

Attachments Sinus.png 66.51 KB
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
            }
        }
    }
}
2
Contributors
2
Replies
25
Views
4 Years
Discussion Span
Last Post by ddanbe
0

This is preaty slick!!
I am just learning C# so I olny understand ~ half of the code.
The cool thing is I understand ~ half of the code!!

0

You can always ask a question if there is a part of the code you don't understand.

Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.