matlab - to find the boundary of hexagonal structure -


i have code generating hexagonal structure in cellular network, input number of tiers required , radius.. when input number of tiers 1, generate 7 hexagonal cells when 2 generate 19 cells. need extract outer boundary of hexagonal structure without inner hexagonal cells.how can generate outer boundary of hexagonal network?

enter code here d = input('enter distance') radius = d/sqrt(3);% radius of each hexagon tier = input('enter tier value'); %num = input('enter value 0 or 1 : n_los=1 or los=0') ; g = ((2*2*d)/10)+1; % value required rearrange matrix in 3 dim  di = 0; global expected_bs; expected_bs = (tier)^2 + (tier+1)^2 + (tier*(tier+1)); display(expected_bs); global bs_cord; %base station co-ordinates %bs_cord = zeros(expected_bs, 2); bs_cord = [0 0]; t = linspace(0,2*pi,7);  % x , y co-ordinates of 7 vertices plotting hexagon. : tier 1 x_ = [   0 0 ((sqrt(3)/2)*d) ((sqrt(3)/2)*d) 0 -((sqrt(3)/2)*d) -((sqrt(3)/2)*d)  ]; y_ = [   0 d (d/2) (-d/2) -d (-d/2) (d/2)  ]; % hexagonal cells generation u=1:tier     g_ = u*x_;% first hexagon structure : x coordinate     h_ = u*y_;% first hexagon : y coordinate      bs = plot_cluster(g_ , h_ , t, radius ,bs );% function defined plot hexagon      if u > 1         m_ = u* [  0 ((sqrt(3)/2)*d) ((sqrt(3)/2)*d) 0 -((sqrt(3)/2)*d) -((sqrt(3)/2)*d) 0 ];% hexagon in inclined direction         n_ = u* [  d (d/2) (-d/2) -d (-d/2) (d/2) d ];          i=1:(u-1)             k=1:6                 inner_cell_x =  (((i * m_(k))+ ((u-i) * m_(k+1)))/(u)); % ratio method obtain coordinates of hexagon                                            inner_cell_y =  ((i * n_(k))+ ((u-i) * n_(k+1)))/(u);                 bs = plot_cell(inner_cell_x , inner_cell_y , t, radius, bs);             end         end     end end  bs_cord = unique(bs_cord,'rows'); % base station coordinates unique func eliminating replicated coordinates 

someone please help

i spent embarrassingly long amount of time working on this. hope works well.

the secret need rid of duplicate hexagons count have vertices 0 or 1 overlap.

edit added ring center

function [x, y, xe, ye, x0, y0, outercenter] = hexagon(varargin) %% hexagon: hexagon cell generating function % possible inputs %   hexagon %       assume new figure not wanted. using option call %       inputs in command window information cells. %   hexagon(newfigure) %       newfigure logical, if true cell shown on %       new figure. if false cell shown on old figure (if %       available). using option call inputs in command %       window information cells. %   hexagon(d, tiers) %       newfigure logical, if true cell shown on %       new figure. if false cell shown on old figure (if %       available). %   hexagon(d, tiers, newfigure) %       newfigure logical, if true cell shown on %       new figure. if false cell shown on old figure (if %       available). % % outputs %   x  - cell arrays of x-coordinates of hexagons %   y  - cell arrays of y-coordinates of hexagons %   xe - matrix of x-coordinates of verticies of edge %   ye - matrix of y-coordinates of verticies of edge %   x  - cell arrays of x-coordinates of hexagons on edge %   y  - cell arrays of y-coordinates of hexagons on edge  %% input parsing     global centers     %these centers of hexagons, current tier.     %having in allows program check if there     %higher tier hexagon in same space     centers = [0, 0, -1];      if nargin == 0         newfigure = 0;         d = input('enter circumscribed diameter: ');         tiers = input('enter tier value: ');     elseif nargin == 1         newfigure = varargin{1};         d = input('enter circumscribed diameter: ');         tiers = input('enter tier value: ');     elseif nargin == 2         newfigure = 0;         d = varargin{1};         tiers = varargin{2};     elseif nargin == 3         d = varargin{1};         tiers = varargin{2};         newfigure = varargin{3};     elseif nargin > 3         error('too many input arguements entered!')     end      x0 = input('enter x0: ');     y0 = input('enter y0: ');  %% call first hexagon @ origin     [x, y] = makehexagon(x0, y0, d, tiers);   %% find edge of cell     [xe, ye] = findedge(x, y, x0, y0);  %% find hexagons on edge     [x0, y0] = findedgehexagons(x, y, xe, ye);  %% outside center     outercenter = centers(:, [1,2]);     outercenter(centers(:, 3) == 0) = [];  %% output , plot     disp(['a ', num2str(tiers), ' tier network of hexagons has ', num2str(numel(x)), ' elements.'])      if newfigure         fig = figure;     else         fig = gcf;         figure(fig.number);     end      t = [x; y]; %prepare plot hexagons     plot(t{:},'linewidth',2)         hold on             t = [x0; y0]; %prepare plot hexagons             plot( t{:},'k','linewidth',4) %plot edge             plot( xe, ye,'k','linewidth',4) %plot edge         axis equal  end  %% make hexagons function [x, y] = makehexagon(x0, y0, d, tiers)     global centers      x = {};     y = {};  %% find out if center has been claimed higher tier     [~, ib] = ismembertol([x0, y0], centers(:, [1,2]),'byrows',true);      if any(ib) && (tiers > centers(ib, 3))         %if it's higher tiered previous hexagon, update list         centers(ib, 3) = tiers;     elseif any(ib) && (tiers <= centers(ib, 3))         %if it's equal or lower tiered previous hexagon, space         %it explore claimed         %this returns empty cell array         return     else         %it's in new space         centers = [centers; x0, y0, tiers];     end  %% make hexagon , children     %get verticies of hexagon, note there 7 verticies     %close shape     [x, y] = hexcoords(x0, y0, d);      %if it's not @ bottom, hexagon needs spawn children     if tiers > 0         %the centers alway @ 30, 90, 150, 210, 270 , 330.         theta = linspace(0, 5*pi/3, 6)' - pi/6;         centerx = sqrt(3)*d/2*cos(theta) + x0;         centery = sqrt(3)*d/2*sin(theta) + y0;          %call each child, 1 level lower         [x, y] = arrayfun(@(x, y) makehexagon(x, y, d, tiers - 1), centerx, centery, 'uni', 0);          %flatten cell arrays combines generations 0 tiers - 1         x = flatten(x);         y = flatten(y);     end      %combine previous generations generation     x = horzcat(x, x{:});     y = horzcat(y, y{:});      %remove shapes in same spot     [~, i, ~] = uniquetol([x', y'], 'byrows', true);      x = num2cell(x(:, i),1);     y = num2cell(y(:, i),1);  end  %% find edge of cell  function [x0, y0] = findedge(x, y, x0, y0)     %remove vertex closed shape     xy = cellfun(@(x, y) uniquetol([x(:), y(:)], 'byrows', true), x, y, 'uni', 0);      %convert cell array matrix of x , y     xy = vertcat(xy{:});      %find how many times each vertex used     [~, uniqueverticies, mappingindicies] = uniquetol(xy, 'byrows', true);     [n, ~, ~] = histcounts(mappingindicies, length(uniqueverticies));      %if once or twice it's on edge     xy = xy(uniqueverticies(n <= 2), :);      %arrange verticies angularly     data = sortrows([atan2(xy(:,2) - y0, xy(:,1) - x0), xy]);      %finally, seperate them , put final curl on     x0 = [data(:,2); data(1,2)];     y0 = [data(:,3); data(1,3)]; end  %% find hexagons on edge of cell function [x0, y0] = findedgehexagons(x, y, xe, ye)      = cellfun(@(x, y) any(ismembertol([xe(:), ye(:)], [x(:), y(:)],'byrows',true)) , x, y);     x0 = x(i);     y0 = y(i);  end  %% map verticies of hexagon function [x, y] = hexcoords(x0, y0, d)     %the centers alway @ 0, 60, 120, 180, 240, 300 , 360     %(to close figure).     theta = linspace(0, 2*pi, 7)';      %convert polar cartesian coordinates      x = d/2 * cos(theta) + x0;     y = d/2 * sin(theta) + y0; end  %% flatten cell array , remove empty elements function x = flatten(x)     x = x(~cellfun(@isempty, x));     = cellfun(@iscell, x);      if any(i)         x = horzcat(x{~i}, flatten(horzcat(x{i})));     end end 

Comments