CA-宇宙画师

甲虫

宇宙画师

CA(cellular automata),元胞自动机,是世界上第一位画师。他的第一幅画作甚至诞生在宇宙之前。
CA的理念很朴素,他基于画布现在的状态来推测画布将来的状态。而这个推测规则同样遵循质朴的原则。
比方上图CA1。作为CA的第一幅画作,它的推测规则是:
若一个点周围没有点,那么这个点存在,否则这个点不存在。
质朴而又富有哲学。然而画出的图案却异常繁复,并且在每一个时间点去观察,这幅画都不一样。
CA的画是流动的,并且无限

256幅画

CA大师有无数幅画作,但他的前256幅被他称为起源
这256画,从虚无到整体,从混乱到有序,是一次轮回。
虽然这些画不分高低,但有意思的是,CA更喜欢奇数次的画作,并且都为他们准备了华丽的画框。
画作节选:

阵列


十字玫瑰

降临

想要获取CA大师的256幅画作,运行一下程序即可

import Utils.ImageShowBox;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Random;

import static java.awt.image.BufferedImage.TYPE_INT_RGB;

/**
 * Copyright reserved by Beijing Xperdit Technology Co., Ltd. 12/9 0009.
 */
public class CA {
    private static final int len = 1024;
    private static int states[] = new int[9];
    private static float stateTh = 0.5f;
    private static int seed =new Random().nextInt();
    //-382239055
    //171768946
    private static Random r = new Random(seed);
    static {
        System.out.println(seed);
        for (int i = 0; i < states.length; i++) {
            states[i] = r.nextFloat() > stateTh ? 1 : 0;
        }
    }


    public static void main(String[] args) {
//        动态展示        
//        CAN(254, Long.MAX_VALUE,true);
        printAll();
    }


    private static int[][] CAN(int seed,long t,boolean print){
        ImageShowBox box = new ImageShowBox();
        if(print) box.setVisible(true);

        for (int tt = 0; tt < 9; tt++) {
            states[tt] = (seed >> tt) & 1;
        }
        int[][] arr = new int[len][len];
        arr[len/2][len/2 - 1] = 1;
        arr[len/2 + 1][len/2] = 1;
        arr[len/2][len/2] = 1;
        arr[len/2 - 1][len/2] = 1;
        arr[len/2][len/2 + 1] = 1;

        for (int time = 0; time < t; time++) {
            arr = compute(arr);
            if (print){
                box.flush(arr);
            }
        }

        return arr;
    }

    private static int[][] compute(int[][] arr){
        int[][] copy = new int[len][len];

        for (int i = 0; i < len; i++) {
            System.arraycopy(arr[i], 0, copy[i], 0, len);
        }

        for (int i = 0; i < len; i++) {
            for (int j = 0; j < len; j++) {
                copy[i][j] = nextVal(i,j,arr);
            }
        }

        arr = copy;
        return arr;
    }

    private static void printAll(){

        for (int t = 0; t < 256; t++) {

            snapshot(CAN(t,50,false),"CA" + t);

        }
    }

    private static void CA1(){

        for (int tt = 0; tt < 9; tt++) {
            states[tt] = (seed >> tt) & 1;
        }
        int[][] arr = new int[len][len];
        arr[512][512] = 1;
        arr[512][511] = 1;
        arr[512][513] = 1;

        while (true){
            ImageShowBox box = new ImageShowBox();
            box.flush(arr);
        }


    }

    private static void snapshot(int[][] arr,String name) {
        BufferedImage img = new BufferedImage(len,len,TYPE_INT_RGB);
        for (int i = 0; i < len; i++) {
            for (int j = 0; j < len; j++) {
                img.setRGB(i,j,arr[i][j] == 1 ? 16581375 : 0);
            }
        }
        File outputfile = new File(name + ".png");
        try {
            ImageIO.write(img, "png", outputfile);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    private static int[][] detail(int[][] arr) {
        int size = 50;

        int expansion = 15;

        int[][] exp = new int[size * expansion][size * expansion];

        for (int i = 0; i < size; i++) {
            for (int j = 0; j < size; j++) {
                int val = arr[i][j];
                for (int k = i * expansion ; k < (i + 1) * expansion; k++) {
                    for (int l = j * expansion; l < (j + 1) * expansion; l++) {
                        exp[k][l] = val;
                    }
                }
            }
        }

        return exp;

    }

    private static int nextVal(int x, int y, int[][] arr){
        int lc = lifesOfNeighbor(x,y,arr,2);
        lc = lc % 8;
        return states[lc];
//        return lc == 8 ? 1 : 0;
    }
    public static int lifesOfNeighbor(int x, int y, int[][] arr, int deep) {
        int lifes = 0;

        for (int i = x - deep; i <= x + deep; i++) {
            for (int j = y - deep; j <= y + deep; j++) {
                if(i == x && j == y) continue;
                if(validPos(i,j)) lifes += arr[i][j];
            }
        }



        return lifes;
    }
    public static boolean validPos(int x,int y){
        return !(x < 0 || x >= len || y < 0 || y >= len);
    }
}

package Utils;

import java.awt.*;
import java.awt.image.BufferedImage;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
 
public class ImageShowBox extends JFrame{
    
    private static final long serialVersionUID = 1L;
    private JLabel label;

    public static void main(String[] args) {
        new ImageShowBox();
    }
    public ImageShowBox(){
        init();
    }
    private void init(){
        try{
            setTitle("Utils.ImageShowBox");
            label = new JLabel();
            add(label);
            setSize(Toolkit.getDefaultToolkit().getScreenSize());

        }catch(Exception e){
            System.out.println("初始化失败"+e.getMessage());
            e.printStackTrace();
        }
    }

    public void flush(int[][] arr){
        int width = arr.length;
        int height = arr[0].length;
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        for(int x = 0;x < width; x++){
            for(int y = 0;y < height; y++){
                image.setRGB(x, y, arr[x][y] == 1 ? 0xFF000000 : 0xFFFFFFFF);
            }
        }
        Icon icon = new ImageIcon(image);
        label.setIcon(icon);
    }
    public void flush1(int[][] arr){
        int width = arr.length;
        int height = arr[0].length;
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        for(int x = 0;x < width; x++){
            for(int y = 0;y < height; y++){
                image.setRGB(x, y, arr[x][y]);
            }
        }
        Icon icon = new ImageIcon(image);
        label.setIcon(icon);
    }
}

你可能感兴趣的:(CA-宇宙画师)