基於Matlab的六邊形細胞自動機

1.簡介

目前大多數細胞自動機都是矩形的,這個是六邊形的。

生存規則在倒數13 - 倒數18行。

2.演示

下面是兩種不同規則的動畫。

 

3.程序

clear;

N_col = 20;
% 列數設置
N_row = round(N_col/0.433*1.5/1.2);
Cell = zeros(N_row,N_col, 14,2);
% Cell - [細胞狀態 細胞中點座標 六個邊點座標 六個鄰近圖形序號]
% 細胞狀態(1)- [當前狀態 下一步狀態]; 0- 死亡 ,1- 存活。
% 中方點座標(2)- [x, y]
% 六個邊點座標(3~8)- [x,y]。
% 六個鄰近圖形行、列號(9~14)- [行號,列號]。


% 生成六邊形中點座標 %
y_point= 0;
for i_row =1:1:N_row
    if(mod(i_row,2)==1)
        x_point = 0.5;
    else
        x_point = 1.25;
    end
    y_point= y_point+ 0.433;
    for i_col = 1:1:N_col
        x_point = x_point+ 1.5;
        Cell(i_row, i_col,2,:)= [x_point,y_point];
    end
end

% 生成六邊形角點座標 %
Cell(:,:,3,1)= Cell(:,: ,2,1)-0.5;
Cell(:,:,[4,8],1)= repmat(Cell(:,: ,2,1)-0.25,1,1,2);
Cell(:,:,[5,7],1)= repmat(Cell(:,: ,2,1)+0.25,1,1,2);
Cell(:,:,6,1)= Cell(:,: ,2,1)+0.5;
Cell(:,:,[3,6],2)= repmat(Cell(:,: ,2,2),1,1,2);
Cell(:,:,[4,5],2)= repmat(Cell(:,: ,2,2)+0.433,1,1,2);
Cell(:,:,[7,8],2)= repmat(Cell(:,: ,2,2)-0.433,1,1,2);

% 繪製中點 %
plot(Cell(:, :,2,1),Cell(:, :,2,2),'LineStyle','none','Marker','.','color','b' );hold on;
if(Cell(N_row,N_col,2,1)>1.2*Cell(N_row,N_col,2,2))
    xlim([0, Cell(N_row,N_col,2,1)]+1.5);ylim([0, Cell(N_row,N_col,2,1)]./1.2);
else
    xlim([0, Cell(N_row,N_col,2,2).*1.2+1]);ylim([0, Cell(N_row,N_col,2,2)]);
end

% 繪製六邊形 %
pic= cell(N_row,N_col);
for i=1:1:N_row
    for j=1:1:N_col
        pic{i,j}= patch(reshape(Cell(i,j,3:8,1),[1,6]),reshape(Cell(i,j,3:8,2),[1,6]),'w');
    end
end

% 尋找鄰近圖形的行、列號(沒有細胞的位置爲[-1,-1])
for i_center=1:1:N_row
    for j_center=1:1:N_col
        for i=1:1:6
            found = 0;
            % 在目標細胞附近 士2 格的範圍內尋找,減少運算量
            for i_find= max(1, i_center-2):min(N_row, i_center+2)
                for j_find= max(1, j_center-2):min(N_col, j_center+2)
                    distance = sqrt((Cell(i_center, j_center,2,1)- Cell(i_find, j_find,2,1))^2 ...
                        + (Cell(i_center, j_center,2,2)- Cell(i_find, j_find,2,2))^2);
                    % 初步根據距離判斷
                    if distance> 0.85 && distance< 0.9
                        x_range = [1,1].* cos(1.0472* (i-1)-0.5236)* distance+ [-0.1 0.1]+ Cell(i_center, j_center,2,1);
                        y_range = [1,1].* sin(1.0472* (i-1)-0.5236)* distance+ [-0.1 0.1]+ Cell(i_center, j_center,2,2);
                        % 再根據夾角判斷
                        if  Cell(i_find, j_find,2,1)> x_range(1) && Cell(i_find, j_find,2,1)< x_range(2) ...
                                && Cell(i_find, j_find,2,2)> y_range(1) && Cell(i_find, j_find,2,2)< y_range(2)
                            Cell(i_center, j_center,i+8,:) = [i_find, j_find];
                            found= 1;
                        end
                    end
                end
            end
            if found ==0
                Cell(i_center, j_center,i+8,:)= -1;
            end
        end
    end
end

% 賦予細胞隨機初試狀態
Cell( :, :, 1, 1)=round(rand(N_row, N_col));

% 開始迭代
while 2>1
    for i=1:1:N_row
        for j=1:1:N_col
            % 計算細胞周圍存活細胞數量
            N_live = 0;
            for k=1:1:6
                if Cell( i, j, k+8, 1)~= -1 && Cell( i, j, k+8, 2)~= -1
                    N_live = N_live+ Cell(Cell( i, j, k+8, 1), Cell( i, j, k+8, 2), 1, 1);
                end
            end
            % 判斷生死
            if N_live>4 || N_live<3
                Cell( i, j, 1, 2)= 0;
            else
                Cell( i, j, 1, 2)= 1;
            end
            % 更新顯示狀態
            if Cell( i, j, 1, 1) >0.5
                set(pic{i, j},'FaceColor',[0.5 1 0.5]);
            else
                set(pic{i, j},'FaceColor',[1 1 1]);
            end
        end
    end
    drawnow;
    % 更新生死狀態
    Cell( :, :, 1, 1)= Cell( :, :, 1, 2);
end

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章