필터 지우기
필터 지우기

How can I find the set of vertices of hexagons which are adjacent to a random point?

조회 수: 3 (최근 30일)
I have a set of hexagons whose blue coordinates are known:
I have a random red point and I need to know the 4 (in some cases it would be 3) points which are immediately adjacent to it. These points are contained in the black box shown for illustration.
I tried to sort the blue points by measuring distances but I keep getting extra points (shown by black arrows) or missing the required points.

채택된 답변

Matt J
Matt J 2024년 8월 13일
편집: Matt J 2024년 8월 13일
hex=[ 0 -1.0000
-0.8660 -0.5000
-0.8660 0.5000
0 1.0000
0.8660 0.5000
0.8660 -0.5000
1.7321 -1.0000
0.8660 -0.5000
0.8660 0.5000
1.7321 1.0000
2.5981 0.5000
2.5981 -0.5000
3.4641 -1.0000
2.5981 -0.5000
2.5981 0.5000
3.4641 1.0000
4.3301 0.5000
4.3301 -0.5000] %hexagon points
hex = 18x2
0 -1.0000 -0.8660 -0.5000 -0.8660 0.5000 0 1.0000 0.8660 0.5000 0.8660 -0.5000 1.7321 -1.0000 0.8660 -0.5000 0.8660 0.5000 1.7321 1.0000
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
hex=unique(hex,'rows');
given=[1.2,-1];
pts=[given;hex];
DT=delaunayTriangulation(pts);
CL=DT.ConnectivityList;
idx=any(CL==1,2);
idx=setdiff(CL(idx,:),1);
nearest=pts(idx,:)
nearest = 3x2
0 -1.0000 0.8660 -0.5000 1.7321 -1.0000
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
plot(hex(:,1),hex(:,2),'x', nearest(:,1),nearest(:,2),'o',given(:,1),given(:,2),'s');
legend Vertices Nearest Given
axis equal padded
  댓글 수: 1
Aravind Varma Dantuluri
Aravind Varma Dantuluri 2024년 8월 13일
I just saw your comment now. I noticed you generated the points on your own. Thanks for the effort and answer.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Mario Malic
Mario Malic 2024년 8월 10일
Hi,
here's some code to give you an idea
X = rand(3,3);
Y = rand(3,3);
pts = [X(:), Y(:)];
distance = squareform(pdist(pts)); % symmetric matrix that contains distances between points
% distance between pt3 and all other pts
[~, idx] = sort(distance(:, 3));
closestPts = idx(2:5) % first one is distance between pt3 and pt3 so we ignore that
  댓글 수: 2
Aravind Varma Dantuluri
Aravind Varma Dantuluri 2024년 8월 12일
Thanks for the answer but this doesn't work.
  1. Not all points are not random. Only 1 is. The rest are vertices of hexagon.
  2. Sorting by distance won't work. Take the below example where the random point is blue mark:
We need to get the 4 points marked by the red line. But if we sort by distance, we omit the black mark and get the red mark.
Mario Malic
Mario Malic 2024년 8월 12일
That random point is just an example, it can be arbitrary!

댓글을 달려면 로그인하십시오.

제품


릴리스

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by