Skip to content

Path.intersects_path sometimes returns incorrect values #20328

Closed

Description

@nwatters01

Bug report

Bug summary

The Path.intersects_path function sometimes returns True when it should return False. I've noticed this only when some vertices in the path deviate slightly from round numbers (as sometimes arises from floating point imprecision).

Code for reproduction

from matplotlib.path import Path

path_0 = Path([
    [1, -1],
    [0, -1],
    [0, -2],
    [1, -2],
])
path_1 = Path([
    [1, 2],
    [0, 2],
    [0, 1],
    [0.999999999999, 1],
])

print(Path.intersects_path(path_0, path_1))

Actual outcome

True

Expected outcome

False 

Note that the two paths are completely separated by a large margin along the y axis, so do not intersect.

Additional observations:

  • Choosing different y values for the paths or translating the entire configuration by choosing different x values does not change the result.
  • Reducing the precision of the last vertex of path_1 (e.g. making it 1 or 0.9999) changes the result to the expected False.

Matplotlib version

  • Operating system: OS X 10.15.1
  • Matplotlib version (import matplotlib; print(matplotlib.__version__)): 3.4.2 (bug exists with 3.1.0 as well)
  • Matplotlib backend (print(matplotlib.get_backend())): MacOSX
  • Python version: 3.7.10

Matplotlib installed using pip in a conda virtual environment (conda activate mpl_bug \ pip install matplotlib) with no other dependencies installed.

Activity

  1. jklymak commented on May 28, 2021

    @jklymak
    Member

    This looks like #15842 but that was supposedly fixed?

  2. added this to the v3.4.3 milestone on May 28, 2021
  3. nwatters01 commented on May 28, 2021

    @nwatters01
    Author

    Clarification: I was mistaken in thinking the bug had to do with the 0.999... vertex. On the contrary, the bug arises with completely round integers. For example, these two disjoint (and very far apart) horizontal segments:

    from matplotlib.path import Path
    
    path_0 = Path([
        [0, -5],
        [1, -5],
    ])
    path_1 = Path([
        [1, 5],
        [0, 5],
    ])
    
    print(Path.intersects_path(path_0, path_1))
    

    Returns True. This is a much better minimal example than the one I initially posted.

  4. jklymak commented on May 28, 2021

    @jklymak
    Member

    Ok and if you coerce to float it is fine?

  5. nwatters01 commented on May 28, 2021

    @nwatters01
    Author

    No, coercing to float is not fine either it seems.

    from matplotlib.path import Path
    import numpy as np
    
    path_0 = Path(np.array([
        [0., -5.],
        [1., -5.],
    ], dtype=np.float32))
    path_1 = Path(np.array([
        [1., 5.],
        [0., 5.],
    ], dtype=np.float32))
    
    print(Path.intersects_path(path_0, path_1))
    

    Also returns True (both with float32 and float64 types).

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