我有许多保证在三角形内的 3D 散点 (x, y, z)
。我现在希望将 z
可视化为一个平滑 2D 热图,其中位置由 (x, y)
给出。
我可以很容易地用meshgrid
和mesh
,if (x, y)
一起形成一个长方形。因为我不想让任何东西落在我的三角形之外,所以我也不能使用 griddate
。
然后呢?
MWE
P = [0 1/sqrt(3); 0.5 -0.5/sqrt(3); -0.5 -0.5/sqrt(3)];
% Vertices
scatter(P(:, 1), P(:, 2), 100, 'ro');
hold on;
% Edges
for idx = 1:size(P, 1)-1
plot([P(idx, 1) P(idx+1, 1)], [P(idx, 2) P(idx+1, 2)], 'r');
end
plot([P(end, 1) P(1, 1)], [P(end, 2) P(1, 2)], 'r');
% Sample points within the triangle
N = 1000; % Number of points
t = sqrt(rand(N, 1));
s = rand(N, 1);
sample_pts = (1-t)*P(1, :)+bsxfun(@times, ((1-s)*P(2, :)+s*P(3, :)), t);
% Colors for demo
C = ones(size(sample_pts, 1), 1).*sample_pts(:, 1);
% Scatter sample points
scatter(sample_pts(:, 1), sample_pts(:, 2), [], C, 'filled');
colorbar;
产生
附言
正如 Nitish 所建议的,增加点数就可以了。但是有没有一种计算成本更低的方法呢?
最佳答案
使用 delaunayTriangulation
对二维数据点进行三角测量,用三角剖分的点评估你的函数,然后使用 trisurf
绘制结果表面:
在 %Colors for demo
之后,添加:
P = [P; sample_pts]; %// Add the edgepoints to the sample points, so we get a triangle.
f = @(X,Y) X; %// Defines the function to evaluate
%// Compute the triangulation
dt = delaunayTriangulation(P(:,1),P(:,2));
%// Plot a trisurf
P = dt.Points;
trisurf(dt.ConnectivityList, ...
P(:,1), P(:,2), f(P(:,1),P(:,2)), ...
'EdgeColor', 'none', ...
'FaceColor', 'interp', ...
'FaceLighting', 'phong');
%// A finer colormap gives more beautiful results:
colormap(jet(2^14)); %// Or use 'parula' instead of 'jet'
view(2);
使这个图形漂亮的技巧是使用'FaceLighting'
,'phong'
而不是'gouraud'
并使用更密集的colormap
比通常使用的要多。
以下仅使用 N = 100
样本点,但是一个很好的 colormap
(使用现在默认的 parula
colormap):
比较默认输出:
trisurf(dt.ConnectivityList, ...
P(:,1), P(:,2), f(P(:,1),P(:,2)), ...
'EdgeColor', 'none', ...
'FaceColor', 'interp');
看起来真的很难看:(我会说主要是因为奇怪的插值,但是 jet
颜色图也有它的缺点)
关于MATLAB - 从三角形内的 (x, y, z) 点平滑热图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28968592/