matlabmathematical-lattices

Trying to map a 2 D hexagonal lattice point for convex hull study


I am trying to map a 2 D hexagonal lattice point for convex hull study. But I can only get rectangular lattice. Here is the code:

Map a hexagonal lattice on a 2D plane

u = 0:1:100;
if mod(u,2) == 0;
  v = 0:2*sqrt(3):50*sqrt(3);
else 
  v = 1:2*sqrt(3):50*sqrt(3)    
end

Define the area in which the convex hull should be constructed. Send all the data points to the convhull function.

[u,v] = meshgrid(u,v);
idx = sqrt((u-25).^2+(v-25).^2) <= 25 ;
u = u(idx);
v = v(idx);
c = convhull(u,v);

Plot the image

plot(u(c),v(c),'r-',u,v,'b.');
hold off

Solution

  • The if statement doesn't work like this in MATLAB. The program will always only follow one path through, not different paths for each vector element as you seem to think.

    Instead, use logical indexing:

    u = 0:1:100;
    index = mod(u,2) == 0;
    v(index) = 0:2*sqrt(3):50*sqrt(3); 
    v(~index) = 1:2*sqrt(3):50*sqrt(3);
    

    (Not tested could fail if vector sizes here don't match).

    However, this still doesn't get you a hexagonal grid, because the meshgrid function generates a rectangular grid by definition.

    An hexagonal grid is two rectangular grids intertwined. One approach to generate it could be as follows:

    u = 0:1:100;
    v = 0:2*sqrt(3):50*sqrt(3); 
    [v,u] = meshgrid(v,u);
    v(2:2:end,:) = v(2:2:end,:)+sqrt(3);
    
    % and then the rest of your code:
    idx = sqrt((u-25).^2+(v-25).^2) <= 25;
    u = u(idx);
    v = v(idx);
    c = convhull(u,v);
    plot(u(c),v(c),'r-',u,v,'b.');
    axis equal
    

    output of code