Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

java - Gnuplot and Sierpinksi Triangle

Question:

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?

Explanation of code:

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).

Code:

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;         
        }
    }   
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

First of all, you have a couple of problems in your code:

  • 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 be x0 = sqrt3; y0 = 1.0; instead.

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

enter image description here

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
}

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
}

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

For this example I used 50 points increments and changed -delay to 100:

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...