Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c734f02

Browse files
Get nearest graphics indices (#699)
* get_nearest_graphics_indices plot helper * map_screen_to_world can return None * black format source dir only
1 parent 0097810 commit c734f02

File tree

2 files changed

+35
-8
lines changed

2 files changed

+35
-8
lines changed

fastplotlib/layouts/_plot_area.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ def get_rect(self) -> tuple[float, float, float, float]:
299299

300300
def map_screen_to_world(
301301
self, pos: tuple[float, float] | pygfx.PointerEvent
302-
) -> np.ndarray:
302+
) -> np.ndarray | None:
303303
"""
304304
Map screen position to world position
305305

fastplotlib/utils/_plot_helpers.py

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@
66
from ..graphics._collection_base import GraphicCollection
77

88

9-
def get_nearest_graphics(
9+
def get_nearest_graphics_indices(
1010
pos: tuple[float, float] | tuple[float, float, float],
1111
graphics: Sequence[Graphic] | GraphicCollection,
12-
) -> np.ndarray[Graphic]:
12+
) -> np.ndarray[int]:
1313
"""
14-
Returns the nearest ``graphics`` to the passed position ``pos`` in world space.
15-
Uses the distance between ``pos`` and the center of the bounding sphere for each graphic.
14+
Returns indices of the nearest ``graphics`` to the passed position ``pos`` in world space
15+
in order of closest to furtherst. Uses the distance between ``pos`` and the center of the
16+
bounding sphere for each graphic.
1617
1718
Parameters
1819
----------
@@ -25,11 +26,10 @@ def get_nearest_graphics(
2526
2627
Returns
2728
-------
28-
tuple[Graphic]
29-
nearest graphics to ``pos`` in order
29+
ndarray[int]
30+
indices of the nearest nearest graphics to ``pos`` in order
3031
3132
"""
32-
3333
if isinstance(graphics, GraphicCollection):
3434
graphics = graphics.graphics
3535

@@ -50,4 +50,31 @@ def get_nearest_graphics(
5050
distances = np.linalg.norm(centers[:, : len(pos)] - pos, ord=2, axis=1)
5151

5252
sort_indices = np.argsort(distances)
53+
return sort_indices
54+
55+
56+
def get_nearest_graphics(
57+
pos: tuple[float, float] | tuple[float, float, float],
58+
graphics: Sequence[Graphic] | GraphicCollection,
59+
) -> np.ndarray[Graphic]:
60+
"""
61+
Returns the nearest ``graphics`` to the passed position ``pos`` in world space.
62+
Uses the distance between ``pos`` and the center of the bounding sphere for each graphic.
63+
64+
Parameters
65+
----------
66+
pos: (x, y) | (x, y, z)
67+
position in world space, z-axis is ignored when calculating L2 norms if ``pos`` is 2D
68+
69+
graphics: Sequence, i.e. array, list, tuple, etc. of Graphic | GraphicCollection
70+
the graphics from which to return a sorted array of graphics in order of closest
71+
to furthest graphic
72+
73+
Returns
74+
-------
75+
ndarray[Graphic]
76+
nearest graphics to ``pos`` in order
77+
78+
"""
79+
sort_indices = get_nearest_graphics_indices(pos, graphics)
5380
return np.asarray(graphics)[sort_indices]

0 commit comments

Comments
 (0)