找回密码
 注册
Simdroid-非首页
查看: 153|回复: 2

[编程进阶] Spinski分形三角形问题(代码优化问题)【已解决】

[复制链接]
发表于 2011-4-6 21:29:27 | 显示全部楼层 |阅读模式 来自 山东济南
本帖最后由 treemantan 于 2011-4-18 07:13 编辑

有哪位大侠能看看吗,(Spinski分形三角形)

我只会用Block,我开始觉得一行应该能搞定,应该用迭代NestList就可以,但最后用了Block,还有我觉得不该出现的Do:
  1. v0 = {{0, 0}, {1, 0}, {0.5, Sqrt[3]/2}}
  2. Manipulate[
  3. Block[{x = {0, 0}, i = 0, v = {}},
  4.   Do[i = RandomInteger[{1, 3}]; AppendTo[v, x];
  5.    x = Mean[{x, v0[]}], {j, 1, counter, 1}];
  6.   Graphics[Point[v]]], {counter, 1, 8000, 1}]
复制代码
发表于 2011-4-16 21:26:23 | 显示全部楼层 来自 台湾
Simdroid开发平台

  1. newpt[x_List] := {{x[[1]], Mean@{x[[1]], x[[2]]},
  2.     Mean@{x[[1]], x[[3]]}}, {x[[2]], Mean@{x[[2]], x[[1]]},
  3.     Mean@{x[[2]], x[[3]]}}, {x[[3]], Mean@{x[[3]], x[[1]]},
  4.     Mean@{x[[3]], x[[2]]}}};
  5. pt = {{0, 0}, {2, 0}, {1, Sqrt[3]}};
  6. Stgraph[n_] :=
  7. Graphics[{Red,
  8.    Polygon /@
  9.     Nest[Flatten[ParallelMap[newpt, #], 1] &, {pt}, n]}]; Manipulate[
  10. Stgraph[i], {i, 1, 7, 1}]
复制代码

评分

1

查看全部评分

回复 不支持

使用道具 举报

发表于 2013-4-27 23:55:55 | 显示全部楼层 来自 北京
本帖最后由 chyanog 于 2013-4-28 01:20 编辑

lz的代码是想按照chao game的方法来画吧?
  1. ListPlot@NestList[(# + RandomChoice[{{0, 0}, {1, 0}, {.5, .8}}])/2 &,  N@{0, 0}, 10^4]

  2. (*着色的*)
  3. With[{data =  NestList[(# + RandomChoice@{{0, 0}, {1, 0}, {.5, .8}})/2 &,  N@{0, 0}, 10^4]},
  4. Graphics[Point[data, VertexColors -> ({1, #[[1]], #[[2]]} & /@ Rescale@data)]]]

  5. (*推广到3D*)
  6. Module[{v, pts, vc},
  7. v = {{0, 0, 0.6}, {-0.3, -0.5, -0.2}, {-0.3, 0.5, -0.2}, {0.6,  0, -0.2}};
  8. pts = NestList[(# + RandomChoice[v])/2 &, N@{0, 0, 0}, 10^4];
  9. vc = ColorData["Pastel", #] & /@ Rescale@pts[[All, 3]];
  10. Graphics3D@Point[pts, VertexColors -> vc]
  11. ]
复制代码
用Polygon的画法,
  1. triangle[{a_, b_, c_}] := {{a, (a + c)/2, (a + b)/2}, {(a + b)/2, b, (b + c)/2}, {(a + c)/2, (b + c)/2, c}};
  2. d = Nest[Join @@ triangle /@ # &, N@{{{0, 0}, {1, 0}, {.5, .8}}}, 3];
  3. Graphics[Polygon /@ d]

  4. (*triangle=Map[Mean,Partition[Tuples[#,2],3],{2}]&;*)
复制代码

评分

1

查看全部评分

回复 不支持

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|小黑屋|联系我们|仿真互动网 ( 京ICP备15048925号-7 )

GMT+8, 2024-4-20 11:01 , Processed in 0.042968 second(s), 19 queries , Gzip On, MemCache On.

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表