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