如何生成Sierpinski三角形

时间:2015-03-05 07:24:35

标签: java

对于这个类,我必须使用循环和arraylist生成SierpinskiTriangle。这是我到目前为止所做的事情;但是,我不知道如何进行循环。有没有人有任何想法?谢谢。



public ArrayList<Oval> createASierpinskiTriangle() {
		int no_points  =  new Input().readIntDialog("Please enter number of points");
		if(no_points<1||no_points>MAXIMUM_NUMBER_OF_POINTS){
			JOptionPane.showMessageDialog(null, "Invalid value entered. Please enter from 1 to "+MAXIMUM_NUMBER_OF_ROWS);
		}
		ArrayList<Oval> list = new ArrayList<Oval>();
		int x1 = 250, y1 = 50;
		int x2 = 100, y2 = 350;
		int x3 = 350, y3 = 350;
		
		Random rand = new Random();
		// start point
		int x = x1, y = y1;
		
		// loop
		
		// Select randomly one of the vertices
		int v = rand.nextInt(3) + 1; // 1, 2, or 3
		// next point
		
		
		// half way between the current point and the selected vertex
		if (v == 1) {
			x = (x + x1)/2;
			y = (y + y1)/2;
		} else if (v == 2) { //...
			x = (x + x2)/2;
			y = (y + y2)/2;
			
		} else { // v is 3
			x = (x + x3)/2;
			y = (y + y3)/2;
		}
		Oval oval = new Oval(x-2,y-2,4,4,Color.BLUE,true);
		list.add(oval);
		
		// end of the loop
		
		return list;
	}
&#13;
&#13;
&#13;

0 个答案:

没有答案