What is the meaning of position in the code? Why position(2) then position (1)

7 views (last 30 days)
function plotmap(map,steps)
%Plots the map. See map_convert
[X,Y]=meshgrid(1:20,1:20);
plot(X,Y,'k'); hold on
plot(Y,X,'k');
axis([0, 20, 0, 16]);
axis off
[maprows,mapcols]=size(map);
for i=1:maprows,
for j=1:mapcols,
if (map(i,j) == 1)
placeblock([16-i,j]);
end;
end
end
%plot row indices
for i = 1:maprows,
c=sprintf('%d',i);
text(0.5,maprows-i+1+0.5,c,'FontSize',8);
text(mapcols+1+0.5,maprows-i+1+0.5,c,'FontSize',8);
end
%plot col indices
for i = 1:mapcols,
c=sprintf('%d',i);
text(0.5+i,maprows+1+0.5,c,'FontSize',8);
text(0.5+i,0.5,c,'FontSize',8);
end
if (nargin == 2)
if (length(steps)>0),
for i=1:length(steps),
placestep([16-steps(i,1) steps(i,2)],i);
end
end
end
hold off
end
function placeblock(position)
position=[position(2) position(1)];
rectangle('Position',[position,1,1],'FaceColor','r');
end
function eraseblock(position)
position=[position(2) position(1)];
rectangle('Position',[position,1,1],'FaceColor','w');
end
function placestep(position,i)
position=[position(2)+0.1 position(1)+0.1];
rectangle('Position',[position,0.8,0.8],'FaceColor','y');
c=sprintf('%d',i);
text(position(1)+0.2,position(2)+0.2,c,'FontSize',10);
end

Answers (2)

dpb
dpb on 15 Jul 2022
Edited: dpb on 15 Jul 2022
position is just a dummy argument in the various functions -- and the order required by the <rectangle> function they call is the reverse of the arguments in the calling statement by the way the code was constructed -- that's all.
placeblock just draws a circle with its left edge at value of position(2) and bottom edge at position(1)
You'd have to ask the author of the code why they chose to write the functions that way; there's no inate reason it should have been. One conjecture along that line would be that the calling code was already written and so the function defined to match. Things like this often happen in happenstance coding -- a bunch of code is thrown together to solve a task and things like this drawing may well initially have been in line; just copied over and over for each case before it was decided that would make a good function after enough times.
SIDE NOTE
I've always thought it bizarre that to draw a circle in MATLAB one calls the function "rectangle" -- when a newbi some 30+ year ago I remember searching vainly for a circle function -- at that time, at least, nothing showed up in help or using lookfor or mentioned in the doc. Dunno about now whether it's easier to find the functionality or not; I didn't search.
ADDENDUM SECOND
In the last, regarding using rectangle to draw circles -- NB: that the location of the circle is still the left lower corner of the bounding rectangle, NOT the origin of the circle that one would expect with a function designed to draw a circle -- center and radius would be much more intuitive and likely more useful instead.

Walter Roberson
Walter Roberson on 16 Jul 2022
The code is using nested for loops with i for rows and j for columns. It is passing values derived from i and j to a function. The function needs to interpret the values as x and y coordinates However in MATLAB, rows (first index) corresponds to y not x, and columns (second index) corresponds to x not y. So to calculate x and y from rows and columns, you need to exchange the coordinates.

Categories

Find more on Graphics Performance in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!