2

There is a grid which has been overlaid on top of a polygon. To make the discussion concrete, assume the following picture:

enter image description here

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.

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

I could figure out how to for 1x1 grid cells basically the points like this:

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

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 nxn grid cells in this same way.

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?

I saw here 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.

Community
  • 1
  • 1
user_1_1_1
  • 903
  • 1
  • 12
  • 27

1 Answers1

0

You can consider using fill command, but try to avoid it and use patch if you can. The problem with fill 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 this question to see how to use them with a conditional coloring.

My suggestion is to keep using imagesc and inpolygon, and try to numerically classify the conditions you want to apply on the bordering lattice points.

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

  • w == 0: black

  • w == 1: blue

  • w == 2: red

  • w == 3: green

  • w == 4: yellow

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

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

And this is the result:

enter image description here

Note that I shift the lattice. You should pass the coordinates of the center of lattice cells to imagesc, 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 w).

Also note that if you are interested only in the bordering cells, w > 0 & w < 4 separates them.

Community
  • 1
  • 1
Erfan
  • 1,897
  • 13
  • 23