当前位置:编程学习 > C/C++ >>

函数绘图(一)

 

今天看到了校内上一个batman equation,觉得很顺不舒服。第一个是因为我觉得那个图是错的,第二个是因为这让我开始思考如何对任意的f(x, y)进行绘制。其实这是个很困难的问题。但是如果我假设f(x, y)是处处可微的,那么问题说不定会简单一点。因此今天晚上就忍不住开始写了。我的想法是,对于屏幕上的所有点,分别令x或者y等于该点的其中一个坐标元素,对f(x, y)的的另一个自变量做牛顿迭代法。这样所有的点最后就会收敛到一个点集,然后我把它画出来,大概就是函数图象了吧。

 

    不过因为开始思考的时候已经晚了,所以今天就只写了一点点代码,用C#,可以在Vczh Library++3.0的Candidate\Games\FunctionVisualizer找到(啊,我就是喜欢把代码都往Candidate里面塞)。现在完成的有,对于一个可以包含空格的表达式e,我把它语法分析称语法树,然后再对x和y求导,就得到了三颗语法树。然后我对他进行化简,就得到了六棵语法树。当然这个化简只是简单的,并没有对函数的组合进行处理。

 

    伟大的C#也是可以用指针的,啊哈哈哈哈,下面是语法分析的代码:   1 using System;

  2 using System.Collections.Generic;

  3 using System.Linq;

  4 using System.Text;

  5 using FvCalculation.OperatorExpressions;

  6 using FvCalculation.PrimitiveExpressions;

  7

  8 namespace FvCalculation

  9 {

 10     unsafe static class ExpressionParser

 11     {

 12         private static void SkipSpaces(char** input)

 13         {

 14             while (char.IsWhiteSpace(**input))

 15             {

 16                 (*input)++;

 17             }

 18         }

 19

 20         private static bool Char(char** input, char c)

 21         {

 22             SkipSpaces(input);

 23             if (**input == c)

 24             {

 25                 (*input)++;

 26                 return true;

 27             }

 28             else

 29             {

 30                 return false;

 31             }

 32         }

 33

 34         private static double Number(char** input)

 35         {

 36             SkipSpaces(input);

 37             bool dotted = false;

 38             string s = "";

 39             while (true)

 40             {

 41                 if ('0' <= **input && **input <= '9')

 42                 {

 43                     s += **input;

 44                     (*input)++;

 45                 }

 46                 else if ('.' == **input && !dotted)

 47                 {

 48                     dotted = true;

 49                     s += **input;

 50                     (*input)++;

 51                 }

 52                 else

 53                 {

 54                     break;

 55                 }

 56             }

 57             if (s == "")

 58             {

 59                 return double.NaN;

 60             }

 61             else

 62             {

 63                 return double.Parse(s);

 64             }

 65         }

 66

 67         private static string Name(char** input)

 68         {

 69             SkipSpaces(input);

 70             string s = "";

 71             while (true)

 72             {

 73                 i

补充:软件开发 , C语言 ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,