Skip to content

Inconsistent path intersection #11527

Closed

Description

@firesharker

Bug report

Bug summary
If I two paths has a common segment, the result of a.intersects_path(b) is inconsistent. I think it should return True every time in this case.

Code for reproduction

from matplotlib.path import Path

a = Path([(0,1),(0,3)]) 
b = Path([(0,0),(0,4)])   # larger than a
c = Path([(0,0),(0,2)])   # ends in a
d = Path([(0,1),(0,2),(0,3)])   # same as a, but has an extra point in a
e = Path([(0,0),(0,2),(0,4)])   # same as b, but has an extra point in a
f = Path([(0,0),(0,1),(0,3),(0,4)])  # contains a as a segment
g = Path([(0,1),(0,3),(1,3)])   # a and a non-collinear point

paths = [a,b,c,d,e,f, g]
for i in paths:
    print([1 if p.intersects_path(i) else 0 for p in paths])

Actual outcome

[0, 0, 0, 0, 1, 1, 1]
[0, 0, 0, 0, 0, 0, 1]
[0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 1, 1, 1]
[1, 0, 0, 1, 0, 0, 1]
[1, 0, 0, 1, 0, 0, 1]
[1, 1, 0, 1, 1, 1, 1]

Expected outcome
True for all (since they all have the [(0,1),(0,2)] segment in common).

Matplotlib version

  • Operating system: Ubuntu 16.04
  • Matplotlib version: 2.2.2
  • Matplotlib backend (print(matplotlib.get_backend())): module://ipykernel.pylab.backend_inline
  • Python version: 3.6.5
  • Jupyter version (if applicable): 5.4.1

Activity

  1. tacaswell commented on Jun 28, 2018

    @tacaswell
    Member

    That is interesting.

    I am not sure what the convention for 'intersection' with co-linear lines is. If by "intersect" you mean "some points fall on one side and some points fall on the other" then there is a case that everything but 'g' should not be considered intersecting.

  2. added this to the v3.1 milestone on Jun 28, 2018
  3. tacaswell commented on Jun 28, 2018

    @tacaswell
    Member

    What does shapely do in these cases? We should probably do the same thing as I trust that there are experts in computational geometry working on that project.

  4. firesharker commented on Jun 28, 2018

    @firesharker
    Author

    I expect that if two paths have at least one common point, intersects_path() should return True. It looks like shapely thinks the same:

    from shapely.geometry import LineString
    
    a = LineString([(0,1),(0,3)]) 
    b = LineString([(0,0),(0,4)])   # larger than a
    c = LineString([(0,0),(0,2)])   # ends in a
    d = LineString([(0,1),(0,2),(0,3)])   # same as a, but has an extra point in a
    e = LineString([(0,0),(0,2),(0,4)])   # same as b, but has an extra point in a
    f = LineString([(0,0),(0,1),(0,3),(0,4)])  # contains a as a segment
    g = LineString([(0,1),(0,3),(1,3)])   # a and a not co-linear point
    
    paths = [a,b,c,d,e,f, g]
    for i in paths:
        print([1 if p.intersects(i) else 0 for p in paths])

    returns

    [1, 1, 1, 1, 1, 1, 1]
    [1, 1, 1, 1, 1, 1, 1]
    [1, 1, 1, 1, 1, 1, 1]
    [1, 1, 1, 1, 1, 1, 1]
    [1, 1, 1, 1, 1, 1, 1]
    [1, 1, 1, 1, 1, 1, 1]
    [1, 1, 1, 1, 1, 1, 1]
    

    I can accept that if by "intersect" we mean two lines crossing each other in exactly one point, but this is seriously wrong:

    a = Path([(0,1),(0,2),(0,3)])   # co-linear points
    b = Path([(0,1),(0,2),(1,3)])  # not co-linear points
    >>> a.intersects_path(a)
    False
    >>> b.intersects_path(b)
    True
  5. tacaswell commented on Jun 28, 2018

    @tacaswell
    Member

    Thanks for doing the legwork on getting those answers!

    I suspect the bug lies someplace in https://github.com/matplotlib/matplotlib/blob/master/src/_path_wrapper.cpp or https://github.com/matplotlib/matplotlib/blob/master/src/_path.h

    attn @mdboom @QuLogic

  6. TarasKuzyo commented on Jul 2, 2018

    @TarasKuzyo
    Contributor

    It looks the issue is caused by the early exit condition in segments_intersect
    https://github.com/matplotlib/matplotlib/blob/master/src/_path.h#L825

    double den = ((y4 - y3) * (x2 - x1)) - ((x4 - x3) * (y2 - y1));
        if (den == 0.0) {
            return false;
        }
    

    basically, it returns false if two segments have the same slopes, yet it doesn't take into account the case when segments overlap.

  7. modified the milestones: v3.1.0, v3.2.0 on Feb 25, 2019
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