wolfram-mathematica - 提高(或替代)RegionPlot 的速度

标签 wolfram-mathematica plot

我想在 Manipulate 中包含一些区域图结构,但是渲染速度几乎令人望而却步。代码是

ClearAll[regions, rplot]
r:regions[n_Integer, o_Integer] := r = Apply[And, 
    Subsets[Table[(#1 - Cos[t])^2 + (#2 - Sin[t])^2 <= 1, {t, 2 Pi/n, 
       2 Pi, 2 Pi/n}], {o}], {1}] &
r:rplot[n_Integer, o_Integer] := r = Show[{RegionPlot[
     Evaluate[regions[n, o][x, y]], {x, -2, 2}, {y, -2, 2},
     PlotRange -> {{-2, 2}, {-2, 2}}, PlotRangePadding -> .1, 
     Frame -> False, PlotPoints -> 100], 
    Graphics[Table[Circle[{Cos[t], Sin[t]}, 1], {t, 2 Pi/n, 2 Pi, 2 Pi/n}]]}]

它产生的图形像
GraphicsGrid[{{rplot[3, 2], rplot[5, 3]}, {rplot[7, 2], rplot[4, 1]}}]

circles from above!

以上大约需要 40 秒在我的电脑上计算和渲染。
任何人都可以提出一种更快地获得类似质量图形的方法吗?

注意 1:我已经记住了图形对象,因此在演示中不需要每次都重新计算它 - 但即使是第一次也太慢了。
注 2:我对光栅化图像很满意,所以也许洪水填充类型的解决方案是一种选择......
注 3:我需要类似 Manipulate[ rplot[n, o], {n, 2, 10, 1, Appearance -> "Labeled"}, {{o, 1}, Range[1, (n + 1)/2], ControlType -> RadioButtonBar}] 的东西可用。

最佳答案

我之前发布了这个作为我其他答案的补充。它的灵感来自 Simon 的分析方法,并进行了一些修改以加快速度

intersect[n_, o_] :=
  With[{a = Pi/2 - (o-1) Pi/n},
   If[o-1 >= n/2, Return[{}]]; (* intersection is {} *)
   Polygon[
    Join[Table[{Sin[a] + Sin[phi], (-Cos[a] + Cos[phi])}, {phi, -a, a-2 a/10, 2 a/10}], 
     Table[{Sin[a] + Sin[phi], (Cos[a] - Cos[phi])}, {phi, a, -a+2 a/10, -2 a/10}]]]]

rplot2[n_, o_] := With[{pl = intersect[n, o], opac = .3, col = ColorData[1]},
  Graphics[{{Opacity[opac], 
     Table[{col[k], Rotate[pl, Mod[o - 1, 2] Pi/n + 2 Pi k/n, {0, 0}]}, {k, n}]},
    {Black, Circle[Through[{Re, Im}[Exp[I #]]]] & /@ (Range[n] 2 Pi/n)}}]
 ]

首先,我将其用于给定值 noi之间的交集区域-th 和 i+o-1 -th 圆与第一个和 o 之间的交叉区域相同-th 圆,除了旋转一个角度 2 Pi (i-1)/n ,因此只需计算一次区域并使用 Rotate旋转区域。

另外,我没有使用 ParametricPlot 来绘制相交区域,而是使用了 Polygon所以我只需要计算边界上的一些点就可以节省时间。
GraphicsGrid[{{rplot2[3, 2], rplot2[5, 2]}, {rplot2[7, 3], rplot2[4, 1]}}] 的结果好像

Intersecting circles revisited

我得到的时间是
rplot2[10, 3]; // Timing

(* ==> {0.0016, Null} *)

与西蒙的解决方案相比
rplot[10, 3]; // Timing

(* ==> {0.16519, Null} *)

关于wolfram-mathematica - 提高(或替代)RegionPlot 的速度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8409884/

相关文章:

javascript - 如何用多色线绘制图形

数学 8 : Convert from cartesian to spherical coordinates

wolfram-mathematica - 如何配置 Mathematica 9 自动评估所有笔记本?

wolfram-mathematica - 在mathematica中操作列表中的一个元素

recursion - 在 Mathematica 中计算此递推关系的更有效方法

Python:如何在没有错误的情况下引用可选变量

geometry - 关于 BSpline 的问题

python - 在绘图之前如何将模块转换为列表或数组?

r - 在同一图中绘制多个函数曲线

ios - 有什么方法可以快速绘制和分析数据吗?