Clear Filters
Clear Filters

Point array to generate meshgrid

4 views (last 30 days)
Huiyuan Zheng
Huiyuan Zheng on 15 Dec 2023
Edited: Huiyuan Zheng on 18 Dec 2023
I have a point array describing the equal-spacing points in a hexagon, how can I use this array to generate a meshgrid with identical points so that I can use pcolor?

Answers (2)

Walter Roberson
Walter Roberson on 15 Dec 2023
Xu = uniquetol(X(:) );
Yu = uniquetol(Y(:) );
[Xg, Yg] = meshgid(Xu, Yu);
F = scatteredInterpolant(X(:), Y(:), Z(:)) ;
Zg = F(Xg, Yg);
pcolor(Xg, Yg, Zg);
  3 Comments
Walter Roberson
Walter Roberson on 15 Dec 2023
The locations that are outside of the convex hull of the original points, will interpolate to NaN, and pcolor() displays NaN as transparent.
Huiyuan Zheng
Huiyuan Zheng on 18 Dec 2023
Edited: Huiyuan Zheng on 18 Dec 2023
My point array is the points in the following code. While I try to put it in your code, I don't know the result.
I divide the point array into three sections with different orientations so that the hexagonal boudnaries are all smooth. However, the problem is that, there are three blank lines in between each sections. (I delete the boundary to avoid overcounting since the hexagon is a cell in a periodic function.)
q1 = [1/2;sqrt(3)/2]; % lattice vector 1
q2 = [1/2;-sqrt(3)/2]; % lattice vector 2
q3 = -(q1+q2); % the compensated vector
np = 100;
% delete one boundary to avoid repeating calculation
r1 = linspace(0,1,np+1); r1(end) = [];
r2 = linspace(0,1,np+1); r2(1) = [];
[R1,R2] = meshgrid(r1,r2);
section1 = [q2,q3]*[R1(:),R2(:)].';
section2 = [q3,q1]*[R1(:),R2(:)].';
section3 = [q1,q2]*[R1(:),R2(:)].';
points = [section1,section2,section3];
KX1 = reshape(section1(1,:),np,np);
KY1 = reshape(section1(2,:),np,np);
KX2 = reshape(section2(1,:),np,np);
KY2 = reshape(section2(2,:),np,np);
KX3 = reshape(section3(1,:),np,np);
KY3 = reshape(section3(2,:),np,np);
figure; hold on;
pcolor(KX1,KY1,KX1*0+1);
pcolor(KX2,KY2,KX2*0+2);
pcolor(KX3,KY3,KX3*0+3);
shading interp; axis equal;

Sign in to comment.


Steven Lord
Steven Lord on 15 Dec 2023
I don't believe pcolor allows you to create elements with more or fewer than 4 sides. What I think you want to do is either create various patch objects or perhaps create an array of polyshape objects and use some of the object functions (rotate, scale, translate) to create an array of polyshape objects to plot.
H = nsidedpoly(6);
plot(H)
axis equal
H(2) = translate(H, 2, 0); % Now H has 2 hexagons
H(3:4) = translate(H, 0, 2); % Now it has 4, both of the previous two translated up 2 units
figure
plot(H)
axis equal
  1 Comment
Huiyuan Zheng
Huiyuan Zheng on 18 Dec 2023
Yes, to plot a filled hexagon uses patch is enough, while what I need is a point array describing the the equal-spacing points inside a hexagon. As you said, it seems forbidden to use a create elements with more or fewer than 4 sides, so I divide it into three sections (you can find it in the discussion of the first answer):
Strictly speaking, I have another requirement, which is to avoid the overcounting the boundary since the hexagon is a cell in a periodic function. That's why I made those modifications in my code. However, the problem is, the pcolor of the three sections are not connected, there is a blank line in between.

Sign in to comment.

Categories

Find more on Elementary Polygons in Help Center and File Exchange

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!