A function smart enough to sort coordinates of block into sub-blocks?
조회 수: 1 (최근 30일)
이전 댓글 표시
Hi all,
Imagine there is a 2D shape made of randomly distributed square blocks, with Cartesian coordinates like this:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/169530/image.png)
I can read the coordinates of the nodes, for this case it is:
>> coords
coords =
1 -1 -1
2 -1 1
3 1 -1
4 1 1
5 -1 0
6 0 -1
7 0 0
8 0 1
9 1 0
10 2 0
11 2 1
12 0 2
13 -1 2
14 -2 0
15 -2 -1
I'd like to have a function which is able to read this coordinates data, and sort it into 4-points square blocks, with coordinates in counter-clockwise, so the output would be:
>> otpt{:}
ans =
1 -1 -1
5 -1 0
6 0 -1
7 0 0
ans =
2 -1 1
5 -1 0
7 0 0
8 0 1
ans =
3 1 -1
6 0 -1
7 0 0
9 1 0
ans =
4 1 1
7 0 0
8 0 1
9 1 0
and so on...... (7 cell blocks in this case)
Moreover, for 3D blocks, such as this case:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/169531/image.png)
If I'm able to read the nodal coordinates, can the function also suit for sorting the sub-blocks in 3D? Results would be 9 cell blocks with 8 x-y-z coordinates.
Matt J was very kind to provide me this solution here,
but it only works for uniform shapes. Thanks for any help!
댓글 수: 0
답변 (1개)
David Goodmanson
2017년 11월 13일
편집: David Goodmanson
2017년 11월 13일
Hi Xiaohan,
Here is some code that creates a 4x3xn 3d matrix sqquares (misspelled on purpose) where each level of sqquares in the third dimension, i.e. (:,:,k) is one of the solutions. I did not want to deal with a connectivity matrix so this assumes that every pair of vertices that can be connected by a line is connected by a line. For example suppose you had two more vertices at (1,2) and (2,2) and they are connected with the vertices below them to make a square. You could also have a line from (0,2) to (1,2) to make another square, or not. This code presumes that that line is there.
I am not going for cubes, life is too short.
coords = ...
[1 -1 -1
2 -1 1
3 1 -1
4 1 1
5 -1 0
6 0 -1
7 0 0
8 0 1
9 1 0
10 2 0
11 2 1
12 0 2
13 -1 2
14 -2 0
15 -2 -1];
xtaxi = -coords(:,2)+coords(:,2)'; % taxi displacements
ytaxi = -coords(:,3)+coords(:,3)';
r = xtaxi==1&ytaxi==0; % point on right or not, etc.
rd = xtaxi==1&ytaxi==-1;
d = xtaxi==0&ytaxi==-1;
ulef = find(any(r,2)&any(rd,2)&any(d,2)); % upper left corner of squares
n = length(ulef);
sqquares = zeros(4,3,n);
for k=1:n
ind = ulef(k);
vtx = [ind find(r(ind,:)) find(rd(ind,:)) find(d(ind,:))]; % cw order
sqquares(:,:,k) = coords(vtx,:)
end
if you don't have one of the later versions of Matlab with implicit expansion you can do
[temp1 temp2] = meshgrid(coords(:,2),coords(:,2));
xtaxi = temp1-temp2;
and similarly for ytaxi with coords(:,3)
댓글 수: 2
David Goodmanson
2017년 11월 13일
Hi Xiaohan,
These are just the usual displacements in x and y from the point in question, to find nearby corners. Taxi is a reference to the the path that a taxi has to take on a grid of city streets, no cutting across the diagonal. See for example Taxicab Geometry in Wikipedia.
참고 항목
카테고리
Help Center 및 File Exchange에서 Time Series에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!