0

I'm trying to write a function that will calculate the distance between points in a 2D matrix, calculate a distance type, and then show a visual version of the matrix with a line between the two points fed as parameters with the distance. Only the points fed as parameters should be colored in the output matrix, and there should be a grid.

My code is:

function[resultIm dist] = find_show_distance(inputMat, loc1, loc2, 
   distType)

[rows, cols] = size(inputMat);
resultIm = zeros(rows, cols);

switch distType
case 0
    dist = sqrt((loc2(1) - loc1(1))^2 + (loc2(2) - loc1(2))^2);
case 1
    dist = max((loc1(1) - loc2(1)), (loc1(2) - loc2(2)));
otherwise
    dist = abs(loc1(1) - loc2(1)) + abs(loc1(2) - loc2(2));

end
resultIm(loc1) = 4;
resultIm(loc2) = 155;

image(resultIm);
dist
end
user311302
  • 97
  • 5
  • What are the inputs for your test case? – Matt Jan 26 '18 at 19:11
  • when I tested it on find_show_distance(inp,[1,1],[2,3],0) with inp at a 4 x 3 matrix, I get roughly the same thing – user311302 Jan 26 '18 at 19:17
  • https://stackoverflow.com/questions/4181913/in-matlab-how-to-draw-a-grid-over-an-image this question goes over adding the grid. You want `resultIm(loc1(1),loc1(2)) = 255;resultIm(loc2(1),loc2(2)) = 100;` to get your blocks in the right place. – Matt Jan 26 '18 at 19:30
  • and also, how can I get both axes to start at zero and go by one each? – user311302 Jan 26 '18 at 22:13

1 Answers1

1

If loc1=[1,2]

resultIm(loc1) = 255;

Will work as a single index, therefore setting the first element ([1,1]) and second ([2,1]) element equal to 255.

What you want is:

resultIm(loc1(1),loc1(2)) = 255;

Same thing for coloring loc2.

Camilo Rada
  • 456
  • 3
  • 8