How to colour code polygons by area?
조회 수: 2 (최근 30일)
이전 댓글 표시
Hi,
I have generated a Voronoi diagram and calculated the areas of the polygons formed. I now want to colour code my polygons by area, for example, for areas between 10-20, colour this red. Then 20-30 blue... etc. Ideally, I would like to have a continous colour fade from smallest-largest polygon area. Could someone please shed some light on how to do this. I have currently used the patch(x,y,c) function, but this does not colour by area. Thanks in advance.
댓글 수: 0
채택된 답변
Mike Garrity
2015년 10월 23일
Let's start with the Voronoi diagram with color example from the documentation. It shows using the index i as the color argument to patch. What we need to do is replace that with the area of the polygon. The polyarea function will compute that. It takes the same X & Y coordinates that patch wants. So it simply looks like this:
x = gallery('uniformdata',[10 2],5);
[v,c] = voronoin(x);
for i = 1:length(c)
if all(c{i}~=1)
x = v(c{i},1);
y = v(c{i},2);
a = polyarea(x,y);
patch(x,y,a);
end
end
Here's a more interesting example:
rng default
figure('Position',[100 100 500 600])
pts = randn(500,2);
[v,c] = voronoin(pts);
for i = 1:length(c)
if all(c{i}~=1)
x = v(c{i},1);
y = v(c{i},2);
a = polyarea(x,y);
patch(x,y,a);
end
end
hold on
scatter(pts(:,1),pts(:,2),40,[.9 .1 .1],'Marker','+','MarkerEdgeAlpha',.5)
colorbar('SouthOutside')
axis equal
xlim([-3 3])
ylim([-3 3])
caxis([0 2])
추가 답변 (1개)
Image Analyst
2015년 10월 22일
It's easy to do if you convert it to an image. Just call bwlabel(), regionprops(), sort(), and intlut(). Do you have the Image Processing Toolbox.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Voronoi Diagram에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!