设为首页 加入收藏

TOP

函数绘图(一) (一)
2014-11-23 22:53:52 来源: 作者: 【 】 浏览:3
Tags:函数 绘图

今天看到了校内上一个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 if (('a' <= **input && **input <= 'z') || ('A' <= **input && **input <= 'Z') || ('_' == **input) || (s != "" && '0' <= **input && **input <= '9'))

74 {

75 s += **input;

76 (*input)++;

77 }

78 else

79 {

80 break;

81 }

82 }

83 return s == "" null : s;

84 }

85

86 private static Expression Exp0(char** input)

87 {

88 if (Char(input, '('))

89 {

90 Expression e = Exp3(input);

91 if (!Char(input, ')'))

92 {

93 throw new ArgumentException("Error encountered, at " + new string(*input));

94 }

95 return e;

96 }

97

首页 上一页 1 2 3 4 下一页 尾页 1/4/4
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇 日期/时间校验(yyyyMMddHHmmss) 下一篇hdu 1496 Equations(非常巧妙的h..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: