Skip to content

Commit

Permalink
allow None for any polygon side tangent
Browse files Browse the repository at this point in the history
  • Loading branch information
smichr committed Mar 19, 2024
1 parent 9d13061 commit d1de381
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
17 changes: 15 additions & 2 deletions sympy/geometry/ellipse.py
Original file line number Diff line number Diff line change
Expand Up @@ -694,7 +694,10 @@ def intersection(self, o):
raise TypeError('Intersection not handled for %s' % func_name(o))

def is_tangent(self, o):
"""Is `o` tangent to the ellipse?
"""Is `o` tangent to the ellipse? For a polygon return True if
all sides are tangent, None if any side is tangent, and False
if no side is tangent.
Parameters
==========
Expand Down Expand Up @@ -755,7 +758,17 @@ def is_tangent(self, o):
else:
return False
elif isinstance(o, Polygon):
return all(self.is_tangent(s) for s in o.sides)
hit = [0, 0]
for s in o.sides:
# is_tangent must raise if it can't decide;
# we are expecting True(1) or False(0) here
hit[self.is_tangent(s)] += 1
f, t = hit
if not f:
return True
if not t:
return False
return None
elif isinstance(o, (LinearEntity3D, Point3D)):
raise TypeError('Entity must be two dimensional, not three dimensional')
else:
Expand Down
10 changes: 5 additions & 5 deletions sympy/geometry/tests/test_ellipse.py
Original file line number Diff line number Diff line change
Expand Up @@ -471,19 +471,19 @@ def test_is_tangent():
assert e1.is_tangent(Polygon((-3, 0), (3, 0), (0, 5))) is False
assert e1.is_tangent(Polygon((-3, 0), (0, -5), (3, 0), (0, 5))) is False
assert e1.is_tangent(Polygon((-3, -5), (-3, 5), (3, 5), (3, -5))) is True
assert c1.is_tangent(Polygon((-3, -5), (-3, 5), (3, 5), (3, -5))) is False
assert c1.is_tangent(Polygon((-3, -5), (-3, 5), (3, 5), (3, -5))) is None
assert e1.is_tangent(Polygon((0, 0), (3, 0), (7, 7), (0, 5))) is False
assert e1.is_tangent(Polygon((3, 12), (3, -12), (6, 5))) is False
assert e1.is_tangent(Polygon((3, 12), (3, -12), (0, -5), (0, 5))) is False
assert e1.is_tangent(Polygon((3, 12), (3, -12), (6, 5))) is None
assert e1.is_tangent(Polygon((3, 12), (3, -12), (0, -5), (0, 5))) is None
assert e1.is_tangent(Polygon((3, 0), (5, 7), (6, -5))) is False
assert c1.is_tangent(Segment((0, 0), (-5, -2))) is False
assert e1.is_tangent(Segment((-3, 0), (3, 0))) is False
assert e1.is_tangent(Segment((-3, 5), (3, 5))) is True
assert e1.is_tangent(Polygon((0, 0), (5, 5), (5, -5))) is False
assert e1.is_tangent(Polygon((-100, -50), (-40, -334), (-70, -52))) is False
assert e1.is_tangent(Polygon((-3, -5), (-3, 5), (3, 5), (3, -5))) is True
assert c1.is_tangent(Polygon((-3, -5), (-3, 5), (3, 5), (3, -5))) is False
assert e1.is_tangent(Polygon((3, 12), (3, -12), (0, -5), (0, 5))) is False
assert c1.is_tangent(Polygon((-3, -5), (-3, 5), (3, 5), (3, -5))) is None
assert e1.is_tangent(Polygon((3, 12), (3, -12), (0, -5), (0, 5))) is None
assert e1.is_tangent(Polygon((3, 0), (5, 7), (6, -5))) is False
raises(TypeError, lambda: e1.is_tangent(Point(0, 0, 0)))
raises(TypeError, lambda: e1.is_tangent(Rational(5)))
Expand Down

0 comments on commit d1de381

Please sign in to comment.