Coloring cells of a grid overlaid on top of a polygon with different colors
<p>There is a grid which has been overlaid on top of a polygon. To make the discussion concrete, assume the following picture:</p>

<p><a href="https://i.stack.imgur.com/tLIHQ.png" rel="nofollow"><img src="https://i.stack.imgur.com/tLIHQ.png" alt="enter image description here"></a></p>

<p>I had the yellow polygon and I want to overlay a grid as shown in the image. I wish to color the grid cells based on certain rules. They are not as simple as coloring the intersecting cells red and rest green. I want to preform a conditional check on each grid cell and then color them based on the result. I am at a loss to think how to represent the grid cells and then proceed with my conditional checks on them. This is my first issue. This is more of a data structure question.</p>

<p>Next if I know which certain cells to color orange, how do I do it? This is a graphics question.</p>

<p>I could figure out how to for <code>1x1</code> grid cells basically the points like this:</p>

<pre><code>figure
hold on

roi=[264.418629550321 234.728971962617;207.673447537473 220.710280373832;206.60278372591 78.1869158878505;75.9817987152034 66.5046728971964;58.8511777301927 447.345794392523;201.249464668094 454.355140186916;294.39721627409 380.757009345795;447.502141327623 430.990654205608;476.410064239829 262.766355140187;464.632762312634 213.700934579439;428.230192719486 145.943925233645;365.061027837259 134.261682242991;307.245182012848 152.953271028038;285.831905781585 193.841121495327];

axis([0 500 0 500])
axis equal
view([0 -90])

X=roi(:,1);
Y=roi(:,2);

[a b] = meshgrid(1:500);
inPoly1 = inpolygon(a,b,X,Y);
imagesc(inPoly1); 
line(X, Y,'Color','r'); 

for k = 1:25:500
    x = [1 500];
    y = [k k];
    plot(x,y,'Color','w','LineStyle','-');
    plot(x,y,'Color','k','LineStyle',':');
end

for k = 1:25:500
    x = [k k];
    y = [1 500];
    plot(x,y,'Color','w','LineStyle','-');
    plot(x,y,'Color','k','LineStyle',':');
end
</code></pre>

<p>This code snippet can also be used to produce above image. But this is not helpful. I want to be able to work with general <code>nxn</code> grid cells in this same way.</p>

<p>I had a naÃ¯ve idea that I will form a Matlab-cell of all the quadrilaterals that are the grid cells and then do my conditional check over them and color them just as polygons using fill command. Is there a better, easier way so that I have to reinvent the wheel as less as possible? If your advice is that I should go ahead with this idea, then can you suggest a way to form the Matlab-cell of all these quadrilaterals in a smart way?</p>

<p>I saw <a href="http://stackoverflow.com/a/12269837">here</a> but this uses Mapping toolbox which I don't have. So if someone can suggest within the realm of basic matlab toolboxes, it will be highly appreciated. Else please suggest python solutions. If it is difficult to provide code snippets, algorithmic advice would also be helpful.</p>

<p>You can consider using <code>fill</code> command, but try to avoid it and use <a href="http://www.mathworks.com/help/matlab/ref/patch.html" rel="nofollow"><code>patch</code></a> if you can. The problem with <code>fill</code> is that you will face serious performance issues when the number of lattice points increases. Take a look at the two answers I provided on <a href="http://stackoverflow.com/questions/39556915/hexagonal-lattice-which-is-randomly-colored-with-black-and-white-color/39636195#39636195">this question</a> to see how to use them with a conditional coloring.</p>

<p><strong>My suggestion</strong> is to keep using <code>imagesc</code> and <code>inpolygon</code>, and try to numerically classify the conditions you want to apply on the bordering lattice points.</p>

<p>Let's count the number of corners per lattice cell that falls inside the polygon and call it <code>w</code>. My coloring conditions are:</p>

<ul>
<li><p><code>w == 0</code>: black</p></li>
<li><p><code>w == 1</code>: blue</p></li>
<li><p><code>w == 2</code>: red</p></li>
<li><p><code>w == 3</code>: green</p></li>
<li><p><code>w == 4</code>: yellow</p></li>
</ul>

<p>So I define <code>myColors = [0 0 0; 0 0 1; 1 0 0; 0 1 0; 1 1 0];</code> as my colormap. This is how I count <code>w</code>:</p>

<pre><code>X = [264; 208; 207; 76; 59; 201; 294; 448; 476; 465; 428; 365; 307; 286];
Y = [235; 221; 78; 67; 447; 454; 381; 431; 263; 214; 146; 134; 153; 194];
xl = 10; % xl is your lattice length
yl = 25; % yl is your lattice height
xv = 0:xl:500;
yv = 0:yl:500;
[a, b] = meshgrid(xv, yv);
v = inpolygon(a, b, X, Y);

% summing up 4 adjacent lattice points and decreasing lattice dimensions by 1:
w = v(1:end-1,1:end-1) + v(2:end,1:end-1) + v(1:end-1,2:end) + v(2:end,2:end);

% shifting the new lattice to the center of previous lattice: 
xw = xl/2:xl:500 - xl/2;
yw = yl/2:yl:500 - yl/2;

% plotting
myColors = [0 0 0; 0 0 1; 1 0 0; 0 1 0; 1 1 0];
colormap(myColors)
imagesc(xw, yw, w)
axis equal tight off
</code></pre>

<p>And this is the result:</p>

<p><a href="https://i.stack.imgur.com/RT8Ho.png" rel="nofollow"><img src="https://i.stack.imgur.com/RT8Ho.png" alt="enter image description here"></a></p>

<p>Note that I shift the lattice. You should pass the coordinates of the center of lattice cells to <code>imagesc</code>, and therefore you need to do a half-lattice shift to go from borders to centers (and to reduce the size to one row and one column less, which I did when calculating <code>w</code>).</p>

<p>Also note that if you are interested only in the bordering cells, <code>w &gt; 0 &amp; w &lt; 4</code> separates them.</p>

python matlab