Sitzung: Jeden Freitag in der Vorlesungszeit ab 16 Uhr c. t. im MAR 0.005. In der vorlesungsfreien Zeit unregelmäßig (Jemensch da?). Macht mit!

Javakurs/Übungsaufgaben/Appleman/Musterloesung: Unterschied zwischen den Versionen

K (Kategorisiert)
 
(kein Unterschied)

Aktuelle Version vom 1. August 2010, 11:46 Uhr

class Appleman {
	public static void main(String[] args) {
		//create the drawing area
		Pad drawPad = new Pad();
		
		//set limits for iteration
		int maxIter = 100000;
		double maxAbsZ = 100000;
		
		//set area to draw
		double startX = -2;
		double startY = -1;
		double endX = 1;
		double endY = 1;
		
		//set width of one pixel
		double step = 0.003;
		
		//calculate window width/height
		int width = (int)((endX - startX)/step);
		int height = (int)((endY - startY)/step);
		
		//final pad operations
		drawPad.setPadSize(width, height);
		drawPad.setVisible(true);
		
		
		
		double cReal = startX; //real part of c
		double cImg = startY; //imaginary part of c
		
		//first loop: iterate over y coordinate
		for(int y=0; y<height; y++) {
			cImg += step;
			cReal = startX;
			
			//second loop: iterate over x coordinate
			for(int x=0; x<width; x++) {
				cReal += step;
				
				//do the z_n+1 = z_n + c iteration
				int iter = pointIteration(cReal, cImg, maxAbsZ, maxIter);
 				
				//the point is included in the mandelbrot set, draw a point
				if(iter==maxIter) {
					drawPad.drawDot(x, y);
 				}
			}
		}	
		return;
	}
	
	public static int pointIteration(double cReal, double cImg, double maxAbsZ, int maxIter) {
		double zReal = 0; //real part of z_n
		double zImg = 0; //imaginary part of z_n
		
		int i=0;
		
		//z_n+1 = z_n + c
		//stop when either maxIter is reached (we assume, that the point is included in the mandelbrot set)
		// or when the abs(z_n) is big enough (we assume, that the point is not included in the mandelbrot set)
		while(i<maxIter && (zReal*zReal+zImg*zImg)<maxAbsZ) {
			zReal = zReal*zReal - zImg*zImg + cReal;
			zImg = 2*zReal*zImg + cImg;
			i++;
		}
		
		//return the number of iterations done
		return i;
	}
}