Skip to content

Path.intersects_path returns True when it shouldn't #15842

Closed

Description

@QuLogic

Bug report

Bug summary

Path.intersects_path returns True for the following paths, when they don't intersect at all (as shown by Shapely). This is a simplified outline_path from Cartopy. The trigger seems to be the duplicate point at the end of outline_path; if removed, it works as expected.

A bisect says this broke in bd3fd8f, which is a backport of #11553 to 3.1.0. cc @TarasKuzyo

Code for reproduction

import numpy as np
import matplotlib as mpl
from matplotlib.path import Path
from shapely.geometry import LinearRing


this_path = Path(
    np.array([
        [824.85064295, 2056.26489203],
        [861.69033931, 2041.00539016],
        [868.57864109, 2057.63522175],
        [831.73894473, 2072.89472361],
        [824.85064295, 2056.26489203]]),
    np.array([1, 2, 2, 2, 79], dtype=Path.code_type))
this_ring = LinearRing(this_path.vertices)

outline_path = Path(
    np.array([
        [859.91051028, 2165.38461538],
        [859.06772495, 2149.30331334],
        [859.06772495, 2181.46591743],
        [859.91051028, 2165.38461538],
        [859.91051028, 2165.38461538]]),
    np.array([1, 2, 2, 2, 2],
             dtype=Path.code_type))
outline_ring = LinearRing(outline_path.vertices)

print(mpl.__version__, outline_path.intersects_path(this_path),
      outline_ring.intersects(this_ring))

Actual outcome

3.1.2 True False

Expected outcome

3.1.2 False False

Matplotlib version

  • Operating system: Fedora 30
  • Matplotlib version: 3.1.2
  • Matplotlib backend (print(matplotlib.get_backend())): N/A
  • Python version: 3.7
  • Jupyter version (if applicable): N/A
  • Other libraries: N/A

Activity

  1. added this to the v3.1.3 milestone on Dec 6, 2019
  2. TarasKuzyo commented on Dec 6, 2019

    @TarasKuzyo
    Contributor

    @QuLogic

    it seems a zero-length segment intersects with any other segment.
    Will push a fix be Dec 10.

  3. TarasKuzyo commented on Dec 10, 2019

    @TarasKuzyo
    Contributor

    When checking if the two line segments intersect need to check if any of those are actually points.
    If that is the case, need to check if the line contains the point.

  4. tacaswell commented on Jan 17, 2020

    @tacaswell
    Member

    This is broken on 3.2 and master as well.

  5. added 3 commits that reference this issue on Jan 18, 2020
    ffa84e0
    ca98369
    df6316c
  6. Kojoley commented on Jan 20, 2020

    @Kojoley
    Member

    Probably the same cause:

    import matplotlib.pyplot as plt
    import matplotlib.patches as mpatches
    from matplotlib.patches import Circle
    
    
    def check_collision(a, b):
        a = a.get_path().transformed(a.get_patch_transform())
        b = b.get_path().transformed(b.get_patch_transform())
        return a.intersects_path(b)
    
    fig = plt.figure(figsize=(8, 8))
    ax = fig.add_subplot(111)
    
    fig.subplots_adjust(0, 0, 1, 1, 0, 0)
    ax.set_axis_off()
    
    #shape = mpatches.Rectangle((0.5, 0.5), 0.02, 0.02)
    shape = mpatches.Circle((0, 0), 0.02)
    ax.add_patch(shape)
    
    rect = mpatches.Rectangle((0.4, 0.4), 0.2, 0.2, linewidth=0.5, edgecolor='black', facecolor='cyan')
    ax.add_patch(rect)
    
    def on_mouse_move(event):
        if hasattr(shape, 'set_center'):
            shape.set_center((event.xdata, event.ydata))
        else:
            shape.set_xy((event.xdata, event.ydata))
        shape.set_facecolor('red' if check_collision(rect, shape) else 'gray')
        ax.draw_artist(shape)
        fig.canvas.blit(fig.bbox)
    
    fig.canvas.mpl_connect('motion_notify_event', on_mouse_move)
    
    plt.show()
  7. added 2 commits that reference this issue on Jan 23, 2020
    f8181c9
    b3476a8
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions