• Remix
  • Share
  • New Entry

on 23 Oct 2024
  • 21
  • 167
  • 0
  • 2
  • 1650
Cite your audio source here (if applicable):
drawframe(1);
Write your drawframe function below
function drawframe(f)
persistent seedX seedY V C DT cellPoly intersectPatches circleRadius hTri
if f == 1
% number of seed points
n = 500;
% random x and y coordinates
seedX = rand([n,1]);
seedY = rand([n,1]);
% construct Thiessen (Voronoi) polygons by Voronoi tesselation
% each element of the cell array, C, is a list of idxs to V,
% specifying the vertices of the Voronoi cells corresponding to
% each of the seed points
DT = delaunayTriangulation(seedX,seedY);
[V,C] = DT.voronoiDiagram;
% first, go through and mark any unbounded cells
unboundedIdx = [];
for i = 1:length(C)
if any([V(C{i},1),V(C{i},2)]==inf)
unboundedIdx(end+1) = i;
end
end
% remove the unbounded cells/points
seedX(unboundedIdx) = [];
seedY(unboundedIdx) = [];
C(unboundedIdx) = [];
% empty polyshape array
cellPoly = polyshape.empty();
% construct a polyshape object for each bounded Voronoi cell
for i = 1:numel(C)
cellPoly(i) = polyshape([V(C{i},1),V(C{i},2)]);
end
% set up the axes
hAx = gca;
set(hAx,...
'Visible','off',...
'Units','normalized',...
'InnerPosition',[0 0 1 1],...
'XLim',[0.25 0.75],... % zoom in a bit to hide unbounded cells
'YLim',[0.25 0.75],...
'NextPlot','add');
% the initial radius
circleRadius = 0.001;
% graphics placeholders for the intersection patches
intersectPatches = gobjects(numel(C),1);
for i = 1:numel(C)
% circle centered on this seed point
circlePoly = getCirclePoly(seedX(i),seedY(i),circleRadius);
% intersection of circle and cell
intersectPoly = intersect(cellPoly(i),circlePoly);
% patch object to plot the polyshape
intersectPatches(i,1) = patch(hAx,...
'XData',intersectPoly.Vertices(:,1),...
'YData',intersectPoly.Vertices(:,2),...
'FaceColor',rand(1,3),...
'LineWidth',1);
end
else
% get the axes handle
hAx = gca;
if f <= 48 % first half
% expand in frames 2-48
circleRadius = circleRadius + 0.0015;
else % second half
% plot a new set of faceless patches to show the edges
if f == 49
for i = 1:numel(C)
patch(hAx,...
'XData',cellPoly(i).Vertices(:,1),...
'YData',cellPoly(i).Vertices(:,2),...
'FaceAlpha',0,...
'LineWidth',1);
end
% plot the delaunay triangulation
hTri = triplot(DT);
else
% contract in frames 50-96
circleRadius = circleRadius - 0.0015;
end
% adjust the line alpha of the triangulation plot to gradually
% decrease transparency
hTri.Color = [0 0 0 (f-48)/48];
end
for i = 1:numel(C)
% circle centered on this seed point
circlePoly = getCirclePoly(seedX(i),seedY(i),circleRadius);
% intersection of circle and cell
intersectPoly = intersect(cellPoly(i),circlePoly);
% adjust the patch coordinates
set(intersectPatches(i,1),...
'XData',intersectPoly.Vertices(:,1),...
'YData',intersectPoly.Vertices(:,2));
end
end
function [circlePoly] = getCirclePoly(centerX,centerY,r)
theta = (0:1:359)';
circlePoly = polyshape([r*cosd(theta)+centerX,r*sind(theta)+centerY],"Simplify",true);
end
end
Movie
Audio

This submission does not have audio.

Remix Tree