How to determine maximum length in the x and y direction respectively

How do you determine the determine the maximum length in x and y direction of an irregular rectangular shape and then also show this by a line to mark this length.. If it was a regular shape i was just going to subtract the max y value from the y min value and so forth. How can can I calculate it from the ccordinates. Below is the image and attached is the text file. Any codes wil be greatly appreciated.
Capture.PNG

 Respuesta aceptada

Try this:
xy = dlmread('test2-coordinates.txt');
x = xy(:, 1);
y = xy(:, 2);
plot(x, y, 'g*');
grid on;
% Find the two points farthest apart
distances = pdist2(xy, xy);
maxDistance = max(distances(:))
[rows, columns] = find(distances == maxDistance)
index1 = rows(1);
index2 = rows(2);
% Draw a line between index1 and index2
hold on;
plot([x(index1), x(index2)], [y(index1), y(index2)], 'bo-', 'LineWidth', 2);
0000 Screenshot.png

6 comentarios

Okay thanks for the solution. I meant maximum length and width. The goal of the problem is to be able to give the maxium elongation of width and length. The figure below is of the deformed body. I dont know if I explained well, but I think you understand what i mean. Like in the image below, sorry that it the lines are not straight.
InkedCapture_LI.jpg
Do you just want the caliper widths of the data, like
maxWidth = max(x) - min(x);
maxHeight = max(y) - min(y);
or you want to draw a line from the leftmost x value to the rightmost x value, and a line from the topmost y value to the bottommost y value (Like Star Strider did)?
Thanks for your input. I would like to use that(max(x) - min(x)) when the body is 180 degrees horizontal. as you can see it is a little bit tilted. How can I find the major(orientation axis) axis which also passes through the centroid and then rotate this body through this major axis? By doing so, it makes life easier, i think.
The major axis will most likely be along one of the diagonals. Is that what you want? Otherwise you can fit all the data of the "square" to a line and should get a line running through the middle of the rectangle, not along the diagonal. Swap x and y to get the fitted line in the other direction.
Thank you. how could i fit all the data to a line? Any codes are greatly appreciated!
coefficients = polyfit(x, y, 1);
yFitted = polyval(coefficients, x);
plot(x, yFitted, 'b-', 'LineWidth', 2)

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Etiquetas

Preguntada:

el 25 de Mayo de 2019

Editada:

el 28 de Mayo de 2019

Community Treasure Hunt

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

Start Hunting!

Translated by