Skip to content

Conversation

@tikuma-lsuhsc
Copy link

PR summary

This PR addresses and closes Issue #30944 that I posted earlier. In short, returning more details from the Line2D.contains() hit test could eliminate additional computational needs during time-constrained mouse events. Specifically, the PR adds 'is_vert' boolean array item to the details output dict. This item has the same size as the existing 'ind' item and its element indicates whether the corresponding 'ind' element. Having 'is_vert' is handy when the UI requires a different action when user click on a vertex or on an edge. While this check can be done by user later, Line2D.contains() already performs this test internally (via lines.segment_hits() function). So, why not eliminate the redundant geometric computations.

This PR implements this change by minimally touching lines.Line2D.contains() and lines.segment_hits() functions without breaking their backward compatibility:

  • lines.segment_hits() returns the number of vertex hits as an additional int value. This value is only returned if the optional return_is_vert boolean argument is set to True.
  • Given this vertex hit count, lines.Line2D.contains() composes the is_vert array and adds it to its details return dict.

Usage example:

from matplotlib import pyplot as plt

fig, ax = plt.subplots()
line = ax.plot([0,1], [0,1], 'o-')[0]

def on_click(event):

    hit, details = line.contains(event)

    if hit:
        if details['is_vert'][0]:
            print('grab and move only the clicked vertex')
        else:
            print('move the line as a whole without changing shape')

plt.connect('button_press_event', on_click)

plt.show()

I have also modified the pick_event_demo.py in galleries/example/event_handling to demonstrate the new feature.

Documentation and docstrings have been updated to best of my ability. The docstring of lines.segment_hits() is not a full-fledged one. So, I just expanded it in the same style.

PR checklist

@github-actions github-actions bot added Documentation: examples files in galleries/examples Documentation: user guide files in galleries/users_explain or doc/users labels Jan 9, 2026
@tikuma-lsuhsc tikuma-lsuhsc force-pushed the enhance-Line2D-contains-output-issue-30944 branch from b3298d5 to 5d219d0 Compare January 9, 2026 13:49
@tikuma-lsuhsc tikuma-lsuhsc reopened this Jan 9, 2026


def segment_hits(cx, cy, x, y, radius):
def segment_hits(cx, cy, x, y, radius, return_is_vert=False):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not a good pattern to use in public API.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was afraid of that. Originally, I did not have this argument, but it would break the API (different returns) so I thought to add this argument to keep its function unchanged. Anyway, I'll remove it and make note of the API breakage in the "what changed" doc.

@tacaswell
Copy link
Member

I remain unclear what this adds over leaving the user the ability to ask "is this point with in my threshold of where I clickd?"

@tikuma-lsuhsc
Copy link
Author

I remain unclear what this adds over leaving the user the ability to ask "is this point with in my threshold of where I clickd?"

Hmm, Isn't the purpose of the contains() method exactly to answer this very question? My thought process here is that "we already compute these things, so let's save the users from computing them on their own."

- modified `lines.segment_hits()` to return additional output
- `lines.Line2D.contains()` to return a new `vertex_hit` item in `details` return dict
- updated tests and documentations
@tikuma-lsuhsc tikuma-lsuhsc force-pushed the enhance-Line2D-contains-output-issue-30944 branch from eeb3c73 to cbb94d2 Compare January 20, 2026 03:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Documentation: examples files in galleries/examples Documentation: user guide files in galleries/users_explain or doc/users

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[ENH]: Line2D.contains() to return additional details

2 participants