Gnuplot和Sierpinksi Triangle [英] Gnuplot and Sierpinksi Triangle
问题描述
问题:
我有一个代码,希望能生成一个Sierpinski三角形,我想知道如何输出数据文件你读到gnuplot?我从来没有使用过gnuplot,我正试着玩它。此外,如果那是不可能的,我应该如何修改我的代码以绘制我的三角形,以便我可以看到它?
I have a code, which hopefully makes a Sierpinski triangle, and I am wondering how can I output a data file that you read into gnuplot? I have never used gnuplot and I am trying to play around with it. Also, if that is not possible, how should I modify my code in order to plot my triangle so that I can see it?
代码说明:
我正在尝试生成Sierpinski三角形从点(0,0)
开始,并且有一个 0.33
概率,下一步将介于两者之间当前点和(0,2)
。有一个 0.33
概率,下一步将在当前点和(1,sqrt3)
之间。有一个 0.33
概率,下一步将在当前点和(0,0)
之间。
I am trying to generate the Sierpinski triangle which starts at the point (0,0)
and there is a 0.33
probablity that the next step will be halfway between the current point and (0,2)
. There is a 0.33
probability that the next step will be halfway between the current point and (1,sqrt3)
. There is a 0.33
probability that the next step will be halfway between the current point and (0,0)
.
代码:
import java.util.Random;
public class SierpinskiTriangle {
public static void main(String[] args) {
//int N = Integer.parseInt(args[0]); // number of points
int N = 5000;
double sqrt3 = Math.sqrt(3);
double x = 0.0, y = 0.0; //plots
//need to draw triangle boundary
// triangle rules
for (int i = 0; i < N; i++) {
double r = Math.random();
double x0, y0;
if (r < 1/3) {
x0 = 0.0; y0 = 0.0;
} else if (r < 2/3) {
x0 = 0.0; y0 = 2.0;
} else {
x0 = 1.0; y0 = sqrt3;
}
x = (x0 + x) / 2;
y = (y0 + y) / 2;
}
}
}
推荐答案
首先,你的代码中有几个问题:
First of all, you have a couple of problems in your code:
- 整数除法只导致第三个选项是考虑,因为1/3 = 0和2/3 = 0.使用1./3。和2./3。相反。
- 您已经交换了最后一个选项的坐标,
x0 = 1.0; y0 = sqrt3;
应为x0 = sqrt3; y0 = 1.0;
代替。
- Integer division is leading to only the third option being considered, because 1/3 = 0 and 2/3 = 0. Use 1./3. and 2./3. instead.
- You have exchanged coordinates for the last option,
x0 = 1.0; y0 = sqrt3;
should bex0 = sqrt3; y0 = 1.0;
instead.
将点输出到名为数据
(我在循环中使用 System.out.println(+ x ++ y);
),你可以做gnuplot中的以下内容:
Once you output the points to a file called data
(I used System.out.println("" + x + " " + y);
within your loop), you can do the following in gnuplot:
set size ratio -1
plot "data" u 2:1 pt 7 ps 0.3
要监控三角形如何逐点创建,您可以使用带有暂停的循环:
To monitor how the triangle gets created dot by dot you can use a loop with a pause:
set xrange [0:2]
set yrange [0:1.8]
do for [i=0:4999] {
plot "data" u 2:1 every ::::i pt 7 ps 0.3
pause 0.1
}
或者你可以创建一个包含一系列png文件的动画gif:
Or you can create an animated gif with a series of png files:
set term pngcairo
do for [i=0:4999] {
set output "".i.".png"
plot "data" u 2:1 every ::::i pt 7 ps 0.3
}
期待以上是慢。您可以跳过一些帧以使其更快。然后在gnuplot外面这样做:
Expect the above to be slow. You can skip some of the frames to make it quicker. Then do this outside gnuplot:
convert -delay 10 -loop 0 *.png animation.gif
在本例中,我使用了50个点的增量并将 -delay
更改为100:
For this example I used 50 points increments and changed -delay
to 100:
这篇关于Gnuplot和Sierpinksi Triangle的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!