Back to top

Trójkąt Sierpińskiego

  • Wielkość pliku: 650KB

http://luka.sh/projekty/fraktale

import java.awt.Color;
import java.awt.image.*;
import java.util.Random;

// Trójkąt Sierpińskiego
public class f05
{
	
	public BufferedImage draw()
	{
		int I = 100000000;
		int WIDTH = 10000; 
		int HEIGHT = 10000;
		int IMG_TYPE = java.awt.image.BufferedImage.TYPE_INT_RGB;
		
		BufferedImage image = new BufferedImage(WIDTH, HEIGHT, IMG_TYPE);
		
		ColorModel model = image.getColorModel(); 
		WritableRaster raster = image.getRaster();
		
		Color fcolor; 

		double x1, x = 0;
		double y = 0;

		int z = 0xffffff;
		fcolor = new Color(z);
		
		// maksymalna ilosc przeksztalcen
		int MIP = 3;
		
		double[] AA = {0.5, 0.5, 0.5};
		double[] AB = {0, 0, 0};
		double[] AC = {0, 0, 0};
		double[] AD = {0.5, 0.5, 0.5};
		double[] AE = {0, 0, 0.5};
		double[] AF = {0, 1, 0.5};

		// Skalowanie
		double xl = -0.1;
		double yl = -0.1;
		double xr = 2.1;
		double yr = 1.1;

		double A = WIDTH / (xr - xl);
		double B = -A * xl;
		double C = HEIGHT / (yl - yr);
		double D = -C * yr;
	
		Random random = new Random();
		int RAND = 0;
		
		for(int i = 0; i < I; i++) 
		{ 
									
			RAND = random.nextInt(MIP);
				
			x1 = AA[RAND] * x + AB[RAND] * y + AE[RAND];
			y = AC[RAND] * x + AD[RAND] * y + AF[RAND];
			x = x1;

			// punkt na płótno 
			int x2 = (int)(A * y + B);
			int y2 = (int)(C * x + D);

			if(x2 < WIDTH && y2 < HEIGHT && x2 >= 0 && y2 >= 0)
			{
				raster.setDataElements(x2, y2, 
						model.getDataElements(fcolor.getRGB(), null));
			}

		}
		
		return image;
	}
}