A function smart enough to sort coordinates of block into sub-blocks?
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
Hi all,
Imagine there is a 2D shape made of randomly distributed square blocks, with Cartesian coordinates like this:
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:
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 Commenti
Risposte (1)
David Goodmanson
il 13 Nov 2017
Modificato: David Goodmanson
il 13 Nov 2017
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 Commenti
David Goodmanson
il 13 Nov 2017
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.
Vedere anche
Categorie
Scopri di più su Block Libraries in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!