zoukankan      html  css  js  c++  java
  • A Tool To Plot Mathematical Function

    Plot.cs

    using Microsoft.ClearScript;
    using Microsoft.ClearScript.V8;
    using Microsoft.Win32;
    using System;
    using System.IO;
    using System.IO.Packaging;
    using System.Printing;
    using System.Windows;
    using System.Windows.Media;
    using System.Windows.Xps.Packaging;
    
    namespace Plot
    {
        class Plot
        {
            [STAThread]
            static void Main(string[] args)
            {
                Console.Title = "Plot";
                var openFileDialog = new OpenFileDialog()
                {
                    Filter = "JavaScript|*.js"
                };
                while (openFileDialog.ShowDialog() != true) ;
                Console.WriteLine("Entry: ");
                Console.WriteLine("Start: ");
                Console.WriteLine("End: ");
                Console.WriteLine("Step: ");
                Console.CursorTop = 0;
                Console.CursorLeft = 7;
                var entry = Console.ReadLine();
                Console.CursorLeft = 7;
                var start = double.Parse(Console.ReadLine());
                Console.CursorLeft = 5;
                var end = double.Parse(Console.ReadLine());
                Console.CursorLeft = 6;
                var step = double.Parse(Console.ReadLine());
                var fileStream = new FileStream(openFileDialog.FileName, FileMode.Open);
                var streamReader = new StreamReader(fileStream);
                var v8ScriptEngine = new V8ScriptEngine();
                var v8Script = v8ScriptEngine.Compile(streamReader.ReadToEnd());
                v8ScriptEngine.Execute(v8Script);
                while (v8ScriptEngine.Script[entry] is Undefined)
                {
                    MessageBox.Show(entry + " not exist!");
                    Console.CursorTop = 0;
                    Console.CursorLeft = 7;
                    for (int i = 0; i < entry.Length; i++)
                    {
                        Console.Write((char)0);
                    }
                    Console.CursorLeft = 7;
                    entry = Console.ReadLine();
                    Console.CursorTop = 4;
                }
                var saveFileDialog = new SaveFileDialog()
                {
                    Filter = "XPS 文档|*.xps"
                };
                while (saveFileDialog.ShowDialog() != true) ;
                var package = Package.Open(saveFileDialog.FileName, FileMode.Create);
                var xpsDocument = new XpsDocument(package);
                var xpsDocumentWriter = XpsDocument.CreateXpsDocumentWriter(xpsDocument);
                int count = (int)((end - start) / step) + 1;
                var abscissa = new double[count];
                var ordinate = new double[count];
                for (int i = 0; i < count; i++)
                {
                    abscissa[i] = start + step * i;
                    ordinate[i] = v8ScriptEngine.Script[entry](abscissa[i]);
                }
                double semiWidth = Math.Ceiling(Math.Max(Math.Abs(start), Math.Abs(end)));
                double width = semiWidth * 200;
                double height = semiWidth * 200;
                double thickness = 0.01;
                double phi = 0.5 * Math.Sqrt(5) + 0.5;
                var drawingVisual = new DrawingVisual();
                var drawingContext = drawingVisual.RenderOpen();
                drawingContext.PushTransform(new TranslateTransform(width / 2, height / 2));
                drawingContext.PushTransform(new ScaleTransform(100, -100));
                var orangeRedPen = new Pen(Brushes.OrangeRed, thickness);
                var thickOrangeRedPen = new Pen(Brushes.OrangeRed, thickness * phi);
                var thinOrangeRedPen = new Pen(Brushes.OrangeRed, thickness / phi);
                drawingContext.DrawLine(thickOrangeRedPen, new Point(0, semiWidth), new Point(0, -semiWidth));
                drawingContext.DrawLine(thickOrangeRedPen, new Point(semiWidth, 0), new Point(-semiWidth, 0));
                var thickBluePen = new Pen(Brushes.Blue, thickness * phi);
                for (int i = 1; i < (int)semiWidth * 10; i++)
                {
                    var pen = (Pen)null;
                    if (i % 10 != 0)
                    {
                        pen = thinOrangeRedPen;
                    }
                    else
                    {
                        pen = orangeRedPen;
                    }
                    drawingContext.DrawLine(pen, new Point(0.1 * i, semiWidth), new Point(0.1 * i, -semiWidth));
                    drawingContext.DrawLine(pen, new Point(semiWidth, 0.1 * i), new Point(-semiWidth, 0.1 * i));
                    drawingContext.DrawLine(pen, new Point(-0.1 * i, semiWidth), new Point(-0.1 * i, -semiWidth));
                    drawingContext.DrawLine(pen, new Point(semiWidth, -0.1 * i), new Point(-semiWidth, -0.1 * i));
                }
                for (int i = 0; i < count - 1; i++)
                {
                    if (!double.IsNaN(ordinate[i]) && !double.IsNaN(ordinate[i + 1]))
                    {
                        drawingContext.DrawLine(thickBluePen, new Point(abscissa[i], ordinate[i]), new Point(abscissa[i + 1], ordinate[i + 1]));
                    }
                }
                drawingContext.Close();
                var printTicket = new PrintTicket()
                {
                    PageMediaSize = new PageMediaSize(width, height)
                };
                xpsDocumentWriter.Write(drawingVisual, printTicket);
                xpsDocument.Close();
                package.Close();
            }
        }
    }
  • 相关阅读:
    新概念4-38
    新概念4-37
    新概念4-36
    新概念4-35
    国史通鉴-03 天下为私 04
    新概念4-34
    西门子 框架断路器 及其他中低压配电设备资料查询
    OPC UA 的本质
    经典Scout添加等时同步设备
    同步报故障解同步启动
  • 原文地址:https://www.cnblogs.com/JebediahKerman/p/A_Tool_To_Plot_Mathematical_Function.html
Copyright © 2011-2022 走看看