Calculate Voronoi Cell Area

user1380978 picture user1380978 · May 8, 2012 · Viewed 6.9k times · Source

I am trying to calculate the Area of each Voronoi cell in matlab but I am stuck. I found this code online:

[v , c] = voronoin(sdata);
for i = 1 : size(c ,1)
  ind = c{i}';
  tess_area(i,1) = polyarea( v(ind,1) , v(ind,2) );
end

This code does not work because one of the points in v is [Inf,Inf]. This is a point in infinity. How can I bypass this problem?

Answer

kitchenette picture kitchenette · May 8, 2012

According to the example in the documentation for 'voronoin', the first point of v will always be [Inf, Inf]. Here's a quote:

The first row of V is a point at infinity. If any index in a cell of the cell array is 1, then the corresponding Voronoi cell contains the first point in V, a point at infinity. This means the Voronoi cell is unbounded.

If you want to use 'polyarea' on the vertices v without getting NaNs then my naive answer is to chop off the first row of v before inputting it into 'polyarea':

 sdata = [ 0.5    0
      0      0.5
     -0.5   -0.5
     -0.2   -0.1
     -0.1    0.1
      0.1   -0.1
      0.1    0.1 ]
[v , c] = voronoin(sdata);
for i = 1 : size(c ,1)
  ind = c{i}';
  tess_area(i,1) = polyarea( v(2:end,1), v(2:end,2))
end

But if that's not what you meant by "not working" then perhaps you can elaborate more on what you expected to see?