Gnuplot and Sierpinksi Triangle

前端 未结 1 482
梦谈多话
梦谈多话 2020-12-21 23:13

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 h

相关标签:
1条回答
  • 2020-12-21 23:55

    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

    0 讨论(0)
提交回复
热议问题