Add files using upload-large-folder tool
Browse filesThis view is limited to 50 files because it contains too many changes.
See raw diff
- env-llmeval/lib/python3.10/site-packages/sympy/geometry/__init__.py +45 -0
- env-llmeval/lib/python3.10/site-packages/sympy/geometry/__pycache__/__init__.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/geometry/__pycache__/curve.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/geometry/__pycache__/ellipse.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/geometry/__pycache__/entity.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/geometry/__pycache__/exceptions.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/geometry/__pycache__/line.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/geometry/__pycache__/parabola.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/geometry/__pycache__/plane.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/geometry/__pycache__/point.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/geometry/__pycache__/polygon.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/geometry/__pycache__/util.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/geometry/ellipse.py +1780 -0
- env-llmeval/lib/python3.10/site-packages/sympy/geometry/entity.py +641 -0
- env-llmeval/lib/python3.10/site-packages/sympy/geometry/exceptions.py +5 -0
- env-llmeval/lib/python3.10/site-packages/sympy/geometry/polygon.py +2883 -0
- env-llmeval/lib/python3.10/site-packages/sympy/geometry/tests/__init__.py +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/geometry/tests/__pycache__/__init__.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/geometry/tests/__pycache__/test_curve.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/geometry/tests/__pycache__/test_ellipse.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/geometry/tests/__pycache__/test_entity.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/geometry/tests/__pycache__/test_geometrysets.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/geometry/tests/__pycache__/test_line.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/geometry/tests/__pycache__/test_parabola.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/geometry/tests/__pycache__/test_plane.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/geometry/tests/__pycache__/test_point.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/geometry/tests/__pycache__/test_polygon.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/geometry/tests/__pycache__/test_util.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/geometry/tests/test_curve.py +120 -0
- env-llmeval/lib/python3.10/site-packages/sympy/geometry/tests/test_ellipse.py +601 -0
- env-llmeval/lib/python3.10/site-packages/sympy/geometry/tests/test_entity.py +120 -0
- env-llmeval/lib/python3.10/site-packages/sympy/geometry/tests/test_geometrysets.py +38 -0
- env-llmeval/lib/python3.10/site-packages/sympy/geometry/tests/test_line.py +852 -0
- env-llmeval/lib/python3.10/site-packages/sympy/geometry/tests/test_parabola.py +143 -0
- env-llmeval/lib/python3.10/site-packages/sympy/geometry/tests/test_plane.py +268 -0
- env-llmeval/lib/python3.10/site-packages/sympy/geometry/tests/test_point.py +481 -0
- env-llmeval/lib/python3.10/site-packages/sympy/geometry/tests/test_polygon.py +664 -0
- env-llmeval/lib/python3.10/site-packages/sympy/geometry/tests/test_util.py +151 -0
- env-llmeval/lib/python3.10/site-packages/sympy/geometry/util.py +718 -0
- env-llmeval/lib/python3.10/site-packages/sympy/liealgebras/cartan_matrix.py +25 -0
- env-llmeval/lib/python3.10/site-packages/sympy/liealgebras/root_system.py +199 -0
- env-llmeval/lib/python3.10/site-packages/sympy/multipledispatch/__init__.py +11 -0
- env-llmeval/lib/python3.10/site-packages/sympy/multipledispatch/conflict.py +68 -0
- env-llmeval/lib/python3.10/site-packages/sympy/multipledispatch/core.py +83 -0
- env-llmeval/lib/python3.10/site-packages/sympy/multipledispatch/dispatcher.py +413 -0
- env-llmeval/lib/python3.10/site-packages/sympy/multipledispatch/tests/__init__.py +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/multipledispatch/tests/__pycache__/__init__.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/multipledispatch/tests/__pycache__/test_core.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/multipledispatch/tests/__pycache__/test_dispatcher.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/multipledispatch/tests/test_conflict.py +62 -0
env-llmeval/lib/python3.10/site-packages/sympy/geometry/__init__.py
ADDED
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""
|
2 |
+
A geometry module for the SymPy library. This module contains all of the
|
3 |
+
entities and functions needed to construct basic geometrical data and to
|
4 |
+
perform simple informational queries.
|
5 |
+
|
6 |
+
Usage:
|
7 |
+
======
|
8 |
+
|
9 |
+
Examples
|
10 |
+
========
|
11 |
+
|
12 |
+
"""
|
13 |
+
from sympy.geometry.point import Point, Point2D, Point3D
|
14 |
+
from sympy.geometry.line import Line, Ray, Segment, Line2D, Segment2D, Ray2D, \
|
15 |
+
Line3D, Segment3D, Ray3D
|
16 |
+
from sympy.geometry.plane import Plane
|
17 |
+
from sympy.geometry.ellipse import Ellipse, Circle
|
18 |
+
from sympy.geometry.polygon import Polygon, RegularPolygon, Triangle, rad, deg
|
19 |
+
from sympy.geometry.util import are_similar, centroid, convex_hull, idiff, \
|
20 |
+
intersection, closest_points, farthest_points
|
21 |
+
from sympy.geometry.exceptions import GeometryError
|
22 |
+
from sympy.geometry.curve import Curve
|
23 |
+
from sympy.geometry.parabola import Parabola
|
24 |
+
|
25 |
+
__all__ = [
|
26 |
+
'Point', 'Point2D', 'Point3D',
|
27 |
+
|
28 |
+
'Line', 'Ray', 'Segment', 'Line2D', 'Segment2D', 'Ray2D', 'Line3D',
|
29 |
+
'Segment3D', 'Ray3D',
|
30 |
+
|
31 |
+
'Plane',
|
32 |
+
|
33 |
+
'Ellipse', 'Circle',
|
34 |
+
|
35 |
+
'Polygon', 'RegularPolygon', 'Triangle', 'rad', 'deg',
|
36 |
+
|
37 |
+
'are_similar', 'centroid', 'convex_hull', 'idiff', 'intersection',
|
38 |
+
'closest_points', 'farthest_points',
|
39 |
+
|
40 |
+
'GeometryError',
|
41 |
+
|
42 |
+
'Curve',
|
43 |
+
|
44 |
+
'Parabola',
|
45 |
+
]
|
env-llmeval/lib/python3.10/site-packages/sympy/geometry/__pycache__/__init__.cpython-310.pyc
ADDED
Binary file (1.46 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/sympy/geometry/__pycache__/curve.cpython-310.pyc
ADDED
Binary file (11.8 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/sympy/geometry/__pycache__/ellipse.cpython-310.pyc
ADDED
Binary file (48.4 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/sympy/geometry/__pycache__/entity.cpython-310.pyc
ADDED
Binary file (21.2 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/sympy/geometry/__pycache__/exceptions.cpython-310.pyc
ADDED
Binary file (452 Bytes). View file
|
|
env-llmeval/lib/python3.10/site-packages/sympy/geometry/__pycache__/line.cpython-310.pyc
ADDED
Binary file (70.2 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/sympy/geometry/__pycache__/parabola.cpython-310.pyc
ADDED
Binary file (11.1 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/sympy/geometry/__pycache__/plane.cpython-310.pyc
ADDED
Binary file (25.8 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/sympy/geometry/__pycache__/point.cpython-310.pyc
ADDED
Binary file (39.3 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/sympy/geometry/__pycache__/polygon.cpython-310.pyc
ADDED
Binary file (78 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/sympy/geometry/__pycache__/util.cpython-310.pyc
ADDED
Binary file (20.2 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/sympy/geometry/ellipse.py
ADDED
@@ -0,0 +1,1780 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""Elliptical geometrical entities.
|
2 |
+
|
3 |
+
Contains
|
4 |
+
* Ellipse
|
5 |
+
* Circle
|
6 |
+
|
7 |
+
"""
|
8 |
+
|
9 |
+
from sympy.core.expr import Expr
|
10 |
+
from sympy.core.relational import Eq
|
11 |
+
from sympy.core import S, pi, sympify
|
12 |
+
from sympy.core.evalf import N
|
13 |
+
from sympy.core.parameters import global_parameters
|
14 |
+
from sympy.core.logic import fuzzy_bool
|
15 |
+
from sympy.core.numbers import Rational, oo
|
16 |
+
from sympy.core.sorting import ordered
|
17 |
+
from sympy.core.symbol import Dummy, uniquely_named_symbol, _symbol
|
18 |
+
from sympy.simplify import simplify, trigsimp
|
19 |
+
from sympy.functions.elementary.miscellaneous import sqrt, Max
|
20 |
+
from sympy.functions.elementary.trigonometric import cos, sin
|
21 |
+
from sympy.functions.special.elliptic_integrals import elliptic_e
|
22 |
+
from .entity import GeometryEntity, GeometrySet
|
23 |
+
from .exceptions import GeometryError
|
24 |
+
from .line import Line, Segment, Ray2D, Segment2D, Line2D, LinearEntity3D
|
25 |
+
from .point import Point, Point2D, Point3D
|
26 |
+
from .util import idiff, find
|
27 |
+
from sympy.polys import DomainError, Poly, PolynomialError
|
28 |
+
from sympy.polys.polyutils import _not_a_coeff, _nsort
|
29 |
+
from sympy.solvers import solve
|
30 |
+
from sympy.solvers.solveset import linear_coeffs
|
31 |
+
from sympy.utilities.misc import filldedent, func_name
|
32 |
+
|
33 |
+
from mpmath.libmp.libmpf import prec_to_dps
|
34 |
+
|
35 |
+
import random
|
36 |
+
|
37 |
+
x, y = [Dummy('ellipse_dummy', real=True) for i in range(2)]
|
38 |
+
|
39 |
+
|
40 |
+
class Ellipse(GeometrySet):
|
41 |
+
"""An elliptical GeometryEntity.
|
42 |
+
|
43 |
+
Parameters
|
44 |
+
==========
|
45 |
+
|
46 |
+
center : Point, optional
|
47 |
+
Default value is Point(0, 0)
|
48 |
+
hradius : number or SymPy expression, optional
|
49 |
+
vradius : number or SymPy expression, optional
|
50 |
+
eccentricity : number or SymPy expression, optional
|
51 |
+
Two of `hradius`, `vradius` and `eccentricity` must be supplied to
|
52 |
+
create an Ellipse. The third is derived from the two supplied.
|
53 |
+
|
54 |
+
Attributes
|
55 |
+
==========
|
56 |
+
|
57 |
+
center
|
58 |
+
hradius
|
59 |
+
vradius
|
60 |
+
area
|
61 |
+
circumference
|
62 |
+
eccentricity
|
63 |
+
periapsis
|
64 |
+
apoapsis
|
65 |
+
focus_distance
|
66 |
+
foci
|
67 |
+
|
68 |
+
Raises
|
69 |
+
======
|
70 |
+
|
71 |
+
GeometryError
|
72 |
+
When `hradius`, `vradius` and `eccentricity` are incorrectly supplied
|
73 |
+
as parameters.
|
74 |
+
TypeError
|
75 |
+
When `center` is not a Point.
|
76 |
+
|
77 |
+
See Also
|
78 |
+
========
|
79 |
+
|
80 |
+
Circle
|
81 |
+
|
82 |
+
Notes
|
83 |
+
-----
|
84 |
+
Constructed from a center and two radii, the first being the horizontal
|
85 |
+
radius (along the x-axis) and the second being the vertical radius (along
|
86 |
+
the y-axis).
|
87 |
+
|
88 |
+
When symbolic value for hradius and vradius are used, any calculation that
|
89 |
+
refers to the foci or the major or minor axis will assume that the ellipse
|
90 |
+
has its major radius on the x-axis. If this is not true then a manual
|
91 |
+
rotation is necessary.
|
92 |
+
|
93 |
+
Examples
|
94 |
+
========
|
95 |
+
|
96 |
+
>>> from sympy import Ellipse, Point, Rational
|
97 |
+
>>> e1 = Ellipse(Point(0, 0), 5, 1)
|
98 |
+
>>> e1.hradius, e1.vradius
|
99 |
+
(5, 1)
|
100 |
+
>>> e2 = Ellipse(Point(3, 1), hradius=3, eccentricity=Rational(4, 5))
|
101 |
+
>>> e2
|
102 |
+
Ellipse(Point2D(3, 1), 3, 9/5)
|
103 |
+
|
104 |
+
"""
|
105 |
+
|
106 |
+
def __contains__(self, o):
|
107 |
+
if isinstance(o, Point):
|
108 |
+
res = self.equation(x, y).subs({x: o.x, y: o.y})
|
109 |
+
return trigsimp(simplify(res)) is S.Zero
|
110 |
+
elif isinstance(o, Ellipse):
|
111 |
+
return self == o
|
112 |
+
return False
|
113 |
+
|
114 |
+
def __eq__(self, o):
|
115 |
+
"""Is the other GeometryEntity the same as this ellipse?"""
|
116 |
+
return isinstance(o, Ellipse) and (self.center == o.center and
|
117 |
+
self.hradius == o.hradius and
|
118 |
+
self.vradius == o.vradius)
|
119 |
+
|
120 |
+
def __hash__(self):
|
121 |
+
return super().__hash__()
|
122 |
+
|
123 |
+
def __new__(
|
124 |
+
cls, center=None, hradius=None, vradius=None, eccentricity=None, **kwargs):
|
125 |
+
|
126 |
+
hradius = sympify(hradius)
|
127 |
+
vradius = sympify(vradius)
|
128 |
+
|
129 |
+
if center is None:
|
130 |
+
center = Point(0, 0)
|
131 |
+
else:
|
132 |
+
if len(center) != 2:
|
133 |
+
raise ValueError('The center of "{}" must be a two dimensional point'.format(cls))
|
134 |
+
center = Point(center, dim=2)
|
135 |
+
|
136 |
+
if len(list(filter(lambda x: x is not None, (hradius, vradius, eccentricity)))) != 2:
|
137 |
+
raise ValueError(filldedent('''
|
138 |
+
Exactly two arguments of "hradius", "vradius", and
|
139 |
+
"eccentricity" must not be None.'''))
|
140 |
+
|
141 |
+
if eccentricity is not None:
|
142 |
+
eccentricity = sympify(eccentricity)
|
143 |
+
if eccentricity.is_negative:
|
144 |
+
raise GeometryError("Eccentricity of ellipse/circle should lie between [0, 1)")
|
145 |
+
elif hradius is None:
|
146 |
+
hradius = vradius / sqrt(1 - eccentricity**2)
|
147 |
+
elif vradius is None:
|
148 |
+
vradius = hradius * sqrt(1 - eccentricity**2)
|
149 |
+
|
150 |
+
if hradius == vradius:
|
151 |
+
return Circle(center, hradius, **kwargs)
|
152 |
+
|
153 |
+
if S.Zero in (hradius, vradius):
|
154 |
+
return Segment(Point(center[0] - hradius, center[1] - vradius), Point(center[0] + hradius, center[1] + vradius))
|
155 |
+
|
156 |
+
if hradius.is_real is False or vradius.is_real is False:
|
157 |
+
raise GeometryError("Invalid value encountered when computing hradius / vradius.")
|
158 |
+
|
159 |
+
return GeometryEntity.__new__(cls, center, hradius, vradius, **kwargs)
|
160 |
+
|
161 |
+
def _svg(self, scale_factor=1., fill_color="#66cc99"):
|
162 |
+
"""Returns SVG ellipse element for the Ellipse.
|
163 |
+
|
164 |
+
Parameters
|
165 |
+
==========
|
166 |
+
|
167 |
+
scale_factor : float
|
168 |
+
Multiplication factor for the SVG stroke-width. Default is 1.
|
169 |
+
fill_color : str, optional
|
170 |
+
Hex string for fill color. Default is "#66cc99".
|
171 |
+
"""
|
172 |
+
|
173 |
+
c = N(self.center)
|
174 |
+
h, v = N(self.hradius), N(self.vradius)
|
175 |
+
return (
|
176 |
+
'<ellipse fill="{1}" stroke="#555555" '
|
177 |
+
'stroke-width="{0}" opacity="0.6" cx="{2}" cy="{3}" rx="{4}" ry="{5}"/>'
|
178 |
+
).format(2. * scale_factor, fill_color, c.x, c.y, h, v)
|
179 |
+
|
180 |
+
@property
|
181 |
+
def ambient_dimension(self):
|
182 |
+
return 2
|
183 |
+
|
184 |
+
@property
|
185 |
+
def apoapsis(self):
|
186 |
+
"""The apoapsis of the ellipse.
|
187 |
+
|
188 |
+
The greatest distance between the focus and the contour.
|
189 |
+
|
190 |
+
Returns
|
191 |
+
=======
|
192 |
+
|
193 |
+
apoapsis : number
|
194 |
+
|
195 |
+
See Also
|
196 |
+
========
|
197 |
+
|
198 |
+
periapsis : Returns shortest distance between foci and contour
|
199 |
+
|
200 |
+
Examples
|
201 |
+
========
|
202 |
+
|
203 |
+
>>> from sympy import Point, Ellipse
|
204 |
+
>>> p1 = Point(0, 0)
|
205 |
+
>>> e1 = Ellipse(p1, 3, 1)
|
206 |
+
>>> e1.apoapsis
|
207 |
+
2*sqrt(2) + 3
|
208 |
+
|
209 |
+
"""
|
210 |
+
return self.major * (1 + self.eccentricity)
|
211 |
+
|
212 |
+
def arbitrary_point(self, parameter='t'):
|
213 |
+
"""A parameterized point on the ellipse.
|
214 |
+
|
215 |
+
Parameters
|
216 |
+
==========
|
217 |
+
|
218 |
+
parameter : str, optional
|
219 |
+
Default value is 't'.
|
220 |
+
|
221 |
+
Returns
|
222 |
+
=======
|
223 |
+
|
224 |
+
arbitrary_point : Point
|
225 |
+
|
226 |
+
Raises
|
227 |
+
======
|
228 |
+
|
229 |
+
ValueError
|
230 |
+
When `parameter` already appears in the functions.
|
231 |
+
|
232 |
+
See Also
|
233 |
+
========
|
234 |
+
|
235 |
+
sympy.geometry.point.Point
|
236 |
+
|
237 |
+
Examples
|
238 |
+
========
|
239 |
+
|
240 |
+
>>> from sympy import Point, Ellipse
|
241 |
+
>>> e1 = Ellipse(Point(0, 0), 3, 2)
|
242 |
+
>>> e1.arbitrary_point()
|
243 |
+
Point2D(3*cos(t), 2*sin(t))
|
244 |
+
|
245 |
+
"""
|
246 |
+
t = _symbol(parameter, real=True)
|
247 |
+
if t.name in (f.name for f in self.free_symbols):
|
248 |
+
raise ValueError(filldedent('Symbol %s already appears in object '
|
249 |
+
'and cannot be used as a parameter.' % t.name))
|
250 |
+
return Point(self.center.x + self.hradius*cos(t),
|
251 |
+
self.center.y + self.vradius*sin(t))
|
252 |
+
|
253 |
+
@property
|
254 |
+
def area(self):
|
255 |
+
"""The area of the ellipse.
|
256 |
+
|
257 |
+
Returns
|
258 |
+
=======
|
259 |
+
|
260 |
+
area : number
|
261 |
+
|
262 |
+
Examples
|
263 |
+
========
|
264 |
+
|
265 |
+
>>> from sympy import Point, Ellipse
|
266 |
+
>>> p1 = Point(0, 0)
|
267 |
+
>>> e1 = Ellipse(p1, 3, 1)
|
268 |
+
>>> e1.area
|
269 |
+
3*pi
|
270 |
+
|
271 |
+
"""
|
272 |
+
return simplify(S.Pi * self.hradius * self.vradius)
|
273 |
+
|
274 |
+
@property
|
275 |
+
def bounds(self):
|
276 |
+
"""Return a tuple (xmin, ymin, xmax, ymax) representing the bounding
|
277 |
+
rectangle for the geometric figure.
|
278 |
+
|
279 |
+
"""
|
280 |
+
|
281 |
+
h, v = self.hradius, self.vradius
|
282 |
+
return (self.center.x - h, self.center.y - v, self.center.x + h, self.center.y + v)
|
283 |
+
|
284 |
+
@property
|
285 |
+
def center(self):
|
286 |
+
"""The center of the ellipse.
|
287 |
+
|
288 |
+
Returns
|
289 |
+
=======
|
290 |
+
|
291 |
+
center : number
|
292 |
+
|
293 |
+
See Also
|
294 |
+
========
|
295 |
+
|
296 |
+
sympy.geometry.point.Point
|
297 |
+
|
298 |
+
Examples
|
299 |
+
========
|
300 |
+
|
301 |
+
>>> from sympy import Point, Ellipse
|
302 |
+
>>> p1 = Point(0, 0)
|
303 |
+
>>> e1 = Ellipse(p1, 3, 1)
|
304 |
+
>>> e1.center
|
305 |
+
Point2D(0, 0)
|
306 |
+
|
307 |
+
"""
|
308 |
+
return self.args[0]
|
309 |
+
|
310 |
+
@property
|
311 |
+
def circumference(self):
|
312 |
+
"""The circumference of the ellipse.
|
313 |
+
|
314 |
+
Examples
|
315 |
+
========
|
316 |
+
|
317 |
+
>>> from sympy import Point, Ellipse
|
318 |
+
>>> p1 = Point(0, 0)
|
319 |
+
>>> e1 = Ellipse(p1, 3, 1)
|
320 |
+
>>> e1.circumference
|
321 |
+
12*elliptic_e(8/9)
|
322 |
+
|
323 |
+
"""
|
324 |
+
if self.eccentricity == 1:
|
325 |
+
# degenerate
|
326 |
+
return 4*self.major
|
327 |
+
elif self.eccentricity == 0:
|
328 |
+
# circle
|
329 |
+
return 2*pi*self.hradius
|
330 |
+
else:
|
331 |
+
return 4*self.major*elliptic_e(self.eccentricity**2)
|
332 |
+
|
333 |
+
@property
|
334 |
+
def eccentricity(self):
|
335 |
+
"""The eccentricity of the ellipse.
|
336 |
+
|
337 |
+
Returns
|
338 |
+
=======
|
339 |
+
|
340 |
+
eccentricity : number
|
341 |
+
|
342 |
+
Examples
|
343 |
+
========
|
344 |
+
|
345 |
+
>>> from sympy import Point, Ellipse, sqrt
|
346 |
+
>>> p1 = Point(0, 0)
|
347 |
+
>>> e1 = Ellipse(p1, 3, sqrt(2))
|
348 |
+
>>> e1.eccentricity
|
349 |
+
sqrt(7)/3
|
350 |
+
|
351 |
+
"""
|
352 |
+
return self.focus_distance / self.major
|
353 |
+
|
354 |
+
def encloses_point(self, p):
|
355 |
+
"""
|
356 |
+
Return True if p is enclosed by (is inside of) self.
|
357 |
+
|
358 |
+
Notes
|
359 |
+
-----
|
360 |
+
Being on the border of self is considered False.
|
361 |
+
|
362 |
+
Parameters
|
363 |
+
==========
|
364 |
+
|
365 |
+
p : Point
|
366 |
+
|
367 |
+
Returns
|
368 |
+
=======
|
369 |
+
|
370 |
+
encloses_point : True, False or None
|
371 |
+
|
372 |
+
See Also
|
373 |
+
========
|
374 |
+
|
375 |
+
sympy.geometry.point.Point
|
376 |
+
|
377 |
+
Examples
|
378 |
+
========
|
379 |
+
|
380 |
+
>>> from sympy import Ellipse, S
|
381 |
+
>>> from sympy.abc import t
|
382 |
+
>>> e = Ellipse((0, 0), 3, 2)
|
383 |
+
>>> e.encloses_point((0, 0))
|
384 |
+
True
|
385 |
+
>>> e.encloses_point(e.arbitrary_point(t).subs(t, S.Half))
|
386 |
+
False
|
387 |
+
>>> e.encloses_point((4, 0))
|
388 |
+
False
|
389 |
+
|
390 |
+
"""
|
391 |
+
p = Point(p, dim=2)
|
392 |
+
if p in self:
|
393 |
+
return False
|
394 |
+
|
395 |
+
if len(self.foci) == 2:
|
396 |
+
# if the combined distance from the foci to p (h1 + h2) is less
|
397 |
+
# than the combined distance from the foci to the minor axis
|
398 |
+
# (which is the same as the major axis length) then p is inside
|
399 |
+
# the ellipse
|
400 |
+
h1, h2 = [f.distance(p) for f in self.foci]
|
401 |
+
test = 2*self.major - (h1 + h2)
|
402 |
+
else:
|
403 |
+
test = self.radius - self.center.distance(p)
|
404 |
+
|
405 |
+
return fuzzy_bool(test.is_positive)
|
406 |
+
|
407 |
+
def equation(self, x='x', y='y', _slope=None):
|
408 |
+
"""
|
409 |
+
Returns the equation of an ellipse aligned with the x and y axes;
|
410 |
+
when slope is given, the equation returned corresponds to an ellipse
|
411 |
+
with a major axis having that slope.
|
412 |
+
|
413 |
+
Parameters
|
414 |
+
==========
|
415 |
+
|
416 |
+
x : str, optional
|
417 |
+
Label for the x-axis. Default value is 'x'.
|
418 |
+
y : str, optional
|
419 |
+
Label for the y-axis. Default value is 'y'.
|
420 |
+
_slope : Expr, optional
|
421 |
+
The slope of the major axis. Ignored when 'None'.
|
422 |
+
|
423 |
+
Returns
|
424 |
+
=======
|
425 |
+
|
426 |
+
equation : SymPy expression
|
427 |
+
|
428 |
+
See Also
|
429 |
+
========
|
430 |
+
|
431 |
+
arbitrary_point : Returns parameterized point on ellipse
|
432 |
+
|
433 |
+
Examples
|
434 |
+
========
|
435 |
+
|
436 |
+
>>> from sympy import Point, Ellipse, pi
|
437 |
+
>>> from sympy.abc import x, y
|
438 |
+
>>> e1 = Ellipse(Point(1, 0), 3, 2)
|
439 |
+
>>> eq1 = e1.equation(x, y); eq1
|
440 |
+
y**2/4 + (x/3 - 1/3)**2 - 1
|
441 |
+
>>> eq2 = e1.equation(x, y, _slope=1); eq2
|
442 |
+
(-x + y + 1)**2/8 + (x + y - 1)**2/18 - 1
|
443 |
+
|
444 |
+
A point on e1 satisfies eq1. Let's use one on the x-axis:
|
445 |
+
|
446 |
+
>>> p1 = e1.center + Point(e1.major, 0)
|
447 |
+
>>> assert eq1.subs(x, p1.x).subs(y, p1.y) == 0
|
448 |
+
|
449 |
+
When rotated the same as the rotated ellipse, about the center
|
450 |
+
point of the ellipse, it will satisfy the rotated ellipse's
|
451 |
+
equation, too:
|
452 |
+
|
453 |
+
>>> r1 = p1.rotate(pi/4, e1.center)
|
454 |
+
>>> assert eq2.subs(x, r1.x).subs(y, r1.y) == 0
|
455 |
+
|
456 |
+
References
|
457 |
+
==========
|
458 |
+
|
459 |
+
.. [1] https://math.stackexchange.com/questions/108270/what-is-the-equation-of-an-ellipse-that-is-not-aligned-with-the-axis
|
460 |
+
.. [2] https://en.wikipedia.org/wiki/Ellipse#Shifted_ellipse
|
461 |
+
|
462 |
+
"""
|
463 |
+
|
464 |
+
x = _symbol(x, real=True)
|
465 |
+
y = _symbol(y, real=True)
|
466 |
+
|
467 |
+
dx = x - self.center.x
|
468 |
+
dy = y - self.center.y
|
469 |
+
|
470 |
+
if _slope is not None:
|
471 |
+
L = (dy - _slope*dx)**2
|
472 |
+
l = (_slope*dy + dx)**2
|
473 |
+
h = 1 + _slope**2
|
474 |
+
b = h*self.major**2
|
475 |
+
a = h*self.minor**2
|
476 |
+
return l/b + L/a - 1
|
477 |
+
|
478 |
+
else:
|
479 |
+
t1 = (dx/self.hradius)**2
|
480 |
+
t2 = (dy/self.vradius)**2
|
481 |
+
return t1 + t2 - 1
|
482 |
+
|
483 |
+
def evolute(self, x='x', y='y'):
|
484 |
+
"""The equation of evolute of the ellipse.
|
485 |
+
|
486 |
+
Parameters
|
487 |
+
==========
|
488 |
+
|
489 |
+
x : str, optional
|
490 |
+
Label for the x-axis. Default value is 'x'.
|
491 |
+
y : str, optional
|
492 |
+
Label for the y-axis. Default value is 'y'.
|
493 |
+
|
494 |
+
Returns
|
495 |
+
=======
|
496 |
+
|
497 |
+
equation : SymPy expression
|
498 |
+
|
499 |
+
Examples
|
500 |
+
========
|
501 |
+
|
502 |
+
>>> from sympy import Point, Ellipse
|
503 |
+
>>> e1 = Ellipse(Point(1, 0), 3, 2)
|
504 |
+
>>> e1.evolute()
|
505 |
+
2**(2/3)*y**(2/3) + (3*x - 3)**(2/3) - 5**(2/3)
|
506 |
+
"""
|
507 |
+
if len(self.args) != 3:
|
508 |
+
raise NotImplementedError('Evolute of arbitrary Ellipse is not supported.')
|
509 |
+
x = _symbol(x, real=True)
|
510 |
+
y = _symbol(y, real=True)
|
511 |
+
t1 = (self.hradius*(x - self.center.x))**Rational(2, 3)
|
512 |
+
t2 = (self.vradius*(y - self.center.y))**Rational(2, 3)
|
513 |
+
return t1 + t2 - (self.hradius**2 - self.vradius**2)**Rational(2, 3)
|
514 |
+
|
515 |
+
@property
|
516 |
+
def foci(self):
|
517 |
+
"""The foci of the ellipse.
|
518 |
+
|
519 |
+
Notes
|
520 |
+
-----
|
521 |
+
The foci can only be calculated if the major/minor axes are known.
|
522 |
+
|
523 |
+
Raises
|
524 |
+
======
|
525 |
+
|
526 |
+
ValueError
|
527 |
+
When the major and minor axis cannot be determined.
|
528 |
+
|
529 |
+
See Also
|
530 |
+
========
|
531 |
+
|
532 |
+
sympy.geometry.point.Point
|
533 |
+
focus_distance : Returns the distance between focus and center
|
534 |
+
|
535 |
+
Examples
|
536 |
+
========
|
537 |
+
|
538 |
+
>>> from sympy import Point, Ellipse
|
539 |
+
>>> p1 = Point(0, 0)
|
540 |
+
>>> e1 = Ellipse(p1, 3, 1)
|
541 |
+
>>> e1.foci
|
542 |
+
(Point2D(-2*sqrt(2), 0), Point2D(2*sqrt(2), 0))
|
543 |
+
|
544 |
+
"""
|
545 |
+
c = self.center
|
546 |
+
hr, vr = self.hradius, self.vradius
|
547 |
+
if hr == vr:
|
548 |
+
return (c, c)
|
549 |
+
|
550 |
+
# calculate focus distance manually, since focus_distance calls this
|
551 |
+
# routine
|
552 |
+
fd = sqrt(self.major**2 - self.minor**2)
|
553 |
+
if hr == self.minor:
|
554 |
+
# foci on the y-axis
|
555 |
+
return (c + Point(0, -fd), c + Point(0, fd))
|
556 |
+
elif hr == self.major:
|
557 |
+
# foci on the x-axis
|
558 |
+
return (c + Point(-fd, 0), c + Point(fd, 0))
|
559 |
+
|
560 |
+
@property
|
561 |
+
def focus_distance(self):
|
562 |
+
"""The focal distance of the ellipse.
|
563 |
+
|
564 |
+
The distance between the center and one focus.
|
565 |
+
|
566 |
+
Returns
|
567 |
+
=======
|
568 |
+
|
569 |
+
focus_distance : number
|
570 |
+
|
571 |
+
See Also
|
572 |
+
========
|
573 |
+
|
574 |
+
foci
|
575 |
+
|
576 |
+
Examples
|
577 |
+
========
|
578 |
+
|
579 |
+
>>> from sympy import Point, Ellipse
|
580 |
+
>>> p1 = Point(0, 0)
|
581 |
+
>>> e1 = Ellipse(p1, 3, 1)
|
582 |
+
>>> e1.focus_distance
|
583 |
+
2*sqrt(2)
|
584 |
+
|
585 |
+
"""
|
586 |
+
return Point.distance(self.center, self.foci[0])
|
587 |
+
|
588 |
+
@property
|
589 |
+
def hradius(self):
|
590 |
+
"""The horizontal radius of the ellipse.
|
591 |
+
|
592 |
+
Returns
|
593 |
+
=======
|
594 |
+
|
595 |
+
hradius : number
|
596 |
+
|
597 |
+
See Also
|
598 |
+
========
|
599 |
+
|
600 |
+
vradius, major, minor
|
601 |
+
|
602 |
+
Examples
|
603 |
+
========
|
604 |
+
|
605 |
+
>>> from sympy import Point, Ellipse
|
606 |
+
>>> p1 = Point(0, 0)
|
607 |
+
>>> e1 = Ellipse(p1, 3, 1)
|
608 |
+
>>> e1.hradius
|
609 |
+
3
|
610 |
+
|
611 |
+
"""
|
612 |
+
return self.args[1]
|
613 |
+
|
614 |
+
def intersection(self, o):
|
615 |
+
"""The intersection of this ellipse and another geometrical entity
|
616 |
+
`o`.
|
617 |
+
|
618 |
+
Parameters
|
619 |
+
==========
|
620 |
+
|
621 |
+
o : GeometryEntity
|
622 |
+
|
623 |
+
Returns
|
624 |
+
=======
|
625 |
+
|
626 |
+
intersection : list of GeometryEntity objects
|
627 |
+
|
628 |
+
Notes
|
629 |
+
-----
|
630 |
+
Currently supports intersections with Point, Line, Segment, Ray,
|
631 |
+
Circle and Ellipse types.
|
632 |
+
|
633 |
+
See Also
|
634 |
+
========
|
635 |
+
|
636 |
+
sympy.geometry.entity.GeometryEntity
|
637 |
+
|
638 |
+
Examples
|
639 |
+
========
|
640 |
+
|
641 |
+
>>> from sympy import Ellipse, Point, Line
|
642 |
+
>>> e = Ellipse(Point(0, 0), 5, 7)
|
643 |
+
>>> e.intersection(Point(0, 0))
|
644 |
+
[]
|
645 |
+
>>> e.intersection(Point(5, 0))
|
646 |
+
[Point2D(5, 0)]
|
647 |
+
>>> e.intersection(Line(Point(0,0), Point(0, 1)))
|
648 |
+
[Point2D(0, -7), Point2D(0, 7)]
|
649 |
+
>>> e.intersection(Line(Point(5,0), Point(5, 1)))
|
650 |
+
[Point2D(5, 0)]
|
651 |
+
>>> e.intersection(Line(Point(6,0), Point(6, 1)))
|
652 |
+
[]
|
653 |
+
>>> e = Ellipse(Point(-1, 0), 4, 3)
|
654 |
+
>>> e.intersection(Ellipse(Point(1, 0), 4, 3))
|
655 |
+
[Point2D(0, -3*sqrt(15)/4), Point2D(0, 3*sqrt(15)/4)]
|
656 |
+
>>> e.intersection(Ellipse(Point(5, 0), 4, 3))
|
657 |
+
[Point2D(2, -3*sqrt(7)/4), Point2D(2, 3*sqrt(7)/4)]
|
658 |
+
>>> e.intersection(Ellipse(Point(100500, 0), 4, 3))
|
659 |
+
[]
|
660 |
+
>>> e.intersection(Ellipse(Point(0, 0), 3, 4))
|
661 |
+
[Point2D(3, 0), Point2D(-363/175, -48*sqrt(111)/175), Point2D(-363/175, 48*sqrt(111)/175)]
|
662 |
+
>>> e.intersection(Ellipse(Point(-1, 0), 3, 4))
|
663 |
+
[Point2D(-17/5, -12/5), Point2D(-17/5, 12/5), Point2D(7/5, -12/5), Point2D(7/5, 12/5)]
|
664 |
+
"""
|
665 |
+
# TODO: Replace solve with nonlinsolve, when nonlinsolve will be able to solve in real domain
|
666 |
+
|
667 |
+
if isinstance(o, Point):
|
668 |
+
if o in self:
|
669 |
+
return [o]
|
670 |
+
else:
|
671 |
+
return []
|
672 |
+
|
673 |
+
elif isinstance(o, (Segment2D, Ray2D)):
|
674 |
+
ellipse_equation = self.equation(x, y)
|
675 |
+
result = solve([ellipse_equation, Line(
|
676 |
+
o.points[0], o.points[1]).equation(x, y)], [x, y],
|
677 |
+
set=True)[1]
|
678 |
+
return list(ordered([Point(i) for i in result if i in o]))
|
679 |
+
|
680 |
+
elif isinstance(o, Polygon):
|
681 |
+
return o.intersection(self)
|
682 |
+
|
683 |
+
elif isinstance(o, (Ellipse, Line2D)):
|
684 |
+
if o == self:
|
685 |
+
return self
|
686 |
+
else:
|
687 |
+
ellipse_equation = self.equation(x, y)
|
688 |
+
return list(ordered([Point(i) for i in solve(
|
689 |
+
[ellipse_equation, o.equation(x, y)], [x, y],
|
690 |
+
set=True)[1]]))
|
691 |
+
elif isinstance(o, LinearEntity3D):
|
692 |
+
raise TypeError('Entity must be two dimensional, not three dimensional')
|
693 |
+
else:
|
694 |
+
raise TypeError('Intersection not handled for %s' % func_name(o))
|
695 |
+
|
696 |
+
def is_tangent(self, o):
|
697 |
+
"""Is `o` tangent to the ellipse?
|
698 |
+
|
699 |
+
Parameters
|
700 |
+
==========
|
701 |
+
|
702 |
+
o : GeometryEntity
|
703 |
+
An Ellipse, LinearEntity or Polygon
|
704 |
+
|
705 |
+
Raises
|
706 |
+
======
|
707 |
+
|
708 |
+
NotImplementedError
|
709 |
+
When the wrong type of argument is supplied.
|
710 |
+
|
711 |
+
Returns
|
712 |
+
=======
|
713 |
+
|
714 |
+
is_tangent: boolean
|
715 |
+
True if o is tangent to the ellipse, False otherwise.
|
716 |
+
|
717 |
+
See Also
|
718 |
+
========
|
719 |
+
|
720 |
+
tangent_lines
|
721 |
+
|
722 |
+
Examples
|
723 |
+
========
|
724 |
+
|
725 |
+
>>> from sympy import Point, Ellipse, Line
|
726 |
+
>>> p0, p1, p2 = Point(0, 0), Point(3, 0), Point(3, 3)
|
727 |
+
>>> e1 = Ellipse(p0, 3, 2)
|
728 |
+
>>> l1 = Line(p1, p2)
|
729 |
+
>>> e1.is_tangent(l1)
|
730 |
+
True
|
731 |
+
|
732 |
+
"""
|
733 |
+
if isinstance(o, Point2D):
|
734 |
+
return False
|
735 |
+
elif isinstance(o, Ellipse):
|
736 |
+
intersect = self.intersection(o)
|
737 |
+
if isinstance(intersect, Ellipse):
|
738 |
+
return True
|
739 |
+
elif intersect:
|
740 |
+
return all((self.tangent_lines(i)[0]).equals(o.tangent_lines(i)[0]) for i in intersect)
|
741 |
+
else:
|
742 |
+
return False
|
743 |
+
elif isinstance(o, Line2D):
|
744 |
+
hit = self.intersection(o)
|
745 |
+
if not hit:
|
746 |
+
return False
|
747 |
+
if len(hit) == 1:
|
748 |
+
return True
|
749 |
+
# might return None if it can't decide
|
750 |
+
return hit[0].equals(hit[1])
|
751 |
+
elif isinstance(o, Ray2D):
|
752 |
+
intersect = self.intersection(o)
|
753 |
+
if len(intersect) == 1:
|
754 |
+
return intersect[0] != o.source and not self.encloses_point(o.source)
|
755 |
+
else:
|
756 |
+
return False
|
757 |
+
elif isinstance(o, (Segment2D, Polygon)):
|
758 |
+
all_tangents = False
|
759 |
+
segments = o.sides if isinstance(o, Polygon) else [o]
|
760 |
+
for segment in segments:
|
761 |
+
intersect = self.intersection(segment)
|
762 |
+
if len(intersect) == 1:
|
763 |
+
if not any(intersect[0] in i for i in segment.points) \
|
764 |
+
and not any(self.encloses_point(i) for i in segment.points):
|
765 |
+
all_tangents = True
|
766 |
+
continue
|
767 |
+
else:
|
768 |
+
return False
|
769 |
+
else:
|
770 |
+
return all_tangents
|
771 |
+
return all_tangents
|
772 |
+
elif isinstance(o, (LinearEntity3D, Point3D)):
|
773 |
+
raise TypeError('Entity must be two dimensional, not three dimensional')
|
774 |
+
else:
|
775 |
+
raise TypeError('Is_tangent not handled for %s' % func_name(o))
|
776 |
+
|
777 |
+
@property
|
778 |
+
def major(self):
|
779 |
+
"""Longer axis of the ellipse (if it can be determined) else hradius.
|
780 |
+
|
781 |
+
Returns
|
782 |
+
=======
|
783 |
+
|
784 |
+
major : number or expression
|
785 |
+
|
786 |
+
See Also
|
787 |
+
========
|
788 |
+
|
789 |
+
hradius, vradius, minor
|
790 |
+
|
791 |
+
Examples
|
792 |
+
========
|
793 |
+
|
794 |
+
>>> from sympy import Point, Ellipse, Symbol
|
795 |
+
>>> p1 = Point(0, 0)
|
796 |
+
>>> e1 = Ellipse(p1, 3, 1)
|
797 |
+
>>> e1.major
|
798 |
+
3
|
799 |
+
|
800 |
+
>>> a = Symbol('a')
|
801 |
+
>>> b = Symbol('b')
|
802 |
+
>>> Ellipse(p1, a, b).major
|
803 |
+
a
|
804 |
+
>>> Ellipse(p1, b, a).major
|
805 |
+
b
|
806 |
+
|
807 |
+
>>> m = Symbol('m')
|
808 |
+
>>> M = m + 1
|
809 |
+
>>> Ellipse(p1, m, M).major
|
810 |
+
m + 1
|
811 |
+
|
812 |
+
"""
|
813 |
+
ab = self.args[1:3]
|
814 |
+
if len(ab) == 1:
|
815 |
+
return ab[0]
|
816 |
+
a, b = ab
|
817 |
+
o = b - a < 0
|
818 |
+
if o == True:
|
819 |
+
return a
|
820 |
+
elif o == False:
|
821 |
+
return b
|
822 |
+
return self.hradius
|
823 |
+
|
824 |
+
@property
|
825 |
+
def minor(self):
|
826 |
+
"""Shorter axis of the ellipse (if it can be determined) else vradius.
|
827 |
+
|
828 |
+
Returns
|
829 |
+
=======
|
830 |
+
|
831 |
+
minor : number or expression
|
832 |
+
|
833 |
+
See Also
|
834 |
+
========
|
835 |
+
|
836 |
+
hradius, vradius, major
|
837 |
+
|
838 |
+
Examples
|
839 |
+
========
|
840 |
+
|
841 |
+
>>> from sympy import Point, Ellipse, Symbol
|
842 |
+
>>> p1 = Point(0, 0)
|
843 |
+
>>> e1 = Ellipse(p1, 3, 1)
|
844 |
+
>>> e1.minor
|
845 |
+
1
|
846 |
+
|
847 |
+
>>> a = Symbol('a')
|
848 |
+
>>> b = Symbol('b')
|
849 |
+
>>> Ellipse(p1, a, b).minor
|
850 |
+
b
|
851 |
+
>>> Ellipse(p1, b, a).minor
|
852 |
+
a
|
853 |
+
|
854 |
+
>>> m = Symbol('m')
|
855 |
+
>>> M = m + 1
|
856 |
+
>>> Ellipse(p1, m, M).minor
|
857 |
+
m
|
858 |
+
|
859 |
+
"""
|
860 |
+
ab = self.args[1:3]
|
861 |
+
if len(ab) == 1:
|
862 |
+
return ab[0]
|
863 |
+
a, b = ab
|
864 |
+
o = a - b < 0
|
865 |
+
if o == True:
|
866 |
+
return a
|
867 |
+
elif o == False:
|
868 |
+
return b
|
869 |
+
return self.vradius
|
870 |
+
|
871 |
+
def normal_lines(self, p, prec=None):
|
872 |
+
"""Normal lines between `p` and the ellipse.
|
873 |
+
|
874 |
+
Parameters
|
875 |
+
==========
|
876 |
+
|
877 |
+
p : Point
|
878 |
+
|
879 |
+
Returns
|
880 |
+
=======
|
881 |
+
|
882 |
+
normal_lines : list with 1, 2 or 4 Lines
|
883 |
+
|
884 |
+
Examples
|
885 |
+
========
|
886 |
+
|
887 |
+
>>> from sympy import Point, Ellipse
|
888 |
+
>>> e = Ellipse((0, 0), 2, 3)
|
889 |
+
>>> c = e.center
|
890 |
+
>>> e.normal_lines(c + Point(1, 0))
|
891 |
+
[Line2D(Point2D(0, 0), Point2D(1, 0))]
|
892 |
+
>>> e.normal_lines(c)
|
893 |
+
[Line2D(Point2D(0, 0), Point2D(0, 1)), Line2D(Point2D(0, 0), Point2D(1, 0))]
|
894 |
+
|
895 |
+
Off-axis points require the solution of a quartic equation. This
|
896 |
+
often leads to very large expressions that may be of little practical
|
897 |
+
use. An approximate solution of `prec` digits can be obtained by
|
898 |
+
passing in the desired value:
|
899 |
+
|
900 |
+
>>> e.normal_lines((3, 3), prec=2)
|
901 |
+
[Line2D(Point2D(-0.81, -2.7), Point2D(0.19, -1.2)),
|
902 |
+
Line2D(Point2D(1.5, -2.0), Point2D(2.5, -2.7))]
|
903 |
+
|
904 |
+
Whereas the above solution has an operation count of 12, the exact
|
905 |
+
solution has an operation count of 2020.
|
906 |
+
"""
|
907 |
+
p = Point(p, dim=2)
|
908 |
+
|
909 |
+
# XXX change True to something like self.angle == 0 if the arbitrarily
|
910 |
+
# rotated ellipse is introduced.
|
911 |
+
# https://github.com/sympy/sympy/issues/2815)
|
912 |
+
if True:
|
913 |
+
rv = []
|
914 |
+
if p.x == self.center.x:
|
915 |
+
rv.append(Line(self.center, slope=oo))
|
916 |
+
if p.y == self.center.y:
|
917 |
+
rv.append(Line(self.center, slope=0))
|
918 |
+
if rv:
|
919 |
+
# at these special orientations of p either 1 or 2 normals
|
920 |
+
# exist and we are done
|
921 |
+
return rv
|
922 |
+
|
923 |
+
# find the 4 normal points and construct lines through them with
|
924 |
+
# the corresponding slope
|
925 |
+
eq = self.equation(x, y)
|
926 |
+
dydx = idiff(eq, y, x)
|
927 |
+
norm = -1/dydx
|
928 |
+
slope = Line(p, (x, y)).slope
|
929 |
+
seq = slope - norm
|
930 |
+
|
931 |
+
# TODO: Replace solve with solveset, when this line is tested
|
932 |
+
yis = solve(seq, y)[0]
|
933 |
+
xeq = eq.subs(y, yis).as_numer_denom()[0].expand()
|
934 |
+
if len(xeq.free_symbols) == 1:
|
935 |
+
try:
|
936 |
+
# this is so much faster, it's worth a try
|
937 |
+
xsol = Poly(xeq, x).real_roots()
|
938 |
+
except (DomainError, PolynomialError, NotImplementedError):
|
939 |
+
# TODO: Replace solve with solveset, when these lines are tested
|
940 |
+
xsol = _nsort(solve(xeq, x), separated=True)[0]
|
941 |
+
points = [Point(i, solve(eq.subs(x, i), y)[0]) for i in xsol]
|
942 |
+
else:
|
943 |
+
raise NotImplementedError(
|
944 |
+
'intersections for the general ellipse are not supported')
|
945 |
+
slopes = [norm.subs(zip((x, y), pt.args)) for pt in points]
|
946 |
+
if prec is not None:
|
947 |
+
points = [pt.n(prec) for pt in points]
|
948 |
+
slopes = [i if _not_a_coeff(i) else i.n(prec) for i in slopes]
|
949 |
+
return [Line(pt, slope=s) for pt, s in zip(points, slopes)]
|
950 |
+
|
951 |
+
@property
|
952 |
+
def periapsis(self):
|
953 |
+
"""The periapsis of the ellipse.
|
954 |
+
|
955 |
+
The shortest distance between the focus and the contour.
|
956 |
+
|
957 |
+
Returns
|
958 |
+
=======
|
959 |
+
|
960 |
+
periapsis : number
|
961 |
+
|
962 |
+
See Also
|
963 |
+
========
|
964 |
+
|
965 |
+
apoapsis : Returns greatest distance between focus and contour
|
966 |
+
|
967 |
+
Examples
|
968 |
+
========
|
969 |
+
|
970 |
+
>>> from sympy import Point, Ellipse
|
971 |
+
>>> p1 = Point(0, 0)
|
972 |
+
>>> e1 = Ellipse(p1, 3, 1)
|
973 |
+
>>> e1.periapsis
|
974 |
+
3 - 2*sqrt(2)
|
975 |
+
|
976 |
+
"""
|
977 |
+
return self.major * (1 - self.eccentricity)
|
978 |
+
|
979 |
+
@property
|
980 |
+
def semilatus_rectum(self):
|
981 |
+
"""
|
982 |
+
Calculates the semi-latus rectum of the Ellipse.
|
983 |
+
|
984 |
+
Semi-latus rectum is defined as one half of the chord through a
|
985 |
+
focus parallel to the conic section directrix of a conic section.
|
986 |
+
|
987 |
+
Returns
|
988 |
+
=======
|
989 |
+
|
990 |
+
semilatus_rectum : number
|
991 |
+
|
992 |
+
See Also
|
993 |
+
========
|
994 |
+
|
995 |
+
apoapsis : Returns greatest distance between focus and contour
|
996 |
+
|
997 |
+
periapsis : The shortest distance between the focus and the contour
|
998 |
+
|
999 |
+
Examples
|
1000 |
+
========
|
1001 |
+
|
1002 |
+
>>> from sympy import Point, Ellipse
|
1003 |
+
>>> p1 = Point(0, 0)
|
1004 |
+
>>> e1 = Ellipse(p1, 3, 1)
|
1005 |
+
>>> e1.semilatus_rectum
|
1006 |
+
1/3
|
1007 |
+
|
1008 |
+
References
|
1009 |
+
==========
|
1010 |
+
|
1011 |
+
.. [1] https://mathworld.wolfram.com/SemilatusRectum.html
|
1012 |
+
.. [2] https://en.wikipedia.org/wiki/Ellipse#Semi-latus_rectum
|
1013 |
+
|
1014 |
+
"""
|
1015 |
+
return self.major * (1 - self.eccentricity ** 2)
|
1016 |
+
|
1017 |
+
def auxiliary_circle(self):
|
1018 |
+
"""Returns a Circle whose diameter is the major axis of the ellipse.
|
1019 |
+
|
1020 |
+
Examples
|
1021 |
+
========
|
1022 |
+
|
1023 |
+
>>> from sympy import Ellipse, Point, symbols
|
1024 |
+
>>> c = Point(1, 2)
|
1025 |
+
>>> Ellipse(c, 8, 7).auxiliary_circle()
|
1026 |
+
Circle(Point2D(1, 2), 8)
|
1027 |
+
>>> a, b = symbols('a b')
|
1028 |
+
>>> Ellipse(c, a, b).auxiliary_circle()
|
1029 |
+
Circle(Point2D(1, 2), Max(a, b))
|
1030 |
+
"""
|
1031 |
+
return Circle(self.center, Max(self.hradius, self.vradius))
|
1032 |
+
|
1033 |
+
def director_circle(self):
|
1034 |
+
"""
|
1035 |
+
Returns a Circle consisting of all points where two perpendicular
|
1036 |
+
tangent lines to the ellipse cross each other.
|
1037 |
+
|
1038 |
+
Returns
|
1039 |
+
=======
|
1040 |
+
|
1041 |
+
Circle
|
1042 |
+
A director circle returned as a geometric object.
|
1043 |
+
|
1044 |
+
Examples
|
1045 |
+
========
|
1046 |
+
|
1047 |
+
>>> from sympy import Ellipse, Point, symbols
|
1048 |
+
>>> c = Point(3,8)
|
1049 |
+
>>> Ellipse(c, 7, 9).director_circle()
|
1050 |
+
Circle(Point2D(3, 8), sqrt(130))
|
1051 |
+
>>> a, b = symbols('a b')
|
1052 |
+
>>> Ellipse(c, a, b).director_circle()
|
1053 |
+
Circle(Point2D(3, 8), sqrt(a**2 + b**2))
|
1054 |
+
|
1055 |
+
References
|
1056 |
+
==========
|
1057 |
+
|
1058 |
+
.. [1] https://en.wikipedia.org/wiki/Director_circle
|
1059 |
+
|
1060 |
+
"""
|
1061 |
+
return Circle(self.center, sqrt(self.hradius**2 + self.vradius**2))
|
1062 |
+
|
1063 |
+
def plot_interval(self, parameter='t'):
|
1064 |
+
"""The plot interval for the default geometric plot of the Ellipse.
|
1065 |
+
|
1066 |
+
Parameters
|
1067 |
+
==========
|
1068 |
+
|
1069 |
+
parameter : str, optional
|
1070 |
+
Default value is 't'.
|
1071 |
+
|
1072 |
+
Returns
|
1073 |
+
=======
|
1074 |
+
|
1075 |
+
plot_interval : list
|
1076 |
+
[parameter, lower_bound, upper_bound]
|
1077 |
+
|
1078 |
+
Examples
|
1079 |
+
========
|
1080 |
+
|
1081 |
+
>>> from sympy import Point, Ellipse
|
1082 |
+
>>> e1 = Ellipse(Point(0, 0), 3, 2)
|
1083 |
+
>>> e1.plot_interval()
|
1084 |
+
[t, -pi, pi]
|
1085 |
+
|
1086 |
+
"""
|
1087 |
+
t = _symbol(parameter, real=True)
|
1088 |
+
return [t, -S.Pi, S.Pi]
|
1089 |
+
|
1090 |
+
def random_point(self, seed=None):
|
1091 |
+
"""A random point on the ellipse.
|
1092 |
+
|
1093 |
+
Returns
|
1094 |
+
=======
|
1095 |
+
|
1096 |
+
point : Point
|
1097 |
+
|
1098 |
+
Examples
|
1099 |
+
========
|
1100 |
+
|
1101 |
+
>>> from sympy import Point, Ellipse
|
1102 |
+
>>> e1 = Ellipse(Point(0, 0), 3, 2)
|
1103 |
+
>>> e1.random_point() # gives some random point
|
1104 |
+
Point2D(...)
|
1105 |
+
>>> p1 = e1.random_point(seed=0); p1.n(2)
|
1106 |
+
Point2D(2.1, 1.4)
|
1107 |
+
|
1108 |
+
Notes
|
1109 |
+
=====
|
1110 |
+
|
1111 |
+
When creating a random point, one may simply replace the
|
1112 |
+
parameter with a random number. When doing so, however, the
|
1113 |
+
random number should be made a Rational or else the point
|
1114 |
+
may not test as being in the ellipse:
|
1115 |
+
|
1116 |
+
>>> from sympy.abc import t
|
1117 |
+
>>> from sympy import Rational
|
1118 |
+
>>> arb = e1.arbitrary_point(t); arb
|
1119 |
+
Point2D(3*cos(t), 2*sin(t))
|
1120 |
+
>>> arb.subs(t, .1) in e1
|
1121 |
+
False
|
1122 |
+
>>> arb.subs(t, Rational(.1)) in e1
|
1123 |
+
True
|
1124 |
+
>>> arb.subs(t, Rational('.1')) in e1
|
1125 |
+
True
|
1126 |
+
|
1127 |
+
See Also
|
1128 |
+
========
|
1129 |
+
sympy.geometry.point.Point
|
1130 |
+
arbitrary_point : Returns parameterized point on ellipse
|
1131 |
+
"""
|
1132 |
+
t = _symbol('t', real=True)
|
1133 |
+
x, y = self.arbitrary_point(t).args
|
1134 |
+
# get a random value in [-1, 1) corresponding to cos(t)
|
1135 |
+
# and confirm that it will test as being in the ellipse
|
1136 |
+
if seed is not None:
|
1137 |
+
rng = random.Random(seed)
|
1138 |
+
else:
|
1139 |
+
rng = random
|
1140 |
+
# simplify this now or else the Float will turn s into a Float
|
1141 |
+
r = Rational(rng.random())
|
1142 |
+
c = 2*r - 1
|
1143 |
+
s = sqrt(1 - c**2)
|
1144 |
+
return Point(x.subs(cos(t), c), y.subs(sin(t), s))
|
1145 |
+
|
1146 |
+
def reflect(self, line):
|
1147 |
+
"""Override GeometryEntity.reflect since the radius
|
1148 |
+
is not a GeometryEntity.
|
1149 |
+
|
1150 |
+
Examples
|
1151 |
+
========
|
1152 |
+
|
1153 |
+
>>> from sympy import Circle, Line
|
1154 |
+
>>> Circle((0, 1), 1).reflect(Line((0, 0), (1, 1)))
|
1155 |
+
Circle(Point2D(1, 0), -1)
|
1156 |
+
>>> from sympy import Ellipse, Line, Point
|
1157 |
+
>>> Ellipse(Point(3, 4), 1, 3).reflect(Line(Point(0, -4), Point(5, 0)))
|
1158 |
+
Traceback (most recent call last):
|
1159 |
+
...
|
1160 |
+
NotImplementedError:
|
1161 |
+
General Ellipse is not supported but the equation of the reflected
|
1162 |
+
Ellipse is given by the zeros of: f(x, y) = (9*x/41 + 40*y/41 +
|
1163 |
+
37/41)**2 + (40*x/123 - 3*y/41 - 364/123)**2 - 1
|
1164 |
+
|
1165 |
+
Notes
|
1166 |
+
=====
|
1167 |
+
|
1168 |
+
Until the general ellipse (with no axis parallel to the x-axis) is
|
1169 |
+
supported a NotImplemented error is raised and the equation whose
|
1170 |
+
zeros define the rotated ellipse is given.
|
1171 |
+
|
1172 |
+
"""
|
1173 |
+
|
1174 |
+
if line.slope in (0, oo):
|
1175 |
+
c = self.center
|
1176 |
+
c = c.reflect(line)
|
1177 |
+
return self.func(c, -self.hradius, self.vradius)
|
1178 |
+
else:
|
1179 |
+
x, y = [uniquely_named_symbol(
|
1180 |
+
name, (self, line), modify=lambda s: '_' + s, real=True)
|
1181 |
+
for name in 'xy']
|
1182 |
+
expr = self.equation(x, y)
|
1183 |
+
p = Point(x, y).reflect(line)
|
1184 |
+
result = expr.subs(zip((x, y), p.args
|
1185 |
+
), simultaneous=True)
|
1186 |
+
raise NotImplementedError(filldedent(
|
1187 |
+
'General Ellipse is not supported but the equation '
|
1188 |
+
'of the reflected Ellipse is given by the zeros of: ' +
|
1189 |
+
"f(%s, %s) = %s" % (str(x), str(y), str(result))))
|
1190 |
+
|
1191 |
+
def rotate(self, angle=0, pt=None):
|
1192 |
+
"""Rotate ``angle`` radians counterclockwise about Point ``pt``.
|
1193 |
+
|
1194 |
+
Note: since the general ellipse is not supported, only rotations that
|
1195 |
+
are integer multiples of pi/2 are allowed.
|
1196 |
+
|
1197 |
+
Examples
|
1198 |
+
========
|
1199 |
+
|
1200 |
+
>>> from sympy import Ellipse, pi
|
1201 |
+
>>> Ellipse((1, 0), 2, 1).rotate(pi/2)
|
1202 |
+
Ellipse(Point2D(0, 1), 1, 2)
|
1203 |
+
>>> Ellipse((1, 0), 2, 1).rotate(pi)
|
1204 |
+
Ellipse(Point2D(-1, 0), 2, 1)
|
1205 |
+
"""
|
1206 |
+
if self.hradius == self.vradius:
|
1207 |
+
return self.func(self.center.rotate(angle, pt), self.hradius)
|
1208 |
+
if (angle/S.Pi).is_integer:
|
1209 |
+
return super().rotate(angle, pt)
|
1210 |
+
if (2*angle/S.Pi).is_integer:
|
1211 |
+
return self.func(self.center.rotate(angle, pt), self.vradius, self.hradius)
|
1212 |
+
# XXX see https://github.com/sympy/sympy/issues/2815 for general ellipes
|
1213 |
+
raise NotImplementedError('Only rotations of pi/2 are currently supported for Ellipse.')
|
1214 |
+
|
1215 |
+
def scale(self, x=1, y=1, pt=None):
|
1216 |
+
"""Override GeometryEntity.scale since it is the major and minor
|
1217 |
+
axes which must be scaled and they are not GeometryEntities.
|
1218 |
+
|
1219 |
+
Examples
|
1220 |
+
========
|
1221 |
+
|
1222 |
+
>>> from sympy import Ellipse
|
1223 |
+
>>> Ellipse((0, 0), 2, 1).scale(2, 4)
|
1224 |
+
Circle(Point2D(0, 0), 4)
|
1225 |
+
>>> Ellipse((0, 0), 2, 1).scale(2)
|
1226 |
+
Ellipse(Point2D(0, 0), 4, 1)
|
1227 |
+
"""
|
1228 |
+
c = self.center
|
1229 |
+
if pt:
|
1230 |
+
pt = Point(pt, dim=2)
|
1231 |
+
return self.translate(*(-pt).args).scale(x, y).translate(*pt.args)
|
1232 |
+
h = self.hradius
|
1233 |
+
v = self.vradius
|
1234 |
+
return self.func(c.scale(x, y), hradius=h*x, vradius=v*y)
|
1235 |
+
|
1236 |
+
def tangent_lines(self, p):
|
1237 |
+
"""Tangent lines between `p` and the ellipse.
|
1238 |
+
|
1239 |
+
If `p` is on the ellipse, returns the tangent line through point `p`.
|
1240 |
+
Otherwise, returns the tangent line(s) from `p` to the ellipse, or
|
1241 |
+
None if no tangent line is possible (e.g., `p` inside ellipse).
|
1242 |
+
|
1243 |
+
Parameters
|
1244 |
+
==========
|
1245 |
+
|
1246 |
+
p : Point
|
1247 |
+
|
1248 |
+
Returns
|
1249 |
+
=======
|
1250 |
+
|
1251 |
+
tangent_lines : list with 1 or 2 Lines
|
1252 |
+
|
1253 |
+
Raises
|
1254 |
+
======
|
1255 |
+
|
1256 |
+
NotImplementedError
|
1257 |
+
Can only find tangent lines for a point, `p`, on the ellipse.
|
1258 |
+
|
1259 |
+
See Also
|
1260 |
+
========
|
1261 |
+
|
1262 |
+
sympy.geometry.point.Point, sympy.geometry.line.Line
|
1263 |
+
|
1264 |
+
Examples
|
1265 |
+
========
|
1266 |
+
|
1267 |
+
>>> from sympy import Point, Ellipse
|
1268 |
+
>>> e1 = Ellipse(Point(0, 0), 3, 2)
|
1269 |
+
>>> e1.tangent_lines(Point(3, 0))
|
1270 |
+
[Line2D(Point2D(3, 0), Point2D(3, -12))]
|
1271 |
+
|
1272 |
+
"""
|
1273 |
+
p = Point(p, dim=2)
|
1274 |
+
if self.encloses_point(p):
|
1275 |
+
return []
|
1276 |
+
|
1277 |
+
if p in self:
|
1278 |
+
delta = self.center - p
|
1279 |
+
rise = (self.vradius**2)*delta.x
|
1280 |
+
run = -(self.hradius**2)*delta.y
|
1281 |
+
p2 = Point(simplify(p.x + run),
|
1282 |
+
simplify(p.y + rise))
|
1283 |
+
return [Line(p, p2)]
|
1284 |
+
else:
|
1285 |
+
if len(self.foci) == 2:
|
1286 |
+
f1, f2 = self.foci
|
1287 |
+
maj = self.hradius
|
1288 |
+
test = (2*maj -
|
1289 |
+
Point.distance(f1, p) -
|
1290 |
+
Point.distance(f2, p))
|
1291 |
+
else:
|
1292 |
+
test = self.radius - Point.distance(self.center, p)
|
1293 |
+
if test.is_number and test.is_positive:
|
1294 |
+
return []
|
1295 |
+
# else p is outside the ellipse or we can't tell. In case of the
|
1296 |
+
# latter, the solutions returned will only be valid if
|
1297 |
+
# the point is not inside the ellipse; if it is, nan will result.
|
1298 |
+
eq = self.equation(x, y)
|
1299 |
+
dydx = idiff(eq, y, x)
|
1300 |
+
slope = Line(p, Point(x, y)).slope
|
1301 |
+
|
1302 |
+
# TODO: Replace solve with solveset, when this line is tested
|
1303 |
+
tangent_points = solve([slope - dydx, eq], [x, y])
|
1304 |
+
|
1305 |
+
# handle horizontal and vertical tangent lines
|
1306 |
+
if len(tangent_points) == 1:
|
1307 |
+
if tangent_points[0][
|
1308 |
+
0] == p.x or tangent_points[0][1] == p.y:
|
1309 |
+
return [Line(p, p + Point(1, 0)), Line(p, p + Point(0, 1))]
|
1310 |
+
else:
|
1311 |
+
return [Line(p, p + Point(0, 1)), Line(p, tangent_points[0])]
|
1312 |
+
|
1313 |
+
# others
|
1314 |
+
return [Line(p, tangent_points[0]), Line(p, tangent_points[1])]
|
1315 |
+
|
1316 |
+
@property
|
1317 |
+
def vradius(self):
|
1318 |
+
"""The vertical radius of the ellipse.
|
1319 |
+
|
1320 |
+
Returns
|
1321 |
+
=======
|
1322 |
+
|
1323 |
+
vradius : number
|
1324 |
+
|
1325 |
+
See Also
|
1326 |
+
========
|
1327 |
+
|
1328 |
+
hradius, major, minor
|
1329 |
+
|
1330 |
+
Examples
|
1331 |
+
========
|
1332 |
+
|
1333 |
+
>>> from sympy import Point, Ellipse
|
1334 |
+
>>> p1 = Point(0, 0)
|
1335 |
+
>>> e1 = Ellipse(p1, 3, 1)
|
1336 |
+
>>> e1.vradius
|
1337 |
+
1
|
1338 |
+
|
1339 |
+
"""
|
1340 |
+
return self.args[2]
|
1341 |
+
|
1342 |
+
|
1343 |
+
def second_moment_of_area(self, point=None):
|
1344 |
+
"""Returns the second moment and product moment area of an ellipse.
|
1345 |
+
|
1346 |
+
Parameters
|
1347 |
+
==========
|
1348 |
+
|
1349 |
+
point : Point, two-tuple of sympifiable objects, or None(default=None)
|
1350 |
+
point is the point about which second moment of area is to be found.
|
1351 |
+
If "point=None" it will be calculated about the axis passing through the
|
1352 |
+
centroid of the ellipse.
|
1353 |
+
|
1354 |
+
Returns
|
1355 |
+
=======
|
1356 |
+
|
1357 |
+
I_xx, I_yy, I_xy : number or SymPy expression
|
1358 |
+
I_xx, I_yy are second moment of area of an ellise.
|
1359 |
+
I_xy is product moment of area of an ellipse.
|
1360 |
+
|
1361 |
+
Examples
|
1362 |
+
========
|
1363 |
+
|
1364 |
+
>>> from sympy import Point, Ellipse
|
1365 |
+
>>> p1 = Point(0, 0)
|
1366 |
+
>>> e1 = Ellipse(p1, 3, 1)
|
1367 |
+
>>> e1.second_moment_of_area()
|
1368 |
+
(3*pi/4, 27*pi/4, 0)
|
1369 |
+
|
1370 |
+
References
|
1371 |
+
==========
|
1372 |
+
|
1373 |
+
.. [1] https://en.wikipedia.org/wiki/List_of_second_moments_of_area
|
1374 |
+
|
1375 |
+
"""
|
1376 |
+
|
1377 |
+
I_xx = (S.Pi*(self.hradius)*(self.vradius**3))/4
|
1378 |
+
I_yy = (S.Pi*(self.hradius**3)*(self.vradius))/4
|
1379 |
+
I_xy = 0
|
1380 |
+
|
1381 |
+
if point is None:
|
1382 |
+
return I_xx, I_yy, I_xy
|
1383 |
+
|
1384 |
+
# parallel axis theorem
|
1385 |
+
I_xx = I_xx + self.area*((point[1] - self.center.y)**2)
|
1386 |
+
I_yy = I_yy + self.area*((point[0] - self.center.x)**2)
|
1387 |
+
I_xy = I_xy + self.area*(point[0] - self.center.x)*(point[1] - self.center.y)
|
1388 |
+
|
1389 |
+
return I_xx, I_yy, I_xy
|
1390 |
+
|
1391 |
+
|
1392 |
+
def polar_second_moment_of_area(self):
|
1393 |
+
"""Returns the polar second moment of area of an Ellipse
|
1394 |
+
|
1395 |
+
It is a constituent of the second moment of area, linked through
|
1396 |
+
the perpendicular axis theorem. While the planar second moment of
|
1397 |
+
area describes an object's resistance to deflection (bending) when
|
1398 |
+
subjected to a force applied to a plane parallel to the central
|
1399 |
+
axis, the polar second moment of area describes an object's
|
1400 |
+
resistance to deflection when subjected to a moment applied in a
|
1401 |
+
plane perpendicular to the object's central axis (i.e. parallel to
|
1402 |
+
the cross-section)
|
1403 |
+
|
1404 |
+
Examples
|
1405 |
+
========
|
1406 |
+
|
1407 |
+
>>> from sympy import symbols, Circle, Ellipse
|
1408 |
+
>>> c = Circle((5, 5), 4)
|
1409 |
+
>>> c.polar_second_moment_of_area()
|
1410 |
+
128*pi
|
1411 |
+
>>> a, b = symbols('a, b')
|
1412 |
+
>>> e = Ellipse((0, 0), a, b)
|
1413 |
+
>>> e.polar_second_moment_of_area()
|
1414 |
+
pi*a**3*b/4 + pi*a*b**3/4
|
1415 |
+
|
1416 |
+
References
|
1417 |
+
==========
|
1418 |
+
|
1419 |
+
.. [1] https://en.wikipedia.org/wiki/Polar_moment_of_inertia
|
1420 |
+
|
1421 |
+
"""
|
1422 |
+
second_moment = self.second_moment_of_area()
|
1423 |
+
return second_moment[0] + second_moment[1]
|
1424 |
+
|
1425 |
+
|
1426 |
+
def section_modulus(self, point=None):
|
1427 |
+
"""Returns a tuple with the section modulus of an ellipse
|
1428 |
+
|
1429 |
+
Section modulus is a geometric property of an ellipse defined as the
|
1430 |
+
ratio of second moment of area to the distance of the extreme end of
|
1431 |
+
the ellipse from the centroidal axis.
|
1432 |
+
|
1433 |
+
Parameters
|
1434 |
+
==========
|
1435 |
+
|
1436 |
+
point : Point, two-tuple of sympifyable objects, or None(default=None)
|
1437 |
+
point is the point at which section modulus is to be found.
|
1438 |
+
If "point=None" section modulus will be calculated for the
|
1439 |
+
point farthest from the centroidal axis of the ellipse.
|
1440 |
+
|
1441 |
+
Returns
|
1442 |
+
=======
|
1443 |
+
|
1444 |
+
S_x, S_y: numbers or SymPy expressions
|
1445 |
+
S_x is the section modulus with respect to the x-axis
|
1446 |
+
S_y is the section modulus with respect to the y-axis
|
1447 |
+
A negative sign indicates that the section modulus is
|
1448 |
+
determined for a point below the centroidal axis.
|
1449 |
+
|
1450 |
+
Examples
|
1451 |
+
========
|
1452 |
+
|
1453 |
+
>>> from sympy import Symbol, Ellipse, Circle, Point2D
|
1454 |
+
>>> d = Symbol('d', positive=True)
|
1455 |
+
>>> c = Circle((0, 0), d/2)
|
1456 |
+
>>> c.section_modulus()
|
1457 |
+
(pi*d**3/32, pi*d**3/32)
|
1458 |
+
>>> e = Ellipse(Point2D(0, 0), 2, 4)
|
1459 |
+
>>> e.section_modulus()
|
1460 |
+
(8*pi, 4*pi)
|
1461 |
+
>>> e.section_modulus((2, 2))
|
1462 |
+
(16*pi, 4*pi)
|
1463 |
+
|
1464 |
+
References
|
1465 |
+
==========
|
1466 |
+
|
1467 |
+
.. [1] https://en.wikipedia.org/wiki/Section_modulus
|
1468 |
+
|
1469 |
+
"""
|
1470 |
+
x_c, y_c = self.center
|
1471 |
+
if point is None:
|
1472 |
+
# taking x and y as maximum distances from centroid
|
1473 |
+
x_min, y_min, x_max, y_max = self.bounds
|
1474 |
+
y = max(y_c - y_min, y_max - y_c)
|
1475 |
+
x = max(x_c - x_min, x_max - x_c)
|
1476 |
+
else:
|
1477 |
+
# taking x and y as distances of the given point from the center
|
1478 |
+
point = Point2D(point)
|
1479 |
+
y = point.y - y_c
|
1480 |
+
x = point.x - x_c
|
1481 |
+
|
1482 |
+
second_moment = self.second_moment_of_area()
|
1483 |
+
S_x = second_moment[0]/y
|
1484 |
+
S_y = second_moment[1]/x
|
1485 |
+
|
1486 |
+
return S_x, S_y
|
1487 |
+
|
1488 |
+
|
1489 |
+
class Circle(Ellipse):
|
1490 |
+
"""A circle in space.
|
1491 |
+
|
1492 |
+
Constructed simply from a center and a radius, from three
|
1493 |
+
non-collinear points, or the equation of a circle.
|
1494 |
+
|
1495 |
+
Parameters
|
1496 |
+
==========
|
1497 |
+
|
1498 |
+
center : Point
|
1499 |
+
radius : number or SymPy expression
|
1500 |
+
points : sequence of three Points
|
1501 |
+
equation : equation of a circle
|
1502 |
+
|
1503 |
+
Attributes
|
1504 |
+
==========
|
1505 |
+
|
1506 |
+
radius (synonymous with hradius, vradius, major and minor)
|
1507 |
+
circumference
|
1508 |
+
equation
|
1509 |
+
|
1510 |
+
Raises
|
1511 |
+
======
|
1512 |
+
|
1513 |
+
GeometryError
|
1514 |
+
When the given equation is not that of a circle.
|
1515 |
+
When trying to construct circle from incorrect parameters.
|
1516 |
+
|
1517 |
+
See Also
|
1518 |
+
========
|
1519 |
+
|
1520 |
+
Ellipse, sympy.geometry.point.Point
|
1521 |
+
|
1522 |
+
Examples
|
1523 |
+
========
|
1524 |
+
|
1525 |
+
>>> from sympy import Point, Circle, Eq
|
1526 |
+
>>> from sympy.abc import x, y, a, b
|
1527 |
+
|
1528 |
+
A circle constructed from a center and radius:
|
1529 |
+
|
1530 |
+
>>> c1 = Circle(Point(0, 0), 5)
|
1531 |
+
>>> c1.hradius, c1.vradius, c1.radius
|
1532 |
+
(5, 5, 5)
|
1533 |
+
|
1534 |
+
A circle constructed from three points:
|
1535 |
+
|
1536 |
+
>>> c2 = Circle(Point(0, 0), Point(1, 1), Point(1, 0))
|
1537 |
+
>>> c2.hradius, c2.vradius, c2.radius, c2.center
|
1538 |
+
(sqrt(2)/2, sqrt(2)/2, sqrt(2)/2, Point2D(1/2, 1/2))
|
1539 |
+
|
1540 |
+
A circle can be constructed from an equation in the form
|
1541 |
+
`a*x**2 + by**2 + gx + hy + c = 0`, too:
|
1542 |
+
|
1543 |
+
>>> Circle(x**2 + y**2 - 25)
|
1544 |
+
Circle(Point2D(0, 0), 5)
|
1545 |
+
|
1546 |
+
If the variables corresponding to x and y are named something
|
1547 |
+
else, their name or symbol can be supplied:
|
1548 |
+
|
1549 |
+
>>> Circle(Eq(a**2 + b**2, 25), x='a', y=b)
|
1550 |
+
Circle(Point2D(0, 0), 5)
|
1551 |
+
"""
|
1552 |
+
|
1553 |
+
def __new__(cls, *args, **kwargs):
|
1554 |
+
evaluate = kwargs.get('evaluate', global_parameters.evaluate)
|
1555 |
+
if len(args) == 1 and isinstance(args[0], (Expr, Eq)):
|
1556 |
+
x = kwargs.get('x', 'x')
|
1557 |
+
y = kwargs.get('y', 'y')
|
1558 |
+
equation = args[0].expand()
|
1559 |
+
if isinstance(equation, Eq):
|
1560 |
+
equation = equation.lhs - equation.rhs
|
1561 |
+
x = find(x, equation)
|
1562 |
+
y = find(y, equation)
|
1563 |
+
|
1564 |
+
try:
|
1565 |
+
a, b, c, d, e = linear_coeffs(equation, x**2, y**2, x, y)
|
1566 |
+
except ValueError:
|
1567 |
+
raise GeometryError("The given equation is not that of a circle.")
|
1568 |
+
|
1569 |
+
if S.Zero in (a, b) or a != b:
|
1570 |
+
raise GeometryError("The given equation is not that of a circle.")
|
1571 |
+
|
1572 |
+
center_x = -c/a/2
|
1573 |
+
center_y = -d/b/2
|
1574 |
+
r2 = (center_x**2) + (center_y**2) - e/a
|
1575 |
+
|
1576 |
+
return Circle((center_x, center_y), sqrt(r2), evaluate=evaluate)
|
1577 |
+
|
1578 |
+
else:
|
1579 |
+
c, r = None, None
|
1580 |
+
if len(args) == 3:
|
1581 |
+
args = [Point(a, dim=2, evaluate=evaluate) for a in args]
|
1582 |
+
t = Triangle(*args)
|
1583 |
+
if not isinstance(t, Triangle):
|
1584 |
+
return t
|
1585 |
+
c = t.circumcenter
|
1586 |
+
r = t.circumradius
|
1587 |
+
elif len(args) == 2:
|
1588 |
+
# Assume (center, radius) pair
|
1589 |
+
c = Point(args[0], dim=2, evaluate=evaluate)
|
1590 |
+
r = args[1]
|
1591 |
+
# this will prohibit imaginary radius
|
1592 |
+
try:
|
1593 |
+
r = Point(r, 0, evaluate=evaluate).x
|
1594 |
+
except ValueError:
|
1595 |
+
raise GeometryError("Circle with imaginary radius is not permitted")
|
1596 |
+
|
1597 |
+
if not (c is None or r is None):
|
1598 |
+
if r == 0:
|
1599 |
+
return c
|
1600 |
+
return GeometryEntity.__new__(cls, c, r, **kwargs)
|
1601 |
+
|
1602 |
+
raise GeometryError("Circle.__new__ received unknown arguments")
|
1603 |
+
|
1604 |
+
def _eval_evalf(self, prec=15, **options):
|
1605 |
+
pt, r = self.args
|
1606 |
+
dps = prec_to_dps(prec)
|
1607 |
+
pt = pt.evalf(n=dps, **options)
|
1608 |
+
r = r.evalf(n=dps, **options)
|
1609 |
+
return self.func(pt, r, evaluate=False)
|
1610 |
+
|
1611 |
+
@property
|
1612 |
+
def circumference(self):
|
1613 |
+
"""The circumference of the circle.
|
1614 |
+
|
1615 |
+
Returns
|
1616 |
+
=======
|
1617 |
+
|
1618 |
+
circumference : number or SymPy expression
|
1619 |
+
|
1620 |
+
Examples
|
1621 |
+
========
|
1622 |
+
|
1623 |
+
>>> from sympy import Point, Circle
|
1624 |
+
>>> c1 = Circle(Point(3, 4), 6)
|
1625 |
+
>>> c1.circumference
|
1626 |
+
12*pi
|
1627 |
+
|
1628 |
+
"""
|
1629 |
+
return 2 * S.Pi * self.radius
|
1630 |
+
|
1631 |
+
def equation(self, x='x', y='y'):
|
1632 |
+
"""The equation of the circle.
|
1633 |
+
|
1634 |
+
Parameters
|
1635 |
+
==========
|
1636 |
+
|
1637 |
+
x : str or Symbol, optional
|
1638 |
+
Default value is 'x'.
|
1639 |
+
y : str or Symbol, optional
|
1640 |
+
Default value is 'y'.
|
1641 |
+
|
1642 |
+
Returns
|
1643 |
+
=======
|
1644 |
+
|
1645 |
+
equation : SymPy expression
|
1646 |
+
|
1647 |
+
Examples
|
1648 |
+
========
|
1649 |
+
|
1650 |
+
>>> from sympy import Point, Circle
|
1651 |
+
>>> c1 = Circle(Point(0, 0), 5)
|
1652 |
+
>>> c1.equation()
|
1653 |
+
x**2 + y**2 - 25
|
1654 |
+
|
1655 |
+
"""
|
1656 |
+
x = _symbol(x, real=True)
|
1657 |
+
y = _symbol(y, real=True)
|
1658 |
+
t1 = (x - self.center.x)**2
|
1659 |
+
t2 = (y - self.center.y)**2
|
1660 |
+
return t1 + t2 - self.major**2
|
1661 |
+
|
1662 |
+
def intersection(self, o):
|
1663 |
+
"""The intersection of this circle with another geometrical entity.
|
1664 |
+
|
1665 |
+
Parameters
|
1666 |
+
==========
|
1667 |
+
|
1668 |
+
o : GeometryEntity
|
1669 |
+
|
1670 |
+
Returns
|
1671 |
+
=======
|
1672 |
+
|
1673 |
+
intersection : list of GeometryEntities
|
1674 |
+
|
1675 |
+
Examples
|
1676 |
+
========
|
1677 |
+
|
1678 |
+
>>> from sympy import Point, Circle, Line, Ray
|
1679 |
+
>>> p1, p2, p3 = Point(0, 0), Point(5, 5), Point(6, 0)
|
1680 |
+
>>> p4 = Point(5, 0)
|
1681 |
+
>>> c1 = Circle(p1, 5)
|
1682 |
+
>>> c1.intersection(p2)
|
1683 |
+
[]
|
1684 |
+
>>> c1.intersection(p4)
|
1685 |
+
[Point2D(5, 0)]
|
1686 |
+
>>> c1.intersection(Ray(p1, p2))
|
1687 |
+
[Point2D(5*sqrt(2)/2, 5*sqrt(2)/2)]
|
1688 |
+
>>> c1.intersection(Line(p2, p3))
|
1689 |
+
[]
|
1690 |
+
|
1691 |
+
"""
|
1692 |
+
return Ellipse.intersection(self, o)
|
1693 |
+
|
1694 |
+
@property
|
1695 |
+
def radius(self):
|
1696 |
+
"""The radius of the circle.
|
1697 |
+
|
1698 |
+
Returns
|
1699 |
+
=======
|
1700 |
+
|
1701 |
+
radius : number or SymPy expression
|
1702 |
+
|
1703 |
+
See Also
|
1704 |
+
========
|
1705 |
+
|
1706 |
+
Ellipse.major, Ellipse.minor, Ellipse.hradius, Ellipse.vradius
|
1707 |
+
|
1708 |
+
Examples
|
1709 |
+
========
|
1710 |
+
|
1711 |
+
>>> from sympy import Point, Circle
|
1712 |
+
>>> c1 = Circle(Point(3, 4), 6)
|
1713 |
+
>>> c1.radius
|
1714 |
+
6
|
1715 |
+
|
1716 |
+
"""
|
1717 |
+
return self.args[1]
|
1718 |
+
|
1719 |
+
def reflect(self, line):
|
1720 |
+
"""Override GeometryEntity.reflect since the radius
|
1721 |
+
is not a GeometryEntity.
|
1722 |
+
|
1723 |
+
Examples
|
1724 |
+
========
|
1725 |
+
|
1726 |
+
>>> from sympy import Circle, Line
|
1727 |
+
>>> Circle((0, 1), 1).reflect(Line((0, 0), (1, 1)))
|
1728 |
+
Circle(Point2D(1, 0), -1)
|
1729 |
+
"""
|
1730 |
+
c = self.center
|
1731 |
+
c = c.reflect(line)
|
1732 |
+
return self.func(c, -self.radius)
|
1733 |
+
|
1734 |
+
def scale(self, x=1, y=1, pt=None):
|
1735 |
+
"""Override GeometryEntity.scale since the radius
|
1736 |
+
is not a GeometryEntity.
|
1737 |
+
|
1738 |
+
Examples
|
1739 |
+
========
|
1740 |
+
|
1741 |
+
>>> from sympy import Circle
|
1742 |
+
>>> Circle((0, 0), 1).scale(2, 2)
|
1743 |
+
Circle(Point2D(0, 0), 2)
|
1744 |
+
>>> Circle((0, 0), 1).scale(2, 4)
|
1745 |
+
Ellipse(Point2D(0, 0), 2, 4)
|
1746 |
+
"""
|
1747 |
+
c = self.center
|
1748 |
+
if pt:
|
1749 |
+
pt = Point(pt, dim=2)
|
1750 |
+
return self.translate(*(-pt).args).scale(x, y).translate(*pt.args)
|
1751 |
+
c = c.scale(x, y)
|
1752 |
+
x, y = [abs(i) for i in (x, y)]
|
1753 |
+
if x == y:
|
1754 |
+
return self.func(c, x*self.radius)
|
1755 |
+
h = v = self.radius
|
1756 |
+
return Ellipse(c, hradius=h*x, vradius=v*y)
|
1757 |
+
|
1758 |
+
@property
|
1759 |
+
def vradius(self):
|
1760 |
+
"""
|
1761 |
+
This Ellipse property is an alias for the Circle's radius.
|
1762 |
+
|
1763 |
+
Whereas hradius, major and minor can use Ellipse's conventions,
|
1764 |
+
the vradius does not exist for a circle. It is always a positive
|
1765 |
+
value in order that the Circle, like Polygons, will have an
|
1766 |
+
area that can be positive or negative as determined by the sign
|
1767 |
+
of the hradius.
|
1768 |
+
|
1769 |
+
Examples
|
1770 |
+
========
|
1771 |
+
|
1772 |
+
>>> from sympy import Point, Circle
|
1773 |
+
>>> c1 = Circle(Point(3, 4), 6)
|
1774 |
+
>>> c1.vradius
|
1775 |
+
6
|
1776 |
+
"""
|
1777 |
+
return abs(self.radius)
|
1778 |
+
|
1779 |
+
|
1780 |
+
from .polygon import Polygon, Triangle
|
env-llmeval/lib/python3.10/site-packages/sympy/geometry/entity.py
ADDED
@@ -0,0 +1,641 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""The definition of the base geometrical entity with attributes common to
|
2 |
+
all derived geometrical entities.
|
3 |
+
|
4 |
+
Contains
|
5 |
+
========
|
6 |
+
|
7 |
+
GeometryEntity
|
8 |
+
GeometricSet
|
9 |
+
|
10 |
+
Notes
|
11 |
+
=====
|
12 |
+
|
13 |
+
A GeometryEntity is any object that has special geometric properties.
|
14 |
+
A GeometrySet is a superclass of any GeometryEntity that can also
|
15 |
+
be viewed as a sympy.sets.Set. In particular, points are the only
|
16 |
+
GeometryEntity not considered a Set.
|
17 |
+
|
18 |
+
Rn is a GeometrySet representing n-dimensional Euclidean space. R2 and
|
19 |
+
R3 are currently the only ambient spaces implemented.
|
20 |
+
|
21 |
+
"""
|
22 |
+
from __future__ import annotations
|
23 |
+
|
24 |
+
from sympy.core.basic import Basic
|
25 |
+
from sympy.core.containers import Tuple
|
26 |
+
from sympy.core.evalf import EvalfMixin, N
|
27 |
+
from sympy.core.numbers import oo
|
28 |
+
from sympy.core.symbol import Dummy
|
29 |
+
from sympy.core.sympify import sympify
|
30 |
+
from sympy.functions.elementary.trigonometric import cos, sin, atan
|
31 |
+
from sympy.matrices import eye
|
32 |
+
from sympy.multipledispatch import dispatch
|
33 |
+
from sympy.printing import sstr
|
34 |
+
from sympy.sets import Set, Union, FiniteSet
|
35 |
+
from sympy.sets.handlers.intersection import intersection_sets
|
36 |
+
from sympy.sets.handlers.union import union_sets
|
37 |
+
from sympy.solvers.solvers import solve
|
38 |
+
from sympy.utilities.misc import func_name
|
39 |
+
from sympy.utilities.iterables import is_sequence
|
40 |
+
|
41 |
+
|
42 |
+
# How entities are ordered; used by __cmp__ in GeometryEntity
|
43 |
+
ordering_of_classes = [
|
44 |
+
"Point2D",
|
45 |
+
"Point3D",
|
46 |
+
"Point",
|
47 |
+
"Segment2D",
|
48 |
+
"Ray2D",
|
49 |
+
"Line2D",
|
50 |
+
"Segment3D",
|
51 |
+
"Line3D",
|
52 |
+
"Ray3D",
|
53 |
+
"Segment",
|
54 |
+
"Ray",
|
55 |
+
"Line",
|
56 |
+
"Plane",
|
57 |
+
"Triangle",
|
58 |
+
"RegularPolygon",
|
59 |
+
"Polygon",
|
60 |
+
"Circle",
|
61 |
+
"Ellipse",
|
62 |
+
"Curve",
|
63 |
+
"Parabola"
|
64 |
+
]
|
65 |
+
|
66 |
+
|
67 |
+
x, y = [Dummy('entity_dummy') for i in range(2)]
|
68 |
+
T = Dummy('entity_dummy', real=True)
|
69 |
+
|
70 |
+
|
71 |
+
class GeometryEntity(Basic, EvalfMixin):
|
72 |
+
"""The base class for all geometrical entities.
|
73 |
+
|
74 |
+
This class does not represent any particular geometric entity, it only
|
75 |
+
provides the implementation of some methods common to all subclasses.
|
76 |
+
|
77 |
+
"""
|
78 |
+
|
79 |
+
__slots__: tuple[str, ...] = ()
|
80 |
+
|
81 |
+
def __cmp__(self, other):
|
82 |
+
"""Comparison of two GeometryEntities."""
|
83 |
+
n1 = self.__class__.__name__
|
84 |
+
n2 = other.__class__.__name__
|
85 |
+
c = (n1 > n2) - (n1 < n2)
|
86 |
+
if not c:
|
87 |
+
return 0
|
88 |
+
|
89 |
+
i1 = -1
|
90 |
+
for cls in self.__class__.__mro__:
|
91 |
+
try:
|
92 |
+
i1 = ordering_of_classes.index(cls.__name__)
|
93 |
+
break
|
94 |
+
except ValueError:
|
95 |
+
i1 = -1
|
96 |
+
if i1 == -1:
|
97 |
+
return c
|
98 |
+
|
99 |
+
i2 = -1
|
100 |
+
for cls in other.__class__.__mro__:
|
101 |
+
try:
|
102 |
+
i2 = ordering_of_classes.index(cls.__name__)
|
103 |
+
break
|
104 |
+
except ValueError:
|
105 |
+
i2 = -1
|
106 |
+
if i2 == -1:
|
107 |
+
return c
|
108 |
+
|
109 |
+
return (i1 > i2) - (i1 < i2)
|
110 |
+
|
111 |
+
def __contains__(self, other):
|
112 |
+
"""Subclasses should implement this method for anything more complex than equality."""
|
113 |
+
if type(self) is type(other):
|
114 |
+
return self == other
|
115 |
+
raise NotImplementedError()
|
116 |
+
|
117 |
+
def __getnewargs__(self):
|
118 |
+
"""Returns a tuple that will be passed to __new__ on unpickling."""
|
119 |
+
return tuple(self.args)
|
120 |
+
|
121 |
+
def __ne__(self, o):
|
122 |
+
"""Test inequality of two geometrical entities."""
|
123 |
+
return not self == o
|
124 |
+
|
125 |
+
def __new__(cls, *args, **kwargs):
|
126 |
+
# Points are sequences, but they should not
|
127 |
+
# be converted to Tuples, so use this detection function instead.
|
128 |
+
def is_seq_and_not_point(a):
|
129 |
+
# we cannot use isinstance(a, Point) since we cannot import Point
|
130 |
+
if hasattr(a, 'is_Point') and a.is_Point:
|
131 |
+
return False
|
132 |
+
return is_sequence(a)
|
133 |
+
|
134 |
+
args = [Tuple(*a) if is_seq_and_not_point(a) else sympify(a) for a in args]
|
135 |
+
return Basic.__new__(cls, *args)
|
136 |
+
|
137 |
+
def __radd__(self, a):
|
138 |
+
"""Implementation of reverse add method."""
|
139 |
+
return a.__add__(self)
|
140 |
+
|
141 |
+
def __rtruediv__(self, a):
|
142 |
+
"""Implementation of reverse division method."""
|
143 |
+
return a.__truediv__(self)
|
144 |
+
|
145 |
+
def __repr__(self):
|
146 |
+
"""String representation of a GeometryEntity that can be evaluated
|
147 |
+
by sympy."""
|
148 |
+
return type(self).__name__ + repr(self.args)
|
149 |
+
|
150 |
+
def __rmul__(self, a):
|
151 |
+
"""Implementation of reverse multiplication method."""
|
152 |
+
return a.__mul__(self)
|
153 |
+
|
154 |
+
def __rsub__(self, a):
|
155 |
+
"""Implementation of reverse subtraction method."""
|
156 |
+
return a.__sub__(self)
|
157 |
+
|
158 |
+
def __str__(self):
|
159 |
+
"""String representation of a GeometryEntity."""
|
160 |
+
return type(self).__name__ + sstr(self.args)
|
161 |
+
|
162 |
+
def _eval_subs(self, old, new):
|
163 |
+
from sympy.geometry.point import Point, Point3D
|
164 |
+
if is_sequence(old) or is_sequence(new):
|
165 |
+
if isinstance(self, Point3D):
|
166 |
+
old = Point3D(old)
|
167 |
+
new = Point3D(new)
|
168 |
+
else:
|
169 |
+
old = Point(old)
|
170 |
+
new = Point(new)
|
171 |
+
return self._subs(old, new)
|
172 |
+
|
173 |
+
def _repr_svg_(self):
|
174 |
+
"""SVG representation of a GeometryEntity suitable for IPython"""
|
175 |
+
|
176 |
+
try:
|
177 |
+
bounds = self.bounds
|
178 |
+
except (NotImplementedError, TypeError):
|
179 |
+
# if we have no SVG representation, return None so IPython
|
180 |
+
# will fall back to the next representation
|
181 |
+
return None
|
182 |
+
|
183 |
+
if not all(x.is_number and x.is_finite for x in bounds):
|
184 |
+
return None
|
185 |
+
|
186 |
+
svg_top = '''<svg xmlns="http://www.w3.org/2000/svg"
|
187 |
+
xmlns:xlink="http://www.w3.org/1999/xlink"
|
188 |
+
width="{1}" height="{2}" viewBox="{0}"
|
189 |
+
preserveAspectRatio="xMinYMin meet">
|
190 |
+
<defs>
|
191 |
+
<marker id="markerCircle" markerWidth="8" markerHeight="8"
|
192 |
+
refx="5" refy="5" markerUnits="strokeWidth">
|
193 |
+
<circle cx="5" cy="5" r="1.5" style="stroke: none; fill:#000000;"/>
|
194 |
+
</marker>
|
195 |
+
<marker id="markerArrow" markerWidth="13" markerHeight="13" refx="2" refy="4"
|
196 |
+
orient="auto" markerUnits="strokeWidth">
|
197 |
+
<path d="M2,2 L2,6 L6,4" style="fill: #000000;" />
|
198 |
+
</marker>
|
199 |
+
<marker id="markerReverseArrow" markerWidth="13" markerHeight="13" refx="6" refy="4"
|
200 |
+
orient="auto" markerUnits="strokeWidth">
|
201 |
+
<path d="M6,2 L6,6 L2,4" style="fill: #000000;" />
|
202 |
+
</marker>
|
203 |
+
</defs>'''
|
204 |
+
|
205 |
+
# Establish SVG canvas that will fit all the data + small space
|
206 |
+
xmin, ymin, xmax, ymax = map(N, bounds)
|
207 |
+
if xmin == xmax and ymin == ymax:
|
208 |
+
# This is a point; buffer using an arbitrary size
|
209 |
+
xmin, ymin, xmax, ymax = xmin - .5, ymin -.5, xmax + .5, ymax + .5
|
210 |
+
else:
|
211 |
+
# Expand bounds by a fraction of the data ranges
|
212 |
+
expand = 0.1 # or 10%; this keeps arrowheads in view (R plots use 4%)
|
213 |
+
widest_part = max([xmax - xmin, ymax - ymin])
|
214 |
+
expand_amount = widest_part * expand
|
215 |
+
xmin -= expand_amount
|
216 |
+
ymin -= expand_amount
|
217 |
+
xmax += expand_amount
|
218 |
+
ymax += expand_amount
|
219 |
+
dx = xmax - xmin
|
220 |
+
dy = ymax - ymin
|
221 |
+
width = min([max([100., dx]), 300])
|
222 |
+
height = min([max([100., dy]), 300])
|
223 |
+
|
224 |
+
scale_factor = 1. if max(width, height) == 0 else max(dx, dy) / max(width, height)
|
225 |
+
try:
|
226 |
+
svg = self._svg(scale_factor)
|
227 |
+
except (NotImplementedError, TypeError):
|
228 |
+
# if we have no SVG representation, return None so IPython
|
229 |
+
# will fall back to the next representation
|
230 |
+
return None
|
231 |
+
|
232 |
+
view_box = "{} {} {} {}".format(xmin, ymin, dx, dy)
|
233 |
+
transform = "matrix(1,0,0,-1,0,{})".format(ymax + ymin)
|
234 |
+
svg_top = svg_top.format(view_box, width, height)
|
235 |
+
|
236 |
+
return svg_top + (
|
237 |
+
'<g transform="{}">{}</g></svg>'
|
238 |
+
).format(transform, svg)
|
239 |
+
|
240 |
+
def _svg(self, scale_factor=1., fill_color="#66cc99"):
|
241 |
+
"""Returns SVG path element for the GeometryEntity.
|
242 |
+
|
243 |
+
Parameters
|
244 |
+
==========
|
245 |
+
|
246 |
+
scale_factor : float
|
247 |
+
Multiplication factor for the SVG stroke-width. Default is 1.
|
248 |
+
fill_color : str, optional
|
249 |
+
Hex string for fill color. Default is "#66cc99".
|
250 |
+
"""
|
251 |
+
raise NotImplementedError()
|
252 |
+
|
253 |
+
def _sympy_(self):
|
254 |
+
return self
|
255 |
+
|
256 |
+
@property
|
257 |
+
def ambient_dimension(self):
|
258 |
+
"""What is the dimension of the space that the object is contained in?"""
|
259 |
+
raise NotImplementedError()
|
260 |
+
|
261 |
+
@property
|
262 |
+
def bounds(self):
|
263 |
+
"""Return a tuple (xmin, ymin, xmax, ymax) representing the bounding
|
264 |
+
rectangle for the geometric figure.
|
265 |
+
|
266 |
+
"""
|
267 |
+
|
268 |
+
raise NotImplementedError()
|
269 |
+
|
270 |
+
def encloses(self, o):
|
271 |
+
"""
|
272 |
+
Return True if o is inside (not on or outside) the boundaries of self.
|
273 |
+
|
274 |
+
The object will be decomposed into Points and individual Entities need
|
275 |
+
only define an encloses_point method for their class.
|
276 |
+
|
277 |
+
See Also
|
278 |
+
========
|
279 |
+
|
280 |
+
sympy.geometry.ellipse.Ellipse.encloses_point
|
281 |
+
sympy.geometry.polygon.Polygon.encloses_point
|
282 |
+
|
283 |
+
Examples
|
284 |
+
========
|
285 |
+
|
286 |
+
>>> from sympy import RegularPolygon, Point, Polygon
|
287 |
+
>>> t = Polygon(*RegularPolygon(Point(0, 0), 1, 3).vertices)
|
288 |
+
>>> t2 = Polygon(*RegularPolygon(Point(0, 0), 2, 3).vertices)
|
289 |
+
>>> t2.encloses(t)
|
290 |
+
True
|
291 |
+
>>> t.encloses(t2)
|
292 |
+
False
|
293 |
+
|
294 |
+
"""
|
295 |
+
|
296 |
+
from sympy.geometry.point import Point
|
297 |
+
from sympy.geometry.line import Segment, Ray, Line
|
298 |
+
from sympy.geometry.ellipse import Ellipse
|
299 |
+
from sympy.geometry.polygon import Polygon, RegularPolygon
|
300 |
+
|
301 |
+
if isinstance(o, Point):
|
302 |
+
return self.encloses_point(o)
|
303 |
+
elif isinstance(o, Segment):
|
304 |
+
return all(self.encloses_point(x) for x in o.points)
|
305 |
+
elif isinstance(o, (Ray, Line)):
|
306 |
+
return False
|
307 |
+
elif isinstance(o, Ellipse):
|
308 |
+
return self.encloses_point(o.center) and \
|
309 |
+
self.encloses_point(
|
310 |
+
Point(o.center.x + o.hradius, o.center.y)) and \
|
311 |
+
not self.intersection(o)
|
312 |
+
elif isinstance(o, Polygon):
|
313 |
+
if isinstance(o, RegularPolygon):
|
314 |
+
if not self.encloses_point(o.center):
|
315 |
+
return False
|
316 |
+
return all(self.encloses_point(v) for v in o.vertices)
|
317 |
+
raise NotImplementedError()
|
318 |
+
|
319 |
+
def equals(self, o):
|
320 |
+
return self == o
|
321 |
+
|
322 |
+
def intersection(self, o):
|
323 |
+
"""
|
324 |
+
Returns a list of all of the intersections of self with o.
|
325 |
+
|
326 |
+
Notes
|
327 |
+
=====
|
328 |
+
|
329 |
+
An entity is not required to implement this method.
|
330 |
+
|
331 |
+
If two different types of entities can intersect, the item with
|
332 |
+
higher index in ordering_of_classes should implement
|
333 |
+
intersections with anything having a lower index.
|
334 |
+
|
335 |
+
See Also
|
336 |
+
========
|
337 |
+
|
338 |
+
sympy.geometry.util.intersection
|
339 |
+
|
340 |
+
"""
|
341 |
+
raise NotImplementedError()
|
342 |
+
|
343 |
+
def is_similar(self, other):
|
344 |
+
"""Is this geometrical entity similar to another geometrical entity?
|
345 |
+
|
346 |
+
Two entities are similar if a uniform scaling (enlarging or
|
347 |
+
shrinking) of one of the entities will allow one to obtain the other.
|
348 |
+
|
349 |
+
Notes
|
350 |
+
=====
|
351 |
+
|
352 |
+
This method is not intended to be used directly but rather
|
353 |
+
through the `are_similar` function found in util.py.
|
354 |
+
An entity is not required to implement this method.
|
355 |
+
If two different types of entities can be similar, it is only
|
356 |
+
required that one of them be able to determine this.
|
357 |
+
|
358 |
+
See Also
|
359 |
+
========
|
360 |
+
|
361 |
+
scale
|
362 |
+
|
363 |
+
"""
|
364 |
+
raise NotImplementedError()
|
365 |
+
|
366 |
+
def reflect(self, line):
|
367 |
+
"""
|
368 |
+
Reflects an object across a line.
|
369 |
+
|
370 |
+
Parameters
|
371 |
+
==========
|
372 |
+
|
373 |
+
line: Line
|
374 |
+
|
375 |
+
Examples
|
376 |
+
========
|
377 |
+
|
378 |
+
>>> from sympy import pi, sqrt, Line, RegularPolygon
|
379 |
+
>>> l = Line((0, pi), slope=sqrt(2))
|
380 |
+
>>> pent = RegularPolygon((1, 2), 1, 5)
|
381 |
+
>>> rpent = pent.reflect(l)
|
382 |
+
>>> rpent
|
383 |
+
RegularPolygon(Point2D(-2*sqrt(2)*pi/3 - 1/3 + 4*sqrt(2)/3, 2/3 + 2*sqrt(2)/3 + 2*pi/3), -1, 5, -atan(2*sqrt(2)) + 3*pi/5)
|
384 |
+
|
385 |
+
>>> from sympy import pi, Line, Circle, Point
|
386 |
+
>>> l = Line((0, pi), slope=1)
|
387 |
+
>>> circ = Circle(Point(0, 0), 5)
|
388 |
+
>>> rcirc = circ.reflect(l)
|
389 |
+
>>> rcirc
|
390 |
+
Circle(Point2D(-pi, pi), -5)
|
391 |
+
|
392 |
+
"""
|
393 |
+
from sympy.geometry.point import Point
|
394 |
+
|
395 |
+
g = self
|
396 |
+
l = line
|
397 |
+
o = Point(0, 0)
|
398 |
+
if l.slope.is_zero:
|
399 |
+
v = l.args[0].y
|
400 |
+
if not v: # x-axis
|
401 |
+
return g.scale(y=-1)
|
402 |
+
reps = [(p, p.translate(y=2*(v - p.y))) for p in g.atoms(Point)]
|
403 |
+
elif l.slope is oo:
|
404 |
+
v = l.args[0].x
|
405 |
+
if not v: # y-axis
|
406 |
+
return g.scale(x=-1)
|
407 |
+
reps = [(p, p.translate(x=2*(v - p.x))) for p in g.atoms(Point)]
|
408 |
+
else:
|
409 |
+
if not hasattr(g, 'reflect') and not all(
|
410 |
+
isinstance(arg, Point) for arg in g.args):
|
411 |
+
raise NotImplementedError(
|
412 |
+
'reflect undefined or non-Point args in %s' % g)
|
413 |
+
a = atan(l.slope)
|
414 |
+
c = l.coefficients
|
415 |
+
d = -c[-1]/c[1] # y-intercept
|
416 |
+
# apply the transform to a single point
|
417 |
+
xf = Point(x, y)
|
418 |
+
xf = xf.translate(y=-d).rotate(-a, o).scale(y=-1
|
419 |
+
).rotate(a, o).translate(y=d)
|
420 |
+
# replace every point using that transform
|
421 |
+
reps = [(p, xf.xreplace({x: p.x, y: p.y})) for p in g.atoms(Point)]
|
422 |
+
return g.xreplace(dict(reps))
|
423 |
+
|
424 |
+
def rotate(self, angle, pt=None):
|
425 |
+
"""Rotate ``angle`` radians counterclockwise about Point ``pt``.
|
426 |
+
|
427 |
+
The default pt is the origin, Point(0, 0)
|
428 |
+
|
429 |
+
See Also
|
430 |
+
========
|
431 |
+
|
432 |
+
scale, translate
|
433 |
+
|
434 |
+
Examples
|
435 |
+
========
|
436 |
+
|
437 |
+
>>> from sympy import Point, RegularPolygon, Polygon, pi
|
438 |
+
>>> t = Polygon(*RegularPolygon(Point(0, 0), 1, 3).vertices)
|
439 |
+
>>> t # vertex on x axis
|
440 |
+
Triangle(Point2D(1, 0), Point2D(-1/2, sqrt(3)/2), Point2D(-1/2, -sqrt(3)/2))
|
441 |
+
>>> t.rotate(pi/2) # vertex on y axis now
|
442 |
+
Triangle(Point2D(0, 1), Point2D(-sqrt(3)/2, -1/2), Point2D(sqrt(3)/2, -1/2))
|
443 |
+
|
444 |
+
"""
|
445 |
+
newargs = []
|
446 |
+
for a in self.args:
|
447 |
+
if isinstance(a, GeometryEntity):
|
448 |
+
newargs.append(a.rotate(angle, pt))
|
449 |
+
else:
|
450 |
+
newargs.append(a)
|
451 |
+
return type(self)(*newargs)
|
452 |
+
|
453 |
+
def scale(self, x=1, y=1, pt=None):
|
454 |
+
"""Scale the object by multiplying the x,y-coordinates by x and y.
|
455 |
+
|
456 |
+
If pt is given, the scaling is done relative to that point; the
|
457 |
+
object is shifted by -pt, scaled, and shifted by pt.
|
458 |
+
|
459 |
+
See Also
|
460 |
+
========
|
461 |
+
|
462 |
+
rotate, translate
|
463 |
+
|
464 |
+
Examples
|
465 |
+
========
|
466 |
+
|
467 |
+
>>> from sympy import RegularPolygon, Point, Polygon
|
468 |
+
>>> t = Polygon(*RegularPolygon(Point(0, 0), 1, 3).vertices)
|
469 |
+
>>> t
|
470 |
+
Triangle(Point2D(1, 0), Point2D(-1/2, sqrt(3)/2), Point2D(-1/2, -sqrt(3)/2))
|
471 |
+
>>> t.scale(2)
|
472 |
+
Triangle(Point2D(2, 0), Point2D(-1, sqrt(3)/2), Point2D(-1, -sqrt(3)/2))
|
473 |
+
>>> t.scale(2, 2)
|
474 |
+
Triangle(Point2D(2, 0), Point2D(-1, sqrt(3)), Point2D(-1, -sqrt(3)))
|
475 |
+
|
476 |
+
"""
|
477 |
+
from sympy.geometry.point import Point
|
478 |
+
if pt:
|
479 |
+
pt = Point(pt, dim=2)
|
480 |
+
return self.translate(*(-pt).args).scale(x, y).translate(*pt.args)
|
481 |
+
return type(self)(*[a.scale(x, y) for a in self.args]) # if this fails, override this class
|
482 |
+
|
483 |
+
def translate(self, x=0, y=0):
|
484 |
+
"""Shift the object by adding to the x,y-coordinates the values x and y.
|
485 |
+
|
486 |
+
See Also
|
487 |
+
========
|
488 |
+
|
489 |
+
rotate, scale
|
490 |
+
|
491 |
+
Examples
|
492 |
+
========
|
493 |
+
|
494 |
+
>>> from sympy import RegularPolygon, Point, Polygon
|
495 |
+
>>> t = Polygon(*RegularPolygon(Point(0, 0), 1, 3).vertices)
|
496 |
+
>>> t
|
497 |
+
Triangle(Point2D(1, 0), Point2D(-1/2, sqrt(3)/2), Point2D(-1/2, -sqrt(3)/2))
|
498 |
+
>>> t.translate(2)
|
499 |
+
Triangle(Point2D(3, 0), Point2D(3/2, sqrt(3)/2), Point2D(3/2, -sqrt(3)/2))
|
500 |
+
>>> t.translate(2, 2)
|
501 |
+
Triangle(Point2D(3, 2), Point2D(3/2, sqrt(3)/2 + 2), Point2D(3/2, 2 - sqrt(3)/2))
|
502 |
+
|
503 |
+
"""
|
504 |
+
newargs = []
|
505 |
+
for a in self.args:
|
506 |
+
if isinstance(a, GeometryEntity):
|
507 |
+
newargs.append(a.translate(x, y))
|
508 |
+
else:
|
509 |
+
newargs.append(a)
|
510 |
+
return self.func(*newargs)
|
511 |
+
|
512 |
+
def parameter_value(self, other, t):
|
513 |
+
"""Return the parameter corresponding to the given point.
|
514 |
+
Evaluating an arbitrary point of the entity at this parameter
|
515 |
+
value will return the given point.
|
516 |
+
|
517 |
+
Examples
|
518 |
+
========
|
519 |
+
|
520 |
+
>>> from sympy import Line, Point
|
521 |
+
>>> from sympy.abc import t
|
522 |
+
>>> a = Point(0, 0)
|
523 |
+
>>> b = Point(2, 2)
|
524 |
+
>>> Line(a, b).parameter_value((1, 1), t)
|
525 |
+
{t: 1/2}
|
526 |
+
>>> Line(a, b).arbitrary_point(t).subs(_)
|
527 |
+
Point2D(1, 1)
|
528 |
+
"""
|
529 |
+
from sympy.geometry.point import Point
|
530 |
+
if not isinstance(other, GeometryEntity):
|
531 |
+
other = Point(other, dim=self.ambient_dimension)
|
532 |
+
if not isinstance(other, Point):
|
533 |
+
raise ValueError("other must be a point")
|
534 |
+
sol = solve(self.arbitrary_point(T) - other, T, dict=True)
|
535 |
+
if not sol:
|
536 |
+
raise ValueError("Given point is not on %s" % func_name(self))
|
537 |
+
return {t: sol[0][T]}
|
538 |
+
|
539 |
+
|
540 |
+
class GeometrySet(GeometryEntity, Set):
|
541 |
+
"""Parent class of all GeometryEntity that are also Sets
|
542 |
+
(compatible with sympy.sets)
|
543 |
+
"""
|
544 |
+
__slots__ = ()
|
545 |
+
|
546 |
+
def _contains(self, other):
|
547 |
+
"""sympy.sets uses the _contains method, so include it for compatibility."""
|
548 |
+
|
549 |
+
if isinstance(other, Set) and other.is_FiniteSet:
|
550 |
+
return all(self.__contains__(i) for i in other)
|
551 |
+
|
552 |
+
return self.__contains__(other)
|
553 |
+
|
554 |
+
@dispatch(GeometrySet, Set) # type:ignore # noqa:F811
|
555 |
+
def union_sets(self, o): # noqa:F811
|
556 |
+
""" Returns the union of self and o
|
557 |
+
for use with sympy.sets.Set, if possible. """
|
558 |
+
|
559 |
+
|
560 |
+
# if its a FiniteSet, merge any points
|
561 |
+
# we contain and return a union with the rest
|
562 |
+
if o.is_FiniteSet:
|
563 |
+
other_points = [p for p in o if not self._contains(p)]
|
564 |
+
if len(other_points) == len(o):
|
565 |
+
return None
|
566 |
+
return Union(self, FiniteSet(*other_points))
|
567 |
+
if self._contains(o):
|
568 |
+
return self
|
569 |
+
return None
|
570 |
+
|
571 |
+
|
572 |
+
@dispatch(GeometrySet, Set) # type: ignore # noqa:F811
|
573 |
+
def intersection_sets(self, o): # noqa:F811
|
574 |
+
""" Returns a sympy.sets.Set of intersection objects,
|
575 |
+
if possible. """
|
576 |
+
|
577 |
+
from sympy.geometry.point import Point
|
578 |
+
|
579 |
+
try:
|
580 |
+
# if o is a FiniteSet, find the intersection directly
|
581 |
+
# to avoid infinite recursion
|
582 |
+
if o.is_FiniteSet:
|
583 |
+
inter = FiniteSet(*(p for p in o if self.contains(p)))
|
584 |
+
else:
|
585 |
+
inter = self.intersection(o)
|
586 |
+
except NotImplementedError:
|
587 |
+
# sympy.sets.Set.reduce expects None if an object
|
588 |
+
# doesn't know how to simplify
|
589 |
+
return None
|
590 |
+
|
591 |
+
# put the points in a FiniteSet
|
592 |
+
points = FiniteSet(*[p for p in inter if isinstance(p, Point)])
|
593 |
+
non_points = [p for p in inter if not isinstance(p, Point)]
|
594 |
+
|
595 |
+
return Union(*(non_points + [points]))
|
596 |
+
|
597 |
+
def translate(x, y):
|
598 |
+
"""Return the matrix to translate a 2-D point by x and y."""
|
599 |
+
rv = eye(3)
|
600 |
+
rv[2, 0] = x
|
601 |
+
rv[2, 1] = y
|
602 |
+
return rv
|
603 |
+
|
604 |
+
|
605 |
+
def scale(x, y, pt=None):
|
606 |
+
"""Return the matrix to multiply a 2-D point's coordinates by x and y.
|
607 |
+
|
608 |
+
If pt is given, the scaling is done relative to that point."""
|
609 |
+
rv = eye(3)
|
610 |
+
rv[0, 0] = x
|
611 |
+
rv[1, 1] = y
|
612 |
+
if pt:
|
613 |
+
from sympy.geometry.point import Point
|
614 |
+
pt = Point(pt, dim=2)
|
615 |
+
tr1 = translate(*(-pt).args)
|
616 |
+
tr2 = translate(*pt.args)
|
617 |
+
return tr1*rv*tr2
|
618 |
+
return rv
|
619 |
+
|
620 |
+
|
621 |
+
def rotate(th):
|
622 |
+
"""Return the matrix to rotate a 2-D point about the origin by ``angle``.
|
623 |
+
|
624 |
+
The angle is measured in radians. To Point a point about a point other
|
625 |
+
then the origin, translate the Point, do the rotation, and
|
626 |
+
translate it back:
|
627 |
+
|
628 |
+
>>> from sympy.geometry.entity import rotate, translate
|
629 |
+
>>> from sympy import Point, pi
|
630 |
+
>>> rot_about_11 = translate(-1, -1)*rotate(pi/2)*translate(1, 1)
|
631 |
+
>>> Point(1, 1).transform(rot_about_11)
|
632 |
+
Point2D(1, 1)
|
633 |
+
>>> Point(0, 0).transform(rot_about_11)
|
634 |
+
Point2D(2, 0)
|
635 |
+
"""
|
636 |
+
s = sin(th)
|
637 |
+
rv = eye(3)*cos(th)
|
638 |
+
rv[0, 1] = s
|
639 |
+
rv[1, 0] = -s
|
640 |
+
rv[2, 2] = 1
|
641 |
+
return rv
|
env-llmeval/lib/python3.10/site-packages/sympy/geometry/exceptions.py
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""Geometry Errors."""
|
2 |
+
|
3 |
+
class GeometryError(ValueError):
|
4 |
+
"""An exception raised by classes in the geometry module."""
|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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from sympy.core import Expr, S, oo, pi, sympify
|
2 |
+
from sympy.core.evalf import N
|
3 |
+
from sympy.core.sorting import default_sort_key, ordered
|
4 |
+
from sympy.core.symbol import _symbol, Dummy, Symbol
|
5 |
+
from sympy.functions.elementary.complexes import sign
|
6 |
+
from sympy.functions.elementary.piecewise import Piecewise
|
7 |
+
from sympy.functions.elementary.trigonometric import cos, sin, tan
|
8 |
+
from .ellipse import Circle
|
9 |
+
from .entity import GeometryEntity, GeometrySet
|
10 |
+
from .exceptions import GeometryError
|
11 |
+
from .line import Line, Segment, Ray
|
12 |
+
from .point import Point
|
13 |
+
from sympy.logic import And
|
14 |
+
from sympy.matrices import Matrix
|
15 |
+
from sympy.simplify.simplify import simplify
|
16 |
+
from sympy.solvers.solvers import solve
|
17 |
+
from sympy.utilities.iterables import has_dups, has_variety, uniq, rotate_left, least_rotation
|
18 |
+
from sympy.utilities.misc import as_int, func_name
|
19 |
+
|
20 |
+
from mpmath.libmp.libmpf import prec_to_dps
|
21 |
+
|
22 |
+
import warnings
|
23 |
+
|
24 |
+
|
25 |
+
x, y, T = [Dummy('polygon_dummy', real=True) for i in range(3)]
|
26 |
+
|
27 |
+
|
28 |
+
class Polygon(GeometrySet):
|
29 |
+
"""A two-dimensional polygon.
|
30 |
+
|
31 |
+
A simple polygon in space. Can be constructed from a sequence of points
|
32 |
+
or from a center, radius, number of sides and rotation angle.
|
33 |
+
|
34 |
+
Parameters
|
35 |
+
==========
|
36 |
+
|
37 |
+
vertices
|
38 |
+
A sequence of points.
|
39 |
+
|
40 |
+
n : int, optional
|
41 |
+
If $> 0$, an n-sided RegularPolygon is created.
|
42 |
+
Default value is $0$.
|
43 |
+
|
44 |
+
Attributes
|
45 |
+
==========
|
46 |
+
|
47 |
+
area
|
48 |
+
angles
|
49 |
+
perimeter
|
50 |
+
vertices
|
51 |
+
centroid
|
52 |
+
sides
|
53 |
+
|
54 |
+
Raises
|
55 |
+
======
|
56 |
+
|
57 |
+
GeometryError
|
58 |
+
If all parameters are not Points.
|
59 |
+
|
60 |
+
See Also
|
61 |
+
========
|
62 |
+
|
63 |
+
sympy.geometry.point.Point, sympy.geometry.line.Segment, Triangle
|
64 |
+
|
65 |
+
Notes
|
66 |
+
=====
|
67 |
+
|
68 |
+
Polygons are treated as closed paths rather than 2D areas so
|
69 |
+
some calculations can be be negative or positive (e.g., area)
|
70 |
+
based on the orientation of the points.
|
71 |
+
|
72 |
+
Any consecutive identical points are reduced to a single point
|
73 |
+
and any points collinear and between two points will be removed
|
74 |
+
unless they are needed to define an explicit intersection (see examples).
|
75 |
+
|
76 |
+
A Triangle, Segment or Point will be returned when there are 3 or
|
77 |
+
fewer points provided.
|
78 |
+
|
79 |
+
Examples
|
80 |
+
========
|
81 |
+
|
82 |
+
>>> from sympy import Polygon, pi
|
83 |
+
>>> p1, p2, p3, p4, p5 = [(0, 0), (1, 0), (5, 1), (0, 1), (3, 0)]
|
84 |
+
>>> Polygon(p1, p2, p3, p4)
|
85 |
+
Polygon(Point2D(0, 0), Point2D(1, 0), Point2D(5, 1), Point2D(0, 1))
|
86 |
+
>>> Polygon(p1, p2)
|
87 |
+
Segment2D(Point2D(0, 0), Point2D(1, 0))
|
88 |
+
>>> Polygon(p1, p2, p5)
|
89 |
+
Segment2D(Point2D(0, 0), Point2D(3, 0))
|
90 |
+
|
91 |
+
The area of a polygon is calculated as positive when vertices are
|
92 |
+
traversed in a ccw direction. When the sides of a polygon cross the
|
93 |
+
area will have positive and negative contributions. The following
|
94 |
+
defines a Z shape where the bottom right connects back to the top
|
95 |
+
left.
|
96 |
+
|
97 |
+
>>> Polygon((0, 2), (2, 2), (0, 0), (2, 0)).area
|
98 |
+
0
|
99 |
+
|
100 |
+
When the keyword `n` is used to define the number of sides of the
|
101 |
+
Polygon then a RegularPolygon is created and the other arguments are
|
102 |
+
interpreted as center, radius and rotation. The unrotated RegularPolygon
|
103 |
+
will always have a vertex at Point(r, 0) where `r` is the radius of the
|
104 |
+
circle that circumscribes the RegularPolygon. Its method `spin` can be
|
105 |
+
used to increment that angle.
|
106 |
+
|
107 |
+
>>> p = Polygon((0,0), 1, n=3)
|
108 |
+
>>> p
|
109 |
+
RegularPolygon(Point2D(0, 0), 1, 3, 0)
|
110 |
+
>>> p.vertices[0]
|
111 |
+
Point2D(1, 0)
|
112 |
+
>>> p.args[0]
|
113 |
+
Point2D(0, 0)
|
114 |
+
>>> p.spin(pi/2)
|
115 |
+
>>> p.vertices[0]
|
116 |
+
Point2D(0, 1)
|
117 |
+
|
118 |
+
"""
|
119 |
+
|
120 |
+
__slots__ = ()
|
121 |
+
|
122 |
+
def __new__(cls, *args, n = 0, **kwargs):
|
123 |
+
if n:
|
124 |
+
args = list(args)
|
125 |
+
# return a virtual polygon with n sides
|
126 |
+
if len(args) == 2: # center, radius
|
127 |
+
args.append(n)
|
128 |
+
elif len(args) == 3: # center, radius, rotation
|
129 |
+
args.insert(2, n)
|
130 |
+
return RegularPolygon(*args, **kwargs)
|
131 |
+
|
132 |
+
vertices = [Point(a, dim=2, **kwargs) for a in args]
|
133 |
+
|
134 |
+
# remove consecutive duplicates
|
135 |
+
nodup = []
|
136 |
+
for p in vertices:
|
137 |
+
if nodup and p == nodup[-1]:
|
138 |
+
continue
|
139 |
+
nodup.append(p)
|
140 |
+
if len(nodup) > 1 and nodup[-1] == nodup[0]:
|
141 |
+
nodup.pop() # last point was same as first
|
142 |
+
|
143 |
+
# remove collinear points
|
144 |
+
i = -3
|
145 |
+
while i < len(nodup) - 3 and len(nodup) > 2:
|
146 |
+
a, b, c = nodup[i], nodup[i + 1], nodup[i + 2]
|
147 |
+
if Point.is_collinear(a, b, c):
|
148 |
+
nodup.pop(i + 1)
|
149 |
+
if a == c:
|
150 |
+
nodup.pop(i)
|
151 |
+
else:
|
152 |
+
i += 1
|
153 |
+
|
154 |
+
vertices = list(nodup)
|
155 |
+
|
156 |
+
if len(vertices) > 3:
|
157 |
+
return GeometryEntity.__new__(cls, *vertices, **kwargs)
|
158 |
+
elif len(vertices) == 3:
|
159 |
+
return Triangle(*vertices, **kwargs)
|
160 |
+
elif len(vertices) == 2:
|
161 |
+
return Segment(*vertices, **kwargs)
|
162 |
+
else:
|
163 |
+
return Point(*vertices, **kwargs)
|
164 |
+
|
165 |
+
@property
|
166 |
+
def area(self):
|
167 |
+
"""
|
168 |
+
The area of the polygon.
|
169 |
+
|
170 |
+
Notes
|
171 |
+
=====
|
172 |
+
|
173 |
+
The area calculation can be positive or negative based on the
|
174 |
+
orientation of the points. If any side of the polygon crosses
|
175 |
+
any other side, there will be areas having opposite signs.
|
176 |
+
|
177 |
+
See Also
|
178 |
+
========
|
179 |
+
|
180 |
+
sympy.geometry.ellipse.Ellipse.area
|
181 |
+
|
182 |
+
Examples
|
183 |
+
========
|
184 |
+
|
185 |
+
>>> from sympy import Point, Polygon
|
186 |
+
>>> p1, p2, p3, p4 = map(Point, [(0, 0), (1, 0), (5, 1), (0, 1)])
|
187 |
+
>>> poly = Polygon(p1, p2, p3, p4)
|
188 |
+
>>> poly.area
|
189 |
+
3
|
190 |
+
|
191 |
+
In the Z shaped polygon (with the lower right connecting back
|
192 |
+
to the upper left) the areas cancel out:
|
193 |
+
|
194 |
+
>>> Z = Polygon((0, 1), (1, 1), (0, 0), (1, 0))
|
195 |
+
>>> Z.area
|
196 |
+
0
|
197 |
+
|
198 |
+
In the M shaped polygon, areas do not cancel because no side
|
199 |
+
crosses any other (though there is a point of contact).
|
200 |
+
|
201 |
+
>>> M = Polygon((0, 0), (0, 1), (2, 0), (3, 1), (3, 0))
|
202 |
+
>>> M.area
|
203 |
+
-3/2
|
204 |
+
|
205 |
+
"""
|
206 |
+
area = 0
|
207 |
+
args = self.args
|
208 |
+
for i in range(len(args)):
|
209 |
+
x1, y1 = args[i - 1].args
|
210 |
+
x2, y2 = args[i].args
|
211 |
+
area += x1*y2 - x2*y1
|
212 |
+
return simplify(area) / 2
|
213 |
+
|
214 |
+
@staticmethod
|
215 |
+
def _isright(a, b, c):
|
216 |
+
"""Return True/False for cw/ccw orientation.
|
217 |
+
|
218 |
+
Examples
|
219 |
+
========
|
220 |
+
|
221 |
+
>>> from sympy import Point, Polygon
|
222 |
+
>>> a, b, c = [Point(i) for i in [(0, 0), (1, 1), (1, 0)]]
|
223 |
+
>>> Polygon._isright(a, b, c)
|
224 |
+
True
|
225 |
+
>>> Polygon._isright(a, c, b)
|
226 |
+
False
|
227 |
+
"""
|
228 |
+
ba = b - a
|
229 |
+
ca = c - a
|
230 |
+
t_area = simplify(ba.x*ca.y - ca.x*ba.y)
|
231 |
+
res = t_area.is_nonpositive
|
232 |
+
if res is None:
|
233 |
+
raise ValueError("Can't determine orientation")
|
234 |
+
return res
|
235 |
+
|
236 |
+
@property
|
237 |
+
def angles(self):
|
238 |
+
"""The internal angle at each vertex.
|
239 |
+
|
240 |
+
Returns
|
241 |
+
=======
|
242 |
+
|
243 |
+
angles : dict
|
244 |
+
A dictionary where each key is a vertex and each value is the
|
245 |
+
internal angle at that vertex. The vertices are represented as
|
246 |
+
Points.
|
247 |
+
|
248 |
+
See Also
|
249 |
+
========
|
250 |
+
|
251 |
+
sympy.geometry.point.Point, sympy.geometry.line.LinearEntity.angle_between
|
252 |
+
|
253 |
+
Examples
|
254 |
+
========
|
255 |
+
|
256 |
+
>>> from sympy import Point, Polygon
|
257 |
+
>>> p1, p2, p3, p4 = map(Point, [(0, 0), (1, 0), (5, 1), (0, 1)])
|
258 |
+
>>> poly = Polygon(p1, p2, p3, p4)
|
259 |
+
>>> poly.angles[p1]
|
260 |
+
pi/2
|
261 |
+
>>> poly.angles[p2]
|
262 |
+
acos(-4*sqrt(17)/17)
|
263 |
+
|
264 |
+
"""
|
265 |
+
|
266 |
+
# Determine orientation of points
|
267 |
+
args = self.vertices
|
268 |
+
cw = self._isright(args[-1], args[0], args[1])
|
269 |
+
|
270 |
+
ret = {}
|
271 |
+
for i in range(len(args)):
|
272 |
+
a, b, c = args[i - 2], args[i - 1], args[i]
|
273 |
+
ang = Ray(b, a).angle_between(Ray(b, c))
|
274 |
+
if cw ^ self._isright(a, b, c):
|
275 |
+
ret[b] = 2*S.Pi - ang
|
276 |
+
else:
|
277 |
+
ret[b] = ang
|
278 |
+
return ret
|
279 |
+
|
280 |
+
@property
|
281 |
+
def ambient_dimension(self):
|
282 |
+
return self.vertices[0].ambient_dimension
|
283 |
+
|
284 |
+
@property
|
285 |
+
def perimeter(self):
|
286 |
+
"""The perimeter of the polygon.
|
287 |
+
|
288 |
+
Returns
|
289 |
+
=======
|
290 |
+
|
291 |
+
perimeter : number or Basic instance
|
292 |
+
|
293 |
+
See Also
|
294 |
+
========
|
295 |
+
|
296 |
+
sympy.geometry.line.Segment.length
|
297 |
+
|
298 |
+
Examples
|
299 |
+
========
|
300 |
+
|
301 |
+
>>> from sympy import Point, Polygon
|
302 |
+
>>> p1, p2, p3, p4 = map(Point, [(0, 0), (1, 0), (5, 1), (0, 1)])
|
303 |
+
>>> poly = Polygon(p1, p2, p3, p4)
|
304 |
+
>>> poly.perimeter
|
305 |
+
sqrt(17) + 7
|
306 |
+
"""
|
307 |
+
p = 0
|
308 |
+
args = self.vertices
|
309 |
+
for i in range(len(args)):
|
310 |
+
p += args[i - 1].distance(args[i])
|
311 |
+
return simplify(p)
|
312 |
+
|
313 |
+
@property
|
314 |
+
def vertices(self):
|
315 |
+
"""The vertices of the polygon.
|
316 |
+
|
317 |
+
Returns
|
318 |
+
=======
|
319 |
+
|
320 |
+
vertices : list of Points
|
321 |
+
|
322 |
+
Notes
|
323 |
+
=====
|
324 |
+
|
325 |
+
When iterating over the vertices, it is more efficient to index self
|
326 |
+
rather than to request the vertices and index them. Only use the
|
327 |
+
vertices when you want to process all of them at once. This is even
|
328 |
+
more important with RegularPolygons that calculate each vertex.
|
329 |
+
|
330 |
+
See Also
|
331 |
+
========
|
332 |
+
|
333 |
+
sympy.geometry.point.Point
|
334 |
+
|
335 |
+
Examples
|
336 |
+
========
|
337 |
+
|
338 |
+
>>> from sympy import Point, Polygon
|
339 |
+
>>> p1, p2, p3, p4 = map(Point, [(0, 0), (1, 0), (5, 1), (0, 1)])
|
340 |
+
>>> poly = Polygon(p1, p2, p3, p4)
|
341 |
+
>>> poly.vertices
|
342 |
+
[Point2D(0, 0), Point2D(1, 0), Point2D(5, 1), Point2D(0, 1)]
|
343 |
+
>>> poly.vertices[0]
|
344 |
+
Point2D(0, 0)
|
345 |
+
|
346 |
+
"""
|
347 |
+
return list(self.args)
|
348 |
+
|
349 |
+
@property
|
350 |
+
def centroid(self):
|
351 |
+
"""The centroid of the polygon.
|
352 |
+
|
353 |
+
Returns
|
354 |
+
=======
|
355 |
+
|
356 |
+
centroid : Point
|
357 |
+
|
358 |
+
See Also
|
359 |
+
========
|
360 |
+
|
361 |
+
sympy.geometry.point.Point, sympy.geometry.util.centroid
|
362 |
+
|
363 |
+
Examples
|
364 |
+
========
|
365 |
+
|
366 |
+
>>> from sympy import Point, Polygon
|
367 |
+
>>> p1, p2, p3, p4 = map(Point, [(0, 0), (1, 0), (5, 1), (0, 1)])
|
368 |
+
>>> poly = Polygon(p1, p2, p3, p4)
|
369 |
+
>>> poly.centroid
|
370 |
+
Point2D(31/18, 11/18)
|
371 |
+
|
372 |
+
"""
|
373 |
+
A = 1/(6*self.area)
|
374 |
+
cx, cy = 0, 0
|
375 |
+
args = self.args
|
376 |
+
for i in range(len(args)):
|
377 |
+
x1, y1 = args[i - 1].args
|
378 |
+
x2, y2 = args[i].args
|
379 |
+
v = x1*y2 - x2*y1
|
380 |
+
cx += v*(x1 + x2)
|
381 |
+
cy += v*(y1 + y2)
|
382 |
+
return Point(simplify(A*cx), simplify(A*cy))
|
383 |
+
|
384 |
+
|
385 |
+
def second_moment_of_area(self, point=None):
|
386 |
+
"""Returns the second moment and product moment of area of a two dimensional polygon.
|
387 |
+
|
388 |
+
Parameters
|
389 |
+
==========
|
390 |
+
|
391 |
+
point : Point, two-tuple of sympifyable objects, or None(default=None)
|
392 |
+
point is the point about which second moment of area is to be found.
|
393 |
+
If "point=None" it will be calculated about the axis passing through the
|
394 |
+
centroid of the polygon.
|
395 |
+
|
396 |
+
Returns
|
397 |
+
=======
|
398 |
+
|
399 |
+
I_xx, I_yy, I_xy : number or SymPy expression
|
400 |
+
I_xx, I_yy are second moment of area of a two dimensional polygon.
|
401 |
+
I_xy is product moment of area of a two dimensional polygon.
|
402 |
+
|
403 |
+
Examples
|
404 |
+
========
|
405 |
+
|
406 |
+
>>> from sympy import Polygon, symbols
|
407 |
+
>>> a, b = symbols('a, b')
|
408 |
+
>>> p1, p2, p3, p4, p5 = [(0, 0), (a, 0), (a, b), (0, b), (a/3, b/3)]
|
409 |
+
>>> rectangle = Polygon(p1, p2, p3, p4)
|
410 |
+
>>> rectangle.second_moment_of_area()
|
411 |
+
(a*b**3/12, a**3*b/12, 0)
|
412 |
+
>>> rectangle.second_moment_of_area(p5)
|
413 |
+
(a*b**3/9, a**3*b/9, a**2*b**2/36)
|
414 |
+
|
415 |
+
References
|
416 |
+
==========
|
417 |
+
|
418 |
+
.. [1] https://en.wikipedia.org/wiki/Second_moment_of_area
|
419 |
+
|
420 |
+
"""
|
421 |
+
|
422 |
+
I_xx, I_yy, I_xy = 0, 0, 0
|
423 |
+
args = self.vertices
|
424 |
+
for i in range(len(args)):
|
425 |
+
x1, y1 = args[i-1].args
|
426 |
+
x2, y2 = args[i].args
|
427 |
+
v = x1*y2 - x2*y1
|
428 |
+
I_xx += (y1**2 + y1*y2 + y2**2)*v
|
429 |
+
I_yy += (x1**2 + x1*x2 + x2**2)*v
|
430 |
+
I_xy += (x1*y2 + 2*x1*y1 + 2*x2*y2 + x2*y1)*v
|
431 |
+
A = self.area
|
432 |
+
c_x = self.centroid[0]
|
433 |
+
c_y = self.centroid[1]
|
434 |
+
# parallel axis theorem
|
435 |
+
I_xx_c = (I_xx/12) - (A*(c_y**2))
|
436 |
+
I_yy_c = (I_yy/12) - (A*(c_x**2))
|
437 |
+
I_xy_c = (I_xy/24) - (A*(c_x*c_y))
|
438 |
+
if point is None:
|
439 |
+
return I_xx_c, I_yy_c, I_xy_c
|
440 |
+
|
441 |
+
I_xx = (I_xx_c + A*((point[1]-c_y)**2))
|
442 |
+
I_yy = (I_yy_c + A*((point[0]-c_x)**2))
|
443 |
+
I_xy = (I_xy_c + A*((point[0]-c_x)*(point[1]-c_y)))
|
444 |
+
|
445 |
+
return I_xx, I_yy, I_xy
|
446 |
+
|
447 |
+
|
448 |
+
def first_moment_of_area(self, point=None):
|
449 |
+
"""
|
450 |
+
Returns the first moment of area of a two-dimensional polygon with
|
451 |
+
respect to a certain point of interest.
|
452 |
+
|
453 |
+
First moment of area is a measure of the distribution of the area
|
454 |
+
of a polygon in relation to an axis. The first moment of area of
|
455 |
+
the entire polygon about its own centroid is always zero. Therefore,
|
456 |
+
here it is calculated for an area, above or below a certain point
|
457 |
+
of interest, that makes up a smaller portion of the polygon. This
|
458 |
+
area is bounded by the point of interest and the extreme end
|
459 |
+
(top or bottom) of the polygon. The first moment for this area is
|
460 |
+
is then determined about the centroidal axis of the initial polygon.
|
461 |
+
|
462 |
+
References
|
463 |
+
==========
|
464 |
+
|
465 |
+
.. [1] https://skyciv.com/docs/tutorials/section-tutorials/calculating-the-statical-or-first-moment-of-area-of-beam-sections/?cc=BMD
|
466 |
+
.. [2] https://mechanicalc.com/reference/cross-sections
|
467 |
+
|
468 |
+
Parameters
|
469 |
+
==========
|
470 |
+
|
471 |
+
point: Point, two-tuple of sympifyable objects, or None (default=None)
|
472 |
+
point is the point above or below which the area of interest lies
|
473 |
+
If ``point=None`` then the centroid acts as the point of interest.
|
474 |
+
|
475 |
+
Returns
|
476 |
+
=======
|
477 |
+
|
478 |
+
Q_x, Q_y: number or SymPy expressions
|
479 |
+
Q_x is the first moment of area about the x-axis
|
480 |
+
Q_y is the first moment of area about the y-axis
|
481 |
+
A negative sign indicates that the section modulus is
|
482 |
+
determined for a section below (or left of) the centroidal axis
|
483 |
+
|
484 |
+
Examples
|
485 |
+
========
|
486 |
+
|
487 |
+
>>> from sympy import Point, Polygon
|
488 |
+
>>> a, b = 50, 10
|
489 |
+
>>> p1, p2, p3, p4 = [(0, b), (0, 0), (a, 0), (a, b)]
|
490 |
+
>>> p = Polygon(p1, p2, p3, p4)
|
491 |
+
>>> p.first_moment_of_area()
|
492 |
+
(625, 3125)
|
493 |
+
>>> p.first_moment_of_area(point=Point(30, 7))
|
494 |
+
(525, 3000)
|
495 |
+
"""
|
496 |
+
if point:
|
497 |
+
xc, yc = self.centroid
|
498 |
+
else:
|
499 |
+
point = self.centroid
|
500 |
+
xc, yc = point
|
501 |
+
|
502 |
+
h_line = Line(point, slope=0)
|
503 |
+
v_line = Line(point, slope=S.Infinity)
|
504 |
+
|
505 |
+
h_poly = self.cut_section(h_line)
|
506 |
+
v_poly = self.cut_section(v_line)
|
507 |
+
|
508 |
+
poly_1 = h_poly[0] if h_poly[0].area <= h_poly[1].area else h_poly[1]
|
509 |
+
poly_2 = v_poly[0] if v_poly[0].area <= v_poly[1].area else v_poly[1]
|
510 |
+
|
511 |
+
Q_x = (poly_1.centroid.y - yc)*poly_1.area
|
512 |
+
Q_y = (poly_2.centroid.x - xc)*poly_2.area
|
513 |
+
|
514 |
+
return Q_x, Q_y
|
515 |
+
|
516 |
+
|
517 |
+
def polar_second_moment_of_area(self):
|
518 |
+
"""Returns the polar modulus of a two-dimensional polygon
|
519 |
+
|
520 |
+
It is a constituent of the second moment of area, linked through
|
521 |
+
the perpendicular axis theorem. While the planar second moment of
|
522 |
+
area describes an object's resistance to deflection (bending) when
|
523 |
+
subjected to a force applied to a plane parallel to the central
|
524 |
+
axis, the polar second moment of area describes an object's
|
525 |
+
resistance to deflection when subjected to a moment applied in a
|
526 |
+
plane perpendicular to the object's central axis (i.e. parallel to
|
527 |
+
the cross-section)
|
528 |
+
|
529 |
+
Examples
|
530 |
+
========
|
531 |
+
|
532 |
+
>>> from sympy import Polygon, symbols
|
533 |
+
>>> a, b = symbols('a, b')
|
534 |
+
>>> rectangle = Polygon((0, 0), (a, 0), (a, b), (0, b))
|
535 |
+
>>> rectangle.polar_second_moment_of_area()
|
536 |
+
a**3*b/12 + a*b**3/12
|
537 |
+
|
538 |
+
References
|
539 |
+
==========
|
540 |
+
|
541 |
+
.. [1] https://en.wikipedia.org/wiki/Polar_moment_of_inertia
|
542 |
+
|
543 |
+
"""
|
544 |
+
second_moment = self.second_moment_of_area()
|
545 |
+
return second_moment[0] + second_moment[1]
|
546 |
+
|
547 |
+
|
548 |
+
def section_modulus(self, point=None):
|
549 |
+
"""Returns a tuple with the section modulus of a two-dimensional
|
550 |
+
polygon.
|
551 |
+
|
552 |
+
Section modulus is a geometric property of a polygon defined as the
|
553 |
+
ratio of second moment of area to the distance of the extreme end of
|
554 |
+
the polygon from the centroidal axis.
|
555 |
+
|
556 |
+
Parameters
|
557 |
+
==========
|
558 |
+
|
559 |
+
point : Point, two-tuple of sympifyable objects, or None(default=None)
|
560 |
+
point is the point at which section modulus is to be found.
|
561 |
+
If "point=None" it will be calculated for the point farthest from the
|
562 |
+
centroidal axis of the polygon.
|
563 |
+
|
564 |
+
Returns
|
565 |
+
=======
|
566 |
+
|
567 |
+
S_x, S_y: numbers or SymPy expressions
|
568 |
+
S_x is the section modulus with respect to the x-axis
|
569 |
+
S_y is the section modulus with respect to the y-axis
|
570 |
+
A negative sign indicates that the section modulus is
|
571 |
+
determined for a point below the centroidal axis
|
572 |
+
|
573 |
+
Examples
|
574 |
+
========
|
575 |
+
|
576 |
+
>>> from sympy import symbols, Polygon, Point
|
577 |
+
>>> a, b = symbols('a, b', positive=True)
|
578 |
+
>>> rectangle = Polygon((0, 0), (a, 0), (a, b), (0, b))
|
579 |
+
>>> rectangle.section_modulus()
|
580 |
+
(a*b**2/6, a**2*b/6)
|
581 |
+
>>> rectangle.section_modulus(Point(a/4, b/4))
|
582 |
+
(-a*b**2/3, -a**2*b/3)
|
583 |
+
|
584 |
+
References
|
585 |
+
==========
|
586 |
+
|
587 |
+
.. [1] https://en.wikipedia.org/wiki/Section_modulus
|
588 |
+
|
589 |
+
"""
|
590 |
+
x_c, y_c = self.centroid
|
591 |
+
if point is None:
|
592 |
+
# taking x and y as maximum distances from centroid
|
593 |
+
x_min, y_min, x_max, y_max = self.bounds
|
594 |
+
y = max(y_c - y_min, y_max - y_c)
|
595 |
+
x = max(x_c - x_min, x_max - x_c)
|
596 |
+
else:
|
597 |
+
# taking x and y as distances of the given point from the centroid
|
598 |
+
y = point.y - y_c
|
599 |
+
x = point.x - x_c
|
600 |
+
|
601 |
+
second_moment= self.second_moment_of_area()
|
602 |
+
S_x = second_moment[0]/y
|
603 |
+
S_y = second_moment[1]/x
|
604 |
+
|
605 |
+
return S_x, S_y
|
606 |
+
|
607 |
+
|
608 |
+
@property
|
609 |
+
def sides(self):
|
610 |
+
"""The directed line segments that form the sides of the polygon.
|
611 |
+
|
612 |
+
Returns
|
613 |
+
=======
|
614 |
+
|
615 |
+
sides : list of sides
|
616 |
+
Each side is a directed Segment.
|
617 |
+
|
618 |
+
See Also
|
619 |
+
========
|
620 |
+
|
621 |
+
sympy.geometry.point.Point, sympy.geometry.line.Segment
|
622 |
+
|
623 |
+
Examples
|
624 |
+
========
|
625 |
+
|
626 |
+
>>> from sympy import Point, Polygon
|
627 |
+
>>> p1, p2, p3, p4 = map(Point, [(0, 0), (1, 0), (5, 1), (0, 1)])
|
628 |
+
>>> poly = Polygon(p1, p2, p3, p4)
|
629 |
+
>>> poly.sides
|
630 |
+
[Segment2D(Point2D(0, 0), Point2D(1, 0)),
|
631 |
+
Segment2D(Point2D(1, 0), Point2D(5, 1)),
|
632 |
+
Segment2D(Point2D(5, 1), Point2D(0, 1)), Segment2D(Point2D(0, 1), Point2D(0, 0))]
|
633 |
+
|
634 |
+
"""
|
635 |
+
res = []
|
636 |
+
args = self.vertices
|
637 |
+
for i in range(-len(args), 0):
|
638 |
+
res.append(Segment(args[i], args[i + 1]))
|
639 |
+
return res
|
640 |
+
|
641 |
+
@property
|
642 |
+
def bounds(self):
|
643 |
+
"""Return a tuple (xmin, ymin, xmax, ymax) representing the bounding
|
644 |
+
rectangle for the geometric figure.
|
645 |
+
|
646 |
+
"""
|
647 |
+
|
648 |
+
verts = self.vertices
|
649 |
+
xs = [p.x for p in verts]
|
650 |
+
ys = [p.y for p in verts]
|
651 |
+
return (min(xs), min(ys), max(xs), max(ys))
|
652 |
+
|
653 |
+
def is_convex(self):
|
654 |
+
"""Is the polygon convex?
|
655 |
+
|
656 |
+
A polygon is convex if all its interior angles are less than 180
|
657 |
+
degrees and there are no intersections between sides.
|
658 |
+
|
659 |
+
Returns
|
660 |
+
=======
|
661 |
+
|
662 |
+
is_convex : boolean
|
663 |
+
True if this polygon is convex, False otherwise.
|
664 |
+
|
665 |
+
See Also
|
666 |
+
========
|
667 |
+
|
668 |
+
sympy.geometry.util.convex_hull
|
669 |
+
|
670 |
+
Examples
|
671 |
+
========
|
672 |
+
|
673 |
+
>>> from sympy import Point, Polygon
|
674 |
+
>>> p1, p2, p3, p4 = map(Point, [(0, 0), (1, 0), (5, 1), (0, 1)])
|
675 |
+
>>> poly = Polygon(p1, p2, p3, p4)
|
676 |
+
>>> poly.is_convex()
|
677 |
+
True
|
678 |
+
|
679 |
+
"""
|
680 |
+
# Determine orientation of points
|
681 |
+
args = self.vertices
|
682 |
+
cw = self._isright(args[-2], args[-1], args[0])
|
683 |
+
for i in range(1, len(args)):
|
684 |
+
if cw ^ self._isright(args[i - 2], args[i - 1], args[i]):
|
685 |
+
return False
|
686 |
+
# check for intersecting sides
|
687 |
+
sides = self.sides
|
688 |
+
for i, si in enumerate(sides):
|
689 |
+
pts = si.args
|
690 |
+
# exclude the sides connected to si
|
691 |
+
for j in range(1 if i == len(sides) - 1 else 0, i - 1):
|
692 |
+
sj = sides[j]
|
693 |
+
if sj.p1 not in pts and sj.p2 not in pts:
|
694 |
+
hit = si.intersection(sj)
|
695 |
+
if hit:
|
696 |
+
return False
|
697 |
+
return True
|
698 |
+
|
699 |
+
def encloses_point(self, p):
|
700 |
+
"""
|
701 |
+
Return True if p is enclosed by (is inside of) self.
|
702 |
+
|
703 |
+
Notes
|
704 |
+
=====
|
705 |
+
|
706 |
+
Being on the border of self is considered False.
|
707 |
+
|
708 |
+
Parameters
|
709 |
+
==========
|
710 |
+
|
711 |
+
p : Point
|
712 |
+
|
713 |
+
Returns
|
714 |
+
=======
|
715 |
+
|
716 |
+
encloses_point : True, False or None
|
717 |
+
|
718 |
+
See Also
|
719 |
+
========
|
720 |
+
|
721 |
+
sympy.geometry.point.Point, sympy.geometry.ellipse.Ellipse.encloses_point
|
722 |
+
|
723 |
+
Examples
|
724 |
+
========
|
725 |
+
|
726 |
+
>>> from sympy import Polygon, Point
|
727 |
+
>>> p = Polygon((0, 0), (4, 0), (4, 4))
|
728 |
+
>>> p.encloses_point(Point(2, 1))
|
729 |
+
True
|
730 |
+
>>> p.encloses_point(Point(2, 2))
|
731 |
+
False
|
732 |
+
>>> p.encloses_point(Point(5, 5))
|
733 |
+
False
|
734 |
+
|
735 |
+
References
|
736 |
+
==========
|
737 |
+
|
738 |
+
.. [1] http://paulbourke.net/geometry/polygonmesh/#insidepoly
|
739 |
+
|
740 |
+
"""
|
741 |
+
p = Point(p, dim=2)
|
742 |
+
if p in self.vertices or any(p in s for s in self.sides):
|
743 |
+
return False
|
744 |
+
|
745 |
+
# move to p, checking that the result is numeric
|
746 |
+
lit = []
|
747 |
+
for v in self.vertices:
|
748 |
+
lit.append(v - p) # the difference is simplified
|
749 |
+
if lit[-1].free_symbols:
|
750 |
+
return None
|
751 |
+
|
752 |
+
poly = Polygon(*lit)
|
753 |
+
|
754 |
+
# polygon closure is assumed in the following test but Polygon removes duplicate pts so
|
755 |
+
# the last point has to be added so all sides are computed. Using Polygon.sides is
|
756 |
+
# not good since Segments are unordered.
|
757 |
+
args = poly.args
|
758 |
+
indices = list(range(-len(args), 1))
|
759 |
+
|
760 |
+
if poly.is_convex():
|
761 |
+
orientation = None
|
762 |
+
for i in indices:
|
763 |
+
a = args[i]
|
764 |
+
b = args[i + 1]
|
765 |
+
test = ((-a.y)*(b.x - a.x) - (-a.x)*(b.y - a.y)).is_negative
|
766 |
+
if orientation is None:
|
767 |
+
orientation = test
|
768 |
+
elif test is not orientation:
|
769 |
+
return False
|
770 |
+
return True
|
771 |
+
|
772 |
+
hit_odd = False
|
773 |
+
p1x, p1y = args[0].args
|
774 |
+
for i in indices[1:]:
|
775 |
+
p2x, p2y = args[i].args
|
776 |
+
if 0 > min(p1y, p2y):
|
777 |
+
if 0 <= max(p1y, p2y):
|
778 |
+
if 0 <= max(p1x, p2x):
|
779 |
+
if p1y != p2y:
|
780 |
+
xinters = (-p1y)*(p2x - p1x)/(p2y - p1y) + p1x
|
781 |
+
if p1x == p2x or 0 <= xinters:
|
782 |
+
hit_odd = not hit_odd
|
783 |
+
p1x, p1y = p2x, p2y
|
784 |
+
return hit_odd
|
785 |
+
|
786 |
+
def arbitrary_point(self, parameter='t'):
|
787 |
+
"""A parameterized point on the polygon.
|
788 |
+
|
789 |
+
The parameter, varying from 0 to 1, assigns points to the position on
|
790 |
+
the perimeter that is that fraction of the total perimeter. So the
|
791 |
+
point evaluated at t=1/2 would return the point from the first vertex
|
792 |
+
that is 1/2 way around the polygon.
|
793 |
+
|
794 |
+
Parameters
|
795 |
+
==========
|
796 |
+
|
797 |
+
parameter : str, optional
|
798 |
+
Default value is 't'.
|
799 |
+
|
800 |
+
Returns
|
801 |
+
=======
|
802 |
+
|
803 |
+
arbitrary_point : Point
|
804 |
+
|
805 |
+
Raises
|
806 |
+
======
|
807 |
+
|
808 |
+
ValueError
|
809 |
+
When `parameter` already appears in the Polygon's definition.
|
810 |
+
|
811 |
+
See Also
|
812 |
+
========
|
813 |
+
|
814 |
+
sympy.geometry.point.Point
|
815 |
+
|
816 |
+
Examples
|
817 |
+
========
|
818 |
+
|
819 |
+
>>> from sympy import Polygon, Symbol
|
820 |
+
>>> t = Symbol('t', real=True)
|
821 |
+
>>> tri = Polygon((0, 0), (1, 0), (1, 1))
|
822 |
+
>>> p = tri.arbitrary_point('t')
|
823 |
+
>>> perimeter = tri.perimeter
|
824 |
+
>>> s1, s2 = [s.length for s in tri.sides[:2]]
|
825 |
+
>>> p.subs(t, (s1 + s2/2)/perimeter)
|
826 |
+
Point2D(1, 1/2)
|
827 |
+
|
828 |
+
"""
|
829 |
+
t = _symbol(parameter, real=True)
|
830 |
+
if t.name in (f.name for f in self.free_symbols):
|
831 |
+
raise ValueError('Symbol %s already appears in object and cannot be used as a parameter.' % t.name)
|
832 |
+
sides = []
|
833 |
+
perimeter = self.perimeter
|
834 |
+
perim_fraction_start = 0
|
835 |
+
for s in self.sides:
|
836 |
+
side_perim_fraction = s.length/perimeter
|
837 |
+
perim_fraction_end = perim_fraction_start + side_perim_fraction
|
838 |
+
pt = s.arbitrary_point(parameter).subs(
|
839 |
+
t, (t - perim_fraction_start)/side_perim_fraction)
|
840 |
+
sides.append(
|
841 |
+
(pt, (And(perim_fraction_start <= t, t < perim_fraction_end))))
|
842 |
+
perim_fraction_start = perim_fraction_end
|
843 |
+
return Piecewise(*sides)
|
844 |
+
|
845 |
+
def parameter_value(self, other, t):
|
846 |
+
if not isinstance(other,GeometryEntity):
|
847 |
+
other = Point(other, dim=self.ambient_dimension)
|
848 |
+
if not isinstance(other,Point):
|
849 |
+
raise ValueError("other must be a point")
|
850 |
+
if other.free_symbols:
|
851 |
+
raise NotImplementedError('non-numeric coordinates')
|
852 |
+
unknown = False
|
853 |
+
p = self.arbitrary_point(T)
|
854 |
+
for pt, cond in p.args:
|
855 |
+
sol = solve(pt - other, T, dict=True)
|
856 |
+
if not sol:
|
857 |
+
continue
|
858 |
+
value = sol[0][T]
|
859 |
+
if simplify(cond.subs(T, value)) == True:
|
860 |
+
return {t: value}
|
861 |
+
unknown = True
|
862 |
+
if unknown:
|
863 |
+
raise ValueError("Given point may not be on %s" % func_name(self))
|
864 |
+
raise ValueError("Given point is not on %s" % func_name(self))
|
865 |
+
|
866 |
+
def plot_interval(self, parameter='t'):
|
867 |
+
"""The plot interval for the default geometric plot of the polygon.
|
868 |
+
|
869 |
+
Parameters
|
870 |
+
==========
|
871 |
+
|
872 |
+
parameter : str, optional
|
873 |
+
Default value is 't'.
|
874 |
+
|
875 |
+
Returns
|
876 |
+
=======
|
877 |
+
|
878 |
+
plot_interval : list (plot interval)
|
879 |
+
[parameter, lower_bound, upper_bound]
|
880 |
+
|
881 |
+
Examples
|
882 |
+
========
|
883 |
+
|
884 |
+
>>> from sympy import Polygon
|
885 |
+
>>> p = Polygon((0, 0), (1, 0), (1, 1))
|
886 |
+
>>> p.plot_interval()
|
887 |
+
[t, 0, 1]
|
888 |
+
|
889 |
+
"""
|
890 |
+
t = Symbol(parameter, real=True)
|
891 |
+
return [t, 0, 1]
|
892 |
+
|
893 |
+
def intersection(self, o):
|
894 |
+
"""The intersection of polygon and geometry entity.
|
895 |
+
|
896 |
+
The intersection may be empty and can contain individual Points and
|
897 |
+
complete Line Segments.
|
898 |
+
|
899 |
+
Parameters
|
900 |
+
==========
|
901 |
+
|
902 |
+
other: GeometryEntity
|
903 |
+
|
904 |
+
Returns
|
905 |
+
=======
|
906 |
+
|
907 |
+
intersection : list
|
908 |
+
The list of Segments and Points
|
909 |
+
|
910 |
+
See Also
|
911 |
+
========
|
912 |
+
|
913 |
+
sympy.geometry.point.Point, sympy.geometry.line.Segment
|
914 |
+
|
915 |
+
Examples
|
916 |
+
========
|
917 |
+
|
918 |
+
>>> from sympy import Point, Polygon, Line
|
919 |
+
>>> p1, p2, p3, p4 = map(Point, [(0, 0), (1, 0), (5, 1), (0, 1)])
|
920 |
+
>>> poly1 = Polygon(p1, p2, p3, p4)
|
921 |
+
>>> p5, p6, p7 = map(Point, [(3, 2), (1, -1), (0, 2)])
|
922 |
+
>>> poly2 = Polygon(p5, p6, p7)
|
923 |
+
>>> poly1.intersection(poly2)
|
924 |
+
[Point2D(1/3, 1), Point2D(2/3, 0), Point2D(9/5, 1/5), Point2D(7/3, 1)]
|
925 |
+
>>> poly1.intersection(Line(p1, p2))
|
926 |
+
[Segment2D(Point2D(0, 0), Point2D(1, 0))]
|
927 |
+
>>> poly1.intersection(p1)
|
928 |
+
[Point2D(0, 0)]
|
929 |
+
"""
|
930 |
+
intersection_result = []
|
931 |
+
k = o.sides if isinstance(o, Polygon) else [o]
|
932 |
+
for side in self.sides:
|
933 |
+
for side1 in k:
|
934 |
+
intersection_result.extend(side.intersection(side1))
|
935 |
+
|
936 |
+
intersection_result = list(uniq(intersection_result))
|
937 |
+
points = [entity for entity in intersection_result if isinstance(entity, Point)]
|
938 |
+
segments = [entity for entity in intersection_result if isinstance(entity, Segment)]
|
939 |
+
|
940 |
+
if points and segments:
|
941 |
+
points_in_segments = list(uniq([point for point in points for segment in segments if point in segment]))
|
942 |
+
if points_in_segments:
|
943 |
+
for i in points_in_segments:
|
944 |
+
points.remove(i)
|
945 |
+
return list(ordered(segments + points))
|
946 |
+
else:
|
947 |
+
return list(ordered(intersection_result))
|
948 |
+
|
949 |
+
|
950 |
+
def cut_section(self, line):
|
951 |
+
"""
|
952 |
+
Returns a tuple of two polygon segments that lie above and below
|
953 |
+
the intersecting line respectively.
|
954 |
+
|
955 |
+
Parameters
|
956 |
+
==========
|
957 |
+
|
958 |
+
line: Line object of geometry module
|
959 |
+
line which cuts the Polygon. The part of the Polygon that lies
|
960 |
+
above and below this line is returned.
|
961 |
+
|
962 |
+
Returns
|
963 |
+
=======
|
964 |
+
|
965 |
+
upper_polygon, lower_polygon: Polygon objects or None
|
966 |
+
upper_polygon is the polygon that lies above the given line.
|
967 |
+
lower_polygon is the polygon that lies below the given line.
|
968 |
+
upper_polygon and lower polygon are ``None`` when no polygon
|
969 |
+
exists above the line or below the line.
|
970 |
+
|
971 |
+
Raises
|
972 |
+
======
|
973 |
+
|
974 |
+
ValueError: When the line does not intersect the polygon
|
975 |
+
|
976 |
+
Examples
|
977 |
+
========
|
978 |
+
|
979 |
+
>>> from sympy import Polygon, Line
|
980 |
+
>>> a, b = 20, 10
|
981 |
+
>>> p1, p2, p3, p4 = [(0, b), (0, 0), (a, 0), (a, b)]
|
982 |
+
>>> rectangle = Polygon(p1, p2, p3, p4)
|
983 |
+
>>> t = rectangle.cut_section(Line((0, 5), slope=0))
|
984 |
+
>>> t
|
985 |
+
(Polygon(Point2D(0, 10), Point2D(0, 5), Point2D(20, 5), Point2D(20, 10)),
|
986 |
+
Polygon(Point2D(0, 5), Point2D(0, 0), Point2D(20, 0), Point2D(20, 5)))
|
987 |
+
>>> upper_segment, lower_segment = t
|
988 |
+
>>> upper_segment.area
|
989 |
+
100
|
990 |
+
>>> upper_segment.centroid
|
991 |
+
Point2D(10, 15/2)
|
992 |
+
>>> lower_segment.centroid
|
993 |
+
Point2D(10, 5/2)
|
994 |
+
|
995 |
+
References
|
996 |
+
==========
|
997 |
+
|
998 |
+
.. [1] https://github.com/sympy/sympy/wiki/A-method-to-return-a-cut-section-of-any-polygon-geometry
|
999 |
+
|
1000 |
+
"""
|
1001 |
+
intersection_points = self.intersection(line)
|
1002 |
+
if not intersection_points:
|
1003 |
+
raise ValueError("This line does not intersect the polygon")
|
1004 |
+
|
1005 |
+
points = list(self.vertices)
|
1006 |
+
points.append(points[0])
|
1007 |
+
|
1008 |
+
eq = line.equation(x, y)
|
1009 |
+
|
1010 |
+
# considering equation of line to be `ax +by + c`
|
1011 |
+
a = eq.coeff(x)
|
1012 |
+
b = eq.coeff(y)
|
1013 |
+
|
1014 |
+
upper_vertices = []
|
1015 |
+
lower_vertices = []
|
1016 |
+
# prev is true when previous point is above the line
|
1017 |
+
prev = True
|
1018 |
+
prev_point = None
|
1019 |
+
for point in points:
|
1020 |
+
# when coefficient of y is 0, right side of the line is
|
1021 |
+
# considered
|
1022 |
+
compare = eq.subs({x: point.x, y: point.y})/b if b \
|
1023 |
+
else eq.subs(x, point.x)/a
|
1024 |
+
|
1025 |
+
# if point lies above line
|
1026 |
+
if compare > 0:
|
1027 |
+
if not prev:
|
1028 |
+
# if previous point lies below the line, the intersection
|
1029 |
+
# point of the polygon edge and the line has to be included
|
1030 |
+
edge = Line(point, prev_point)
|
1031 |
+
new_point = edge.intersection(line)
|
1032 |
+
upper_vertices.append(new_point[0])
|
1033 |
+
lower_vertices.append(new_point[0])
|
1034 |
+
|
1035 |
+
upper_vertices.append(point)
|
1036 |
+
prev = True
|
1037 |
+
else:
|
1038 |
+
if prev and prev_point:
|
1039 |
+
edge = Line(point, prev_point)
|
1040 |
+
new_point = edge.intersection(line)
|
1041 |
+
upper_vertices.append(new_point[0])
|
1042 |
+
lower_vertices.append(new_point[0])
|
1043 |
+
lower_vertices.append(point)
|
1044 |
+
prev = False
|
1045 |
+
prev_point = point
|
1046 |
+
|
1047 |
+
upper_polygon, lower_polygon = None, None
|
1048 |
+
if upper_vertices and isinstance(Polygon(*upper_vertices), Polygon):
|
1049 |
+
upper_polygon = Polygon(*upper_vertices)
|
1050 |
+
if lower_vertices and isinstance(Polygon(*lower_vertices), Polygon):
|
1051 |
+
lower_polygon = Polygon(*lower_vertices)
|
1052 |
+
|
1053 |
+
return upper_polygon, lower_polygon
|
1054 |
+
|
1055 |
+
|
1056 |
+
def distance(self, o):
|
1057 |
+
"""
|
1058 |
+
Returns the shortest distance between self and o.
|
1059 |
+
|
1060 |
+
If o is a point, then self does not need to be convex.
|
1061 |
+
If o is another polygon self and o must be convex.
|
1062 |
+
|
1063 |
+
Examples
|
1064 |
+
========
|
1065 |
+
|
1066 |
+
>>> from sympy import Point, Polygon, RegularPolygon
|
1067 |
+
>>> p1, p2 = map(Point, [(0, 0), (7, 5)])
|
1068 |
+
>>> poly = Polygon(*RegularPolygon(p1, 1, 3).vertices)
|
1069 |
+
>>> poly.distance(p2)
|
1070 |
+
sqrt(61)
|
1071 |
+
"""
|
1072 |
+
if isinstance(o, Point):
|
1073 |
+
dist = oo
|
1074 |
+
for side in self.sides:
|
1075 |
+
current = side.distance(o)
|
1076 |
+
if current == 0:
|
1077 |
+
return S.Zero
|
1078 |
+
elif current < dist:
|
1079 |
+
dist = current
|
1080 |
+
return dist
|
1081 |
+
elif isinstance(o, Polygon) and self.is_convex() and o.is_convex():
|
1082 |
+
return self._do_poly_distance(o)
|
1083 |
+
raise NotImplementedError()
|
1084 |
+
|
1085 |
+
def _do_poly_distance(self, e2):
|
1086 |
+
"""
|
1087 |
+
Calculates the least distance between the exteriors of two
|
1088 |
+
convex polygons e1 and e2. Does not check for the convexity
|
1089 |
+
of the polygons as this is checked by Polygon.distance.
|
1090 |
+
|
1091 |
+
Notes
|
1092 |
+
=====
|
1093 |
+
|
1094 |
+
- Prints a warning if the two polygons possibly intersect as the return
|
1095 |
+
value will not be valid in such a case. For a more through test of
|
1096 |
+
intersection use intersection().
|
1097 |
+
|
1098 |
+
See Also
|
1099 |
+
========
|
1100 |
+
|
1101 |
+
sympy.geometry.point.Point.distance
|
1102 |
+
|
1103 |
+
Examples
|
1104 |
+
========
|
1105 |
+
|
1106 |
+
>>> from sympy import Point, Polygon
|
1107 |
+
>>> square = Polygon(Point(0, 0), Point(0, 1), Point(1, 1), Point(1, 0))
|
1108 |
+
>>> triangle = Polygon(Point(1, 2), Point(2, 2), Point(2, 1))
|
1109 |
+
>>> square._do_poly_distance(triangle)
|
1110 |
+
sqrt(2)/2
|
1111 |
+
|
1112 |
+
Description of method used
|
1113 |
+
==========================
|
1114 |
+
|
1115 |
+
Method:
|
1116 |
+
[1] https://web.archive.org/web/20150509035744/http://cgm.cs.mcgill.ca/~orm/mind2p.html
|
1117 |
+
Uses rotating calipers:
|
1118 |
+
[2] https://en.wikipedia.org/wiki/Rotating_calipers
|
1119 |
+
and antipodal points:
|
1120 |
+
[3] https://en.wikipedia.org/wiki/Antipodal_point
|
1121 |
+
"""
|
1122 |
+
e1 = self
|
1123 |
+
|
1124 |
+
'''Tests for a possible intersection between the polygons and outputs a warning'''
|
1125 |
+
e1_center = e1.centroid
|
1126 |
+
e2_center = e2.centroid
|
1127 |
+
e1_max_radius = S.Zero
|
1128 |
+
e2_max_radius = S.Zero
|
1129 |
+
for vertex in e1.vertices:
|
1130 |
+
r = Point.distance(e1_center, vertex)
|
1131 |
+
if e1_max_radius < r:
|
1132 |
+
e1_max_radius = r
|
1133 |
+
for vertex in e2.vertices:
|
1134 |
+
r = Point.distance(e2_center, vertex)
|
1135 |
+
if e2_max_radius < r:
|
1136 |
+
e2_max_radius = r
|
1137 |
+
center_dist = Point.distance(e1_center, e2_center)
|
1138 |
+
if center_dist <= e1_max_radius + e2_max_radius:
|
1139 |
+
warnings.warn("Polygons may intersect producing erroneous output",
|
1140 |
+
stacklevel=3)
|
1141 |
+
|
1142 |
+
'''
|
1143 |
+
Find the upper rightmost vertex of e1 and the lowest leftmost vertex of e2
|
1144 |
+
'''
|
1145 |
+
e1_ymax = Point(0, -oo)
|
1146 |
+
e2_ymin = Point(0, oo)
|
1147 |
+
|
1148 |
+
for vertex in e1.vertices:
|
1149 |
+
if vertex.y > e1_ymax.y or (vertex.y == e1_ymax.y and vertex.x > e1_ymax.x):
|
1150 |
+
e1_ymax = vertex
|
1151 |
+
for vertex in e2.vertices:
|
1152 |
+
if vertex.y < e2_ymin.y or (vertex.y == e2_ymin.y and vertex.x < e2_ymin.x):
|
1153 |
+
e2_ymin = vertex
|
1154 |
+
min_dist = Point.distance(e1_ymax, e2_ymin)
|
1155 |
+
|
1156 |
+
'''
|
1157 |
+
Produce a dictionary with vertices of e1 as the keys and, for each vertex, the points
|
1158 |
+
to which the vertex is connected as its value. The same is then done for e2.
|
1159 |
+
'''
|
1160 |
+
e1_connections = {}
|
1161 |
+
e2_connections = {}
|
1162 |
+
|
1163 |
+
for side in e1.sides:
|
1164 |
+
if side.p1 in e1_connections:
|
1165 |
+
e1_connections[side.p1].append(side.p2)
|
1166 |
+
else:
|
1167 |
+
e1_connections[side.p1] = [side.p2]
|
1168 |
+
|
1169 |
+
if side.p2 in e1_connections:
|
1170 |
+
e1_connections[side.p2].append(side.p1)
|
1171 |
+
else:
|
1172 |
+
e1_connections[side.p2] = [side.p1]
|
1173 |
+
|
1174 |
+
for side in e2.sides:
|
1175 |
+
if side.p1 in e2_connections:
|
1176 |
+
e2_connections[side.p1].append(side.p2)
|
1177 |
+
else:
|
1178 |
+
e2_connections[side.p1] = [side.p2]
|
1179 |
+
|
1180 |
+
if side.p2 in e2_connections:
|
1181 |
+
e2_connections[side.p2].append(side.p1)
|
1182 |
+
else:
|
1183 |
+
e2_connections[side.p2] = [side.p1]
|
1184 |
+
|
1185 |
+
e1_current = e1_ymax
|
1186 |
+
e2_current = e2_ymin
|
1187 |
+
support_line = Line(Point(S.Zero, S.Zero), Point(S.One, S.Zero))
|
1188 |
+
|
1189 |
+
'''
|
1190 |
+
Determine which point in e1 and e2 will be selected after e2_ymin and e1_ymax,
|
1191 |
+
this information combined with the above produced dictionaries determines the
|
1192 |
+
path that will be taken around the polygons
|
1193 |
+
'''
|
1194 |
+
point1 = e1_connections[e1_ymax][0]
|
1195 |
+
point2 = e1_connections[e1_ymax][1]
|
1196 |
+
angle1 = support_line.angle_between(Line(e1_ymax, point1))
|
1197 |
+
angle2 = support_line.angle_between(Line(e1_ymax, point2))
|
1198 |
+
if angle1 < angle2:
|
1199 |
+
e1_next = point1
|
1200 |
+
elif angle2 < angle1:
|
1201 |
+
e1_next = point2
|
1202 |
+
elif Point.distance(e1_ymax, point1) > Point.distance(e1_ymax, point2):
|
1203 |
+
e1_next = point2
|
1204 |
+
else:
|
1205 |
+
e1_next = point1
|
1206 |
+
|
1207 |
+
point1 = e2_connections[e2_ymin][0]
|
1208 |
+
point2 = e2_connections[e2_ymin][1]
|
1209 |
+
angle1 = support_line.angle_between(Line(e2_ymin, point1))
|
1210 |
+
angle2 = support_line.angle_between(Line(e2_ymin, point2))
|
1211 |
+
if angle1 > angle2:
|
1212 |
+
e2_next = point1
|
1213 |
+
elif angle2 > angle1:
|
1214 |
+
e2_next = point2
|
1215 |
+
elif Point.distance(e2_ymin, point1) > Point.distance(e2_ymin, point2):
|
1216 |
+
e2_next = point2
|
1217 |
+
else:
|
1218 |
+
e2_next = point1
|
1219 |
+
|
1220 |
+
'''
|
1221 |
+
Loop which determines the distance between anti-podal pairs and updates the
|
1222 |
+
minimum distance accordingly. It repeats until it reaches the starting position.
|
1223 |
+
'''
|
1224 |
+
while True:
|
1225 |
+
e1_angle = support_line.angle_between(Line(e1_current, e1_next))
|
1226 |
+
e2_angle = pi - support_line.angle_between(Line(
|
1227 |
+
e2_current, e2_next))
|
1228 |
+
|
1229 |
+
if (e1_angle < e2_angle) is True:
|
1230 |
+
support_line = Line(e1_current, e1_next)
|
1231 |
+
e1_segment = Segment(e1_current, e1_next)
|
1232 |
+
min_dist_current = e1_segment.distance(e2_current)
|
1233 |
+
|
1234 |
+
if min_dist_current.evalf() < min_dist.evalf():
|
1235 |
+
min_dist = min_dist_current
|
1236 |
+
|
1237 |
+
if e1_connections[e1_next][0] != e1_current:
|
1238 |
+
e1_current = e1_next
|
1239 |
+
e1_next = e1_connections[e1_next][0]
|
1240 |
+
else:
|
1241 |
+
e1_current = e1_next
|
1242 |
+
e1_next = e1_connections[e1_next][1]
|
1243 |
+
elif (e1_angle > e2_angle) is True:
|
1244 |
+
support_line = Line(e2_next, e2_current)
|
1245 |
+
e2_segment = Segment(e2_current, e2_next)
|
1246 |
+
min_dist_current = e2_segment.distance(e1_current)
|
1247 |
+
|
1248 |
+
if min_dist_current.evalf() < min_dist.evalf():
|
1249 |
+
min_dist = min_dist_current
|
1250 |
+
|
1251 |
+
if e2_connections[e2_next][0] != e2_current:
|
1252 |
+
e2_current = e2_next
|
1253 |
+
e2_next = e2_connections[e2_next][0]
|
1254 |
+
else:
|
1255 |
+
e2_current = e2_next
|
1256 |
+
e2_next = e2_connections[e2_next][1]
|
1257 |
+
else:
|
1258 |
+
support_line = Line(e1_current, e1_next)
|
1259 |
+
e1_segment = Segment(e1_current, e1_next)
|
1260 |
+
e2_segment = Segment(e2_current, e2_next)
|
1261 |
+
min1 = e1_segment.distance(e2_next)
|
1262 |
+
min2 = e2_segment.distance(e1_next)
|
1263 |
+
|
1264 |
+
min_dist_current = min(min1, min2)
|
1265 |
+
if min_dist_current.evalf() < min_dist.evalf():
|
1266 |
+
min_dist = min_dist_current
|
1267 |
+
|
1268 |
+
if e1_connections[e1_next][0] != e1_current:
|
1269 |
+
e1_current = e1_next
|
1270 |
+
e1_next = e1_connections[e1_next][0]
|
1271 |
+
else:
|
1272 |
+
e1_current = e1_next
|
1273 |
+
e1_next = e1_connections[e1_next][1]
|
1274 |
+
|
1275 |
+
if e2_connections[e2_next][0] != e2_current:
|
1276 |
+
e2_current = e2_next
|
1277 |
+
e2_next = e2_connections[e2_next][0]
|
1278 |
+
else:
|
1279 |
+
e2_current = e2_next
|
1280 |
+
e2_next = e2_connections[e2_next][1]
|
1281 |
+
if e1_current == e1_ymax and e2_current == e2_ymin:
|
1282 |
+
break
|
1283 |
+
return min_dist
|
1284 |
+
|
1285 |
+
def _svg(self, scale_factor=1., fill_color="#66cc99"):
|
1286 |
+
"""Returns SVG path element for the Polygon.
|
1287 |
+
|
1288 |
+
Parameters
|
1289 |
+
==========
|
1290 |
+
|
1291 |
+
scale_factor : float
|
1292 |
+
Multiplication factor for the SVG stroke-width. Default is 1.
|
1293 |
+
fill_color : str, optional
|
1294 |
+
Hex string for fill color. Default is "#66cc99".
|
1295 |
+
"""
|
1296 |
+
verts = map(N, self.vertices)
|
1297 |
+
coords = ["{},{}".format(p.x, p.y) for p in verts]
|
1298 |
+
path = "M {} L {} z".format(coords[0], " L ".join(coords[1:]))
|
1299 |
+
return (
|
1300 |
+
'<path fill-rule="evenodd" fill="{2}" stroke="#555555" '
|
1301 |
+
'stroke-width="{0}" opacity="0.6" d="{1}" />'
|
1302 |
+
).format(2. * scale_factor, path, fill_color)
|
1303 |
+
|
1304 |
+
def _hashable_content(self):
|
1305 |
+
|
1306 |
+
D = {}
|
1307 |
+
def ref_list(point_list):
|
1308 |
+
kee = {}
|
1309 |
+
for i, p in enumerate(ordered(set(point_list))):
|
1310 |
+
kee[p] = i
|
1311 |
+
D[i] = p
|
1312 |
+
return [kee[p] for p in point_list]
|
1313 |
+
|
1314 |
+
S1 = ref_list(self.args)
|
1315 |
+
r_nor = rotate_left(S1, least_rotation(S1))
|
1316 |
+
S2 = ref_list(list(reversed(self.args)))
|
1317 |
+
r_rev = rotate_left(S2, least_rotation(S2))
|
1318 |
+
if r_nor < r_rev:
|
1319 |
+
r = r_nor
|
1320 |
+
else:
|
1321 |
+
r = r_rev
|
1322 |
+
canonical_args = [ D[order] for order in r ]
|
1323 |
+
return tuple(canonical_args)
|
1324 |
+
|
1325 |
+
def __contains__(self, o):
|
1326 |
+
"""
|
1327 |
+
Return True if o is contained within the boundary lines of self.altitudes
|
1328 |
+
|
1329 |
+
Parameters
|
1330 |
+
==========
|
1331 |
+
|
1332 |
+
other : GeometryEntity
|
1333 |
+
|
1334 |
+
Returns
|
1335 |
+
=======
|
1336 |
+
|
1337 |
+
contained in : bool
|
1338 |
+
The points (and sides, if applicable) are contained in self.
|
1339 |
+
|
1340 |
+
See Also
|
1341 |
+
========
|
1342 |
+
|
1343 |
+
sympy.geometry.entity.GeometryEntity.encloses
|
1344 |
+
|
1345 |
+
Examples
|
1346 |
+
========
|
1347 |
+
|
1348 |
+
>>> from sympy import Line, Segment, Point
|
1349 |
+
>>> p = Point(0, 0)
|
1350 |
+
>>> q = Point(1, 1)
|
1351 |
+
>>> s = Segment(p, q*2)
|
1352 |
+
>>> l = Line(p, q)
|
1353 |
+
>>> p in q
|
1354 |
+
False
|
1355 |
+
>>> p in s
|
1356 |
+
True
|
1357 |
+
>>> q*3 in s
|
1358 |
+
False
|
1359 |
+
>>> s in l
|
1360 |
+
True
|
1361 |
+
|
1362 |
+
"""
|
1363 |
+
|
1364 |
+
if isinstance(o, Polygon):
|
1365 |
+
return self == o
|
1366 |
+
elif isinstance(o, Segment):
|
1367 |
+
return any(o in s for s in self.sides)
|
1368 |
+
elif isinstance(o, Point):
|
1369 |
+
if o in self.vertices:
|
1370 |
+
return True
|
1371 |
+
for side in self.sides:
|
1372 |
+
if o in side:
|
1373 |
+
return True
|
1374 |
+
|
1375 |
+
return False
|
1376 |
+
|
1377 |
+
def bisectors(p, prec=None):
|
1378 |
+
"""Returns angle bisectors of a polygon. If prec is given
|
1379 |
+
then approximate the point defining the ray to that precision.
|
1380 |
+
|
1381 |
+
The distance between the points defining the bisector ray is 1.
|
1382 |
+
|
1383 |
+
Examples
|
1384 |
+
========
|
1385 |
+
|
1386 |
+
>>> from sympy import Polygon, Point
|
1387 |
+
>>> p = Polygon(Point(0, 0), Point(2, 0), Point(1, 1), Point(0, 3))
|
1388 |
+
>>> p.bisectors(2)
|
1389 |
+
{Point2D(0, 0): Ray2D(Point2D(0, 0), Point2D(0.71, 0.71)),
|
1390 |
+
Point2D(0, 3): Ray2D(Point2D(0, 3), Point2D(0.23, 2.0)),
|
1391 |
+
Point2D(1, 1): Ray2D(Point2D(1, 1), Point2D(0.19, 0.42)),
|
1392 |
+
Point2D(2, 0): Ray2D(Point2D(2, 0), Point2D(1.1, 0.38))}
|
1393 |
+
"""
|
1394 |
+
b = {}
|
1395 |
+
pts = list(p.args)
|
1396 |
+
pts.append(pts[0]) # close it
|
1397 |
+
cw = Polygon._isright(*pts[:3])
|
1398 |
+
if cw:
|
1399 |
+
pts = list(reversed(pts))
|
1400 |
+
for v, a in p.angles.items():
|
1401 |
+
i = pts.index(v)
|
1402 |
+
p1, p2 = Point._normalize_dimension(pts[i], pts[i + 1])
|
1403 |
+
ray = Ray(p1, p2).rotate(a/2, v)
|
1404 |
+
dir = ray.direction
|
1405 |
+
ray = Ray(ray.p1, ray.p1 + dir/dir.distance((0, 0)))
|
1406 |
+
if prec is not None:
|
1407 |
+
ray = Ray(ray.p1, ray.p2.n(prec))
|
1408 |
+
b[v] = ray
|
1409 |
+
return b
|
1410 |
+
|
1411 |
+
|
1412 |
+
class RegularPolygon(Polygon):
|
1413 |
+
"""
|
1414 |
+
A regular polygon.
|
1415 |
+
|
1416 |
+
Such a polygon has all internal angles equal and all sides the same length.
|
1417 |
+
|
1418 |
+
Parameters
|
1419 |
+
==========
|
1420 |
+
|
1421 |
+
center : Point
|
1422 |
+
radius : number or Basic instance
|
1423 |
+
The distance from the center to a vertex
|
1424 |
+
n : int
|
1425 |
+
The number of sides
|
1426 |
+
|
1427 |
+
Attributes
|
1428 |
+
==========
|
1429 |
+
|
1430 |
+
vertices
|
1431 |
+
center
|
1432 |
+
radius
|
1433 |
+
rotation
|
1434 |
+
apothem
|
1435 |
+
interior_angle
|
1436 |
+
exterior_angle
|
1437 |
+
circumcircle
|
1438 |
+
incircle
|
1439 |
+
angles
|
1440 |
+
|
1441 |
+
Raises
|
1442 |
+
======
|
1443 |
+
|
1444 |
+
GeometryError
|
1445 |
+
If the `center` is not a Point, or the `radius` is not a number or Basic
|
1446 |
+
instance, or the number of sides, `n`, is less than three.
|
1447 |
+
|
1448 |
+
Notes
|
1449 |
+
=====
|
1450 |
+
|
1451 |
+
A RegularPolygon can be instantiated with Polygon with the kwarg n.
|
1452 |
+
|
1453 |
+
Regular polygons are instantiated with a center, radius, number of sides
|
1454 |
+
and a rotation angle. Whereas the arguments of a Polygon are vertices, the
|
1455 |
+
vertices of the RegularPolygon must be obtained with the vertices method.
|
1456 |
+
|
1457 |
+
See Also
|
1458 |
+
========
|
1459 |
+
|
1460 |
+
sympy.geometry.point.Point, Polygon
|
1461 |
+
|
1462 |
+
Examples
|
1463 |
+
========
|
1464 |
+
|
1465 |
+
>>> from sympy import RegularPolygon, Point
|
1466 |
+
>>> r = RegularPolygon(Point(0, 0), 5, 3)
|
1467 |
+
>>> r
|
1468 |
+
RegularPolygon(Point2D(0, 0), 5, 3, 0)
|
1469 |
+
>>> r.vertices[0]
|
1470 |
+
Point2D(5, 0)
|
1471 |
+
|
1472 |
+
"""
|
1473 |
+
|
1474 |
+
__slots__ = ('_n', '_center', '_radius', '_rot')
|
1475 |
+
|
1476 |
+
def __new__(self, c, r, n, rot=0, **kwargs):
|
1477 |
+
r, n, rot = map(sympify, (r, n, rot))
|
1478 |
+
c = Point(c, dim=2, **kwargs)
|
1479 |
+
if not isinstance(r, Expr):
|
1480 |
+
raise GeometryError("r must be an Expr object, not %s" % r)
|
1481 |
+
if n.is_Number:
|
1482 |
+
as_int(n) # let an error raise if necessary
|
1483 |
+
if n < 3:
|
1484 |
+
raise GeometryError("n must be a >= 3, not %s" % n)
|
1485 |
+
|
1486 |
+
obj = GeometryEntity.__new__(self, c, r, n, **kwargs)
|
1487 |
+
obj._n = n
|
1488 |
+
obj._center = c
|
1489 |
+
obj._radius = r
|
1490 |
+
obj._rot = rot % (2*S.Pi/n) if rot.is_number else rot
|
1491 |
+
return obj
|
1492 |
+
|
1493 |
+
def _eval_evalf(self, prec=15, **options):
|
1494 |
+
c, r, n, a = self.args
|
1495 |
+
dps = prec_to_dps(prec)
|
1496 |
+
c, r, a = [i.evalf(n=dps, **options) for i in (c, r, a)]
|
1497 |
+
return self.func(c, r, n, a)
|
1498 |
+
|
1499 |
+
@property
|
1500 |
+
def args(self):
|
1501 |
+
"""
|
1502 |
+
Returns the center point, the radius,
|
1503 |
+
the number of sides, and the orientation angle.
|
1504 |
+
|
1505 |
+
Examples
|
1506 |
+
========
|
1507 |
+
|
1508 |
+
>>> from sympy import RegularPolygon, Point
|
1509 |
+
>>> r = RegularPolygon(Point(0, 0), 5, 3)
|
1510 |
+
>>> r.args
|
1511 |
+
(Point2D(0, 0), 5, 3, 0)
|
1512 |
+
"""
|
1513 |
+
return self._center, self._radius, self._n, self._rot
|
1514 |
+
|
1515 |
+
def __str__(self):
|
1516 |
+
return 'RegularPolygon(%s, %s, %s, %s)' % tuple(self.args)
|
1517 |
+
|
1518 |
+
def __repr__(self):
|
1519 |
+
return 'RegularPolygon(%s, %s, %s, %s)' % tuple(self.args)
|
1520 |
+
|
1521 |
+
@property
|
1522 |
+
def area(self):
|
1523 |
+
"""Returns the area.
|
1524 |
+
|
1525 |
+
Examples
|
1526 |
+
========
|
1527 |
+
|
1528 |
+
>>> from sympy import RegularPolygon
|
1529 |
+
>>> square = RegularPolygon((0, 0), 1, 4)
|
1530 |
+
>>> square.area
|
1531 |
+
2
|
1532 |
+
>>> _ == square.length**2
|
1533 |
+
True
|
1534 |
+
"""
|
1535 |
+
c, r, n, rot = self.args
|
1536 |
+
return sign(r)*n*self.length**2/(4*tan(pi/n))
|
1537 |
+
|
1538 |
+
@property
|
1539 |
+
def length(self):
|
1540 |
+
"""Returns the length of the sides.
|
1541 |
+
|
1542 |
+
The half-length of the side and the apothem form two legs
|
1543 |
+
of a right triangle whose hypotenuse is the radius of the
|
1544 |
+
regular polygon.
|
1545 |
+
|
1546 |
+
Examples
|
1547 |
+
========
|
1548 |
+
|
1549 |
+
>>> from sympy import RegularPolygon
|
1550 |
+
>>> from sympy import sqrt
|
1551 |
+
>>> s = square_in_unit_circle = RegularPolygon((0, 0), 1, 4)
|
1552 |
+
>>> s.length
|
1553 |
+
sqrt(2)
|
1554 |
+
>>> sqrt((_/2)**2 + s.apothem**2) == s.radius
|
1555 |
+
True
|
1556 |
+
|
1557 |
+
"""
|
1558 |
+
return self.radius*2*sin(pi/self._n)
|
1559 |
+
|
1560 |
+
@property
|
1561 |
+
def center(self):
|
1562 |
+
"""The center of the RegularPolygon
|
1563 |
+
|
1564 |
+
This is also the center of the circumscribing circle.
|
1565 |
+
|
1566 |
+
Returns
|
1567 |
+
=======
|
1568 |
+
|
1569 |
+
center : Point
|
1570 |
+
|
1571 |
+
See Also
|
1572 |
+
========
|
1573 |
+
|
1574 |
+
sympy.geometry.point.Point, sympy.geometry.ellipse.Ellipse.center
|
1575 |
+
|
1576 |
+
Examples
|
1577 |
+
========
|
1578 |
+
|
1579 |
+
>>> from sympy import RegularPolygon, Point
|
1580 |
+
>>> rp = RegularPolygon(Point(0, 0), 5, 4)
|
1581 |
+
>>> rp.center
|
1582 |
+
Point2D(0, 0)
|
1583 |
+
"""
|
1584 |
+
return self._center
|
1585 |
+
|
1586 |
+
centroid = center
|
1587 |
+
|
1588 |
+
@property
|
1589 |
+
def circumcenter(self):
|
1590 |
+
"""
|
1591 |
+
Alias for center.
|
1592 |
+
|
1593 |
+
Examples
|
1594 |
+
========
|
1595 |
+
|
1596 |
+
>>> from sympy import RegularPolygon, Point
|
1597 |
+
>>> rp = RegularPolygon(Point(0, 0), 5, 4)
|
1598 |
+
>>> rp.circumcenter
|
1599 |
+
Point2D(0, 0)
|
1600 |
+
"""
|
1601 |
+
return self.center
|
1602 |
+
|
1603 |
+
@property
|
1604 |
+
def radius(self):
|
1605 |
+
"""Radius of the RegularPolygon
|
1606 |
+
|
1607 |
+
This is also the radius of the circumscribing circle.
|
1608 |
+
|
1609 |
+
Returns
|
1610 |
+
=======
|
1611 |
+
|
1612 |
+
radius : number or instance of Basic
|
1613 |
+
|
1614 |
+
See Also
|
1615 |
+
========
|
1616 |
+
|
1617 |
+
sympy.geometry.line.Segment.length, sympy.geometry.ellipse.Circle.radius
|
1618 |
+
|
1619 |
+
Examples
|
1620 |
+
========
|
1621 |
+
|
1622 |
+
>>> from sympy import Symbol
|
1623 |
+
>>> from sympy import RegularPolygon, Point
|
1624 |
+
>>> radius = Symbol('r')
|
1625 |
+
>>> rp = RegularPolygon(Point(0, 0), radius, 4)
|
1626 |
+
>>> rp.radius
|
1627 |
+
r
|
1628 |
+
|
1629 |
+
"""
|
1630 |
+
return self._radius
|
1631 |
+
|
1632 |
+
@property
|
1633 |
+
def circumradius(self):
|
1634 |
+
"""
|
1635 |
+
Alias for radius.
|
1636 |
+
|
1637 |
+
Examples
|
1638 |
+
========
|
1639 |
+
|
1640 |
+
>>> from sympy import Symbol
|
1641 |
+
>>> from sympy import RegularPolygon, Point
|
1642 |
+
>>> radius = Symbol('r')
|
1643 |
+
>>> rp = RegularPolygon(Point(0, 0), radius, 4)
|
1644 |
+
>>> rp.circumradius
|
1645 |
+
r
|
1646 |
+
"""
|
1647 |
+
return self.radius
|
1648 |
+
|
1649 |
+
@property
|
1650 |
+
def rotation(self):
|
1651 |
+
"""CCW angle by which the RegularPolygon is rotated
|
1652 |
+
|
1653 |
+
Returns
|
1654 |
+
=======
|
1655 |
+
|
1656 |
+
rotation : number or instance of Basic
|
1657 |
+
|
1658 |
+
Examples
|
1659 |
+
========
|
1660 |
+
|
1661 |
+
>>> from sympy import pi
|
1662 |
+
>>> from sympy.abc import a
|
1663 |
+
>>> from sympy import RegularPolygon, Point
|
1664 |
+
>>> RegularPolygon(Point(0, 0), 3, 4, pi/4).rotation
|
1665 |
+
pi/4
|
1666 |
+
|
1667 |
+
Numerical rotation angles are made canonical:
|
1668 |
+
|
1669 |
+
>>> RegularPolygon(Point(0, 0), 3, 4, a).rotation
|
1670 |
+
a
|
1671 |
+
>>> RegularPolygon(Point(0, 0), 3, 4, pi).rotation
|
1672 |
+
0
|
1673 |
+
|
1674 |
+
"""
|
1675 |
+
return self._rot
|
1676 |
+
|
1677 |
+
@property
|
1678 |
+
def apothem(self):
|
1679 |
+
"""The inradius of the RegularPolygon.
|
1680 |
+
|
1681 |
+
The apothem/inradius is the radius of the inscribed circle.
|
1682 |
+
|
1683 |
+
Returns
|
1684 |
+
=======
|
1685 |
+
|
1686 |
+
apothem : number or instance of Basic
|
1687 |
+
|
1688 |
+
See Also
|
1689 |
+
========
|
1690 |
+
|
1691 |
+
sympy.geometry.line.Segment.length, sympy.geometry.ellipse.Circle.radius
|
1692 |
+
|
1693 |
+
Examples
|
1694 |
+
========
|
1695 |
+
|
1696 |
+
>>> from sympy import Symbol
|
1697 |
+
>>> from sympy import RegularPolygon, Point
|
1698 |
+
>>> radius = Symbol('r')
|
1699 |
+
>>> rp = RegularPolygon(Point(0, 0), radius, 4)
|
1700 |
+
>>> rp.apothem
|
1701 |
+
sqrt(2)*r/2
|
1702 |
+
|
1703 |
+
"""
|
1704 |
+
return self.radius * cos(S.Pi/self._n)
|
1705 |
+
|
1706 |
+
@property
|
1707 |
+
def inradius(self):
|
1708 |
+
"""
|
1709 |
+
Alias for apothem.
|
1710 |
+
|
1711 |
+
Examples
|
1712 |
+
========
|
1713 |
+
|
1714 |
+
>>> from sympy import Symbol
|
1715 |
+
>>> from sympy import RegularPolygon, Point
|
1716 |
+
>>> radius = Symbol('r')
|
1717 |
+
>>> rp = RegularPolygon(Point(0, 0), radius, 4)
|
1718 |
+
>>> rp.inradius
|
1719 |
+
sqrt(2)*r/2
|
1720 |
+
"""
|
1721 |
+
return self.apothem
|
1722 |
+
|
1723 |
+
@property
|
1724 |
+
def interior_angle(self):
|
1725 |
+
"""Measure of the interior angles.
|
1726 |
+
|
1727 |
+
Returns
|
1728 |
+
=======
|
1729 |
+
|
1730 |
+
interior_angle : number
|
1731 |
+
|
1732 |
+
See Also
|
1733 |
+
========
|
1734 |
+
|
1735 |
+
sympy.geometry.line.LinearEntity.angle_between
|
1736 |
+
|
1737 |
+
Examples
|
1738 |
+
========
|
1739 |
+
|
1740 |
+
>>> from sympy import RegularPolygon, Point
|
1741 |
+
>>> rp = RegularPolygon(Point(0, 0), 4, 8)
|
1742 |
+
>>> rp.interior_angle
|
1743 |
+
3*pi/4
|
1744 |
+
|
1745 |
+
"""
|
1746 |
+
return (self._n - 2)*S.Pi/self._n
|
1747 |
+
|
1748 |
+
@property
|
1749 |
+
def exterior_angle(self):
|
1750 |
+
"""Measure of the exterior angles.
|
1751 |
+
|
1752 |
+
Returns
|
1753 |
+
=======
|
1754 |
+
|
1755 |
+
exterior_angle : number
|
1756 |
+
|
1757 |
+
See Also
|
1758 |
+
========
|
1759 |
+
|
1760 |
+
sympy.geometry.line.LinearEntity.angle_between
|
1761 |
+
|
1762 |
+
Examples
|
1763 |
+
========
|
1764 |
+
|
1765 |
+
>>> from sympy import RegularPolygon, Point
|
1766 |
+
>>> rp = RegularPolygon(Point(0, 0), 4, 8)
|
1767 |
+
>>> rp.exterior_angle
|
1768 |
+
pi/4
|
1769 |
+
|
1770 |
+
"""
|
1771 |
+
return 2*S.Pi/self._n
|
1772 |
+
|
1773 |
+
@property
|
1774 |
+
def circumcircle(self):
|
1775 |
+
"""The circumcircle of the RegularPolygon.
|
1776 |
+
|
1777 |
+
Returns
|
1778 |
+
=======
|
1779 |
+
|
1780 |
+
circumcircle : Circle
|
1781 |
+
|
1782 |
+
See Also
|
1783 |
+
========
|
1784 |
+
|
1785 |
+
circumcenter, sympy.geometry.ellipse.Circle
|
1786 |
+
|
1787 |
+
Examples
|
1788 |
+
========
|
1789 |
+
|
1790 |
+
>>> from sympy import RegularPolygon, Point
|
1791 |
+
>>> rp = RegularPolygon(Point(0, 0), 4, 8)
|
1792 |
+
>>> rp.circumcircle
|
1793 |
+
Circle(Point2D(0, 0), 4)
|
1794 |
+
|
1795 |
+
"""
|
1796 |
+
return Circle(self.center, self.radius)
|
1797 |
+
|
1798 |
+
@property
|
1799 |
+
def incircle(self):
|
1800 |
+
"""The incircle of the RegularPolygon.
|
1801 |
+
|
1802 |
+
Returns
|
1803 |
+
=======
|
1804 |
+
|
1805 |
+
incircle : Circle
|
1806 |
+
|
1807 |
+
See Also
|
1808 |
+
========
|
1809 |
+
|
1810 |
+
inradius, sympy.geometry.ellipse.Circle
|
1811 |
+
|
1812 |
+
Examples
|
1813 |
+
========
|
1814 |
+
|
1815 |
+
>>> from sympy import RegularPolygon, Point
|
1816 |
+
>>> rp = RegularPolygon(Point(0, 0), 4, 7)
|
1817 |
+
>>> rp.incircle
|
1818 |
+
Circle(Point2D(0, 0), 4*cos(pi/7))
|
1819 |
+
|
1820 |
+
"""
|
1821 |
+
return Circle(self.center, self.apothem)
|
1822 |
+
|
1823 |
+
@property
|
1824 |
+
def angles(self):
|
1825 |
+
"""
|
1826 |
+
Returns a dictionary with keys, the vertices of the Polygon,
|
1827 |
+
and values, the interior angle at each vertex.
|
1828 |
+
|
1829 |
+
Examples
|
1830 |
+
========
|
1831 |
+
|
1832 |
+
>>> from sympy import RegularPolygon, Point
|
1833 |
+
>>> r = RegularPolygon(Point(0, 0), 5, 3)
|
1834 |
+
>>> r.angles
|
1835 |
+
{Point2D(-5/2, -5*sqrt(3)/2): pi/3,
|
1836 |
+
Point2D(-5/2, 5*sqrt(3)/2): pi/3,
|
1837 |
+
Point2D(5, 0): pi/3}
|
1838 |
+
"""
|
1839 |
+
ret = {}
|
1840 |
+
ang = self.interior_angle
|
1841 |
+
for v in self.vertices:
|
1842 |
+
ret[v] = ang
|
1843 |
+
return ret
|
1844 |
+
|
1845 |
+
def encloses_point(self, p):
|
1846 |
+
"""
|
1847 |
+
Return True if p is enclosed by (is inside of) self.
|
1848 |
+
|
1849 |
+
Notes
|
1850 |
+
=====
|
1851 |
+
|
1852 |
+
Being on the border of self is considered False.
|
1853 |
+
|
1854 |
+
The general Polygon.encloses_point method is called only if
|
1855 |
+
a point is not within or beyond the incircle or circumcircle,
|
1856 |
+
respectively.
|
1857 |
+
|
1858 |
+
Parameters
|
1859 |
+
==========
|
1860 |
+
|
1861 |
+
p : Point
|
1862 |
+
|
1863 |
+
Returns
|
1864 |
+
=======
|
1865 |
+
|
1866 |
+
encloses_point : True, False or None
|
1867 |
+
|
1868 |
+
See Also
|
1869 |
+
========
|
1870 |
+
|
1871 |
+
sympy.geometry.ellipse.Ellipse.encloses_point
|
1872 |
+
|
1873 |
+
Examples
|
1874 |
+
========
|
1875 |
+
|
1876 |
+
>>> from sympy import RegularPolygon, S, Point, Symbol
|
1877 |
+
>>> p = RegularPolygon((0, 0), 3, 4)
|
1878 |
+
>>> p.encloses_point(Point(0, 0))
|
1879 |
+
True
|
1880 |
+
>>> r, R = p.inradius, p.circumradius
|
1881 |
+
>>> p.encloses_point(Point((r + R)/2, 0))
|
1882 |
+
True
|
1883 |
+
>>> p.encloses_point(Point(R/2, R/2 + (R - r)/10))
|
1884 |
+
False
|
1885 |
+
>>> t = Symbol('t', real=True)
|
1886 |
+
>>> p.encloses_point(p.arbitrary_point().subs(t, S.Half))
|
1887 |
+
False
|
1888 |
+
>>> p.encloses_point(Point(5, 5))
|
1889 |
+
False
|
1890 |
+
|
1891 |
+
"""
|
1892 |
+
|
1893 |
+
c = self.center
|
1894 |
+
d = Segment(c, p).length
|
1895 |
+
if d >= self.radius:
|
1896 |
+
return False
|
1897 |
+
elif d < self.inradius:
|
1898 |
+
return True
|
1899 |
+
else:
|
1900 |
+
# now enumerate the RegularPolygon like a general polygon.
|
1901 |
+
return Polygon.encloses_point(self, p)
|
1902 |
+
|
1903 |
+
def spin(self, angle):
|
1904 |
+
"""Increment *in place* the virtual Polygon's rotation by ccw angle.
|
1905 |
+
|
1906 |
+
See also: rotate method which moves the center.
|
1907 |
+
|
1908 |
+
>>> from sympy import Polygon, Point, pi
|
1909 |
+
>>> r = Polygon(Point(0,0), 1, n=3)
|
1910 |
+
>>> r.vertices[0]
|
1911 |
+
Point2D(1, 0)
|
1912 |
+
>>> r.spin(pi/6)
|
1913 |
+
>>> r.vertices[0]
|
1914 |
+
Point2D(sqrt(3)/2, 1/2)
|
1915 |
+
|
1916 |
+
See Also
|
1917 |
+
========
|
1918 |
+
|
1919 |
+
rotation
|
1920 |
+
rotate : Creates a copy of the RegularPolygon rotated about a Point
|
1921 |
+
|
1922 |
+
"""
|
1923 |
+
self._rot += angle
|
1924 |
+
|
1925 |
+
def rotate(self, angle, pt=None):
|
1926 |
+
"""Override GeometryEntity.rotate to first rotate the RegularPolygon
|
1927 |
+
about its center.
|
1928 |
+
|
1929 |
+
>>> from sympy import Point, RegularPolygon, pi
|
1930 |
+
>>> t = RegularPolygon(Point(1, 0), 1, 3)
|
1931 |
+
>>> t.vertices[0] # vertex on x-axis
|
1932 |
+
Point2D(2, 0)
|
1933 |
+
>>> t.rotate(pi/2).vertices[0] # vertex on y axis now
|
1934 |
+
Point2D(0, 2)
|
1935 |
+
|
1936 |
+
See Also
|
1937 |
+
========
|
1938 |
+
|
1939 |
+
rotation
|
1940 |
+
spin : Rotates a RegularPolygon in place
|
1941 |
+
|
1942 |
+
"""
|
1943 |
+
|
1944 |
+
r = type(self)(*self.args) # need a copy or else changes are in-place
|
1945 |
+
r._rot += angle
|
1946 |
+
return GeometryEntity.rotate(r, angle, pt)
|
1947 |
+
|
1948 |
+
def scale(self, x=1, y=1, pt=None):
|
1949 |
+
"""Override GeometryEntity.scale since it is the radius that must be
|
1950 |
+
scaled (if x == y) or else a new Polygon must be returned.
|
1951 |
+
|
1952 |
+
>>> from sympy import RegularPolygon
|
1953 |
+
|
1954 |
+
Symmetric scaling returns a RegularPolygon:
|
1955 |
+
|
1956 |
+
>>> RegularPolygon((0, 0), 1, 4).scale(2, 2)
|
1957 |
+
RegularPolygon(Point2D(0, 0), 2, 4, 0)
|
1958 |
+
|
1959 |
+
Asymmetric scaling returns a kite as a Polygon:
|
1960 |
+
|
1961 |
+
>>> RegularPolygon((0, 0), 1, 4).scale(2, 1)
|
1962 |
+
Polygon(Point2D(2, 0), Point2D(0, 1), Point2D(-2, 0), Point2D(0, -1))
|
1963 |
+
|
1964 |
+
"""
|
1965 |
+
if pt:
|
1966 |
+
pt = Point(pt, dim=2)
|
1967 |
+
return self.translate(*(-pt).args).scale(x, y).translate(*pt.args)
|
1968 |
+
if x != y:
|
1969 |
+
return Polygon(*self.vertices).scale(x, y)
|
1970 |
+
c, r, n, rot = self.args
|
1971 |
+
r *= x
|
1972 |
+
return self.func(c, r, n, rot)
|
1973 |
+
|
1974 |
+
def reflect(self, line):
|
1975 |
+
"""Override GeometryEntity.reflect since this is not made of only
|
1976 |
+
points.
|
1977 |
+
|
1978 |
+
Examples
|
1979 |
+
========
|
1980 |
+
|
1981 |
+
>>> from sympy import RegularPolygon, Line
|
1982 |
+
|
1983 |
+
>>> RegularPolygon((0, 0), 1, 4).reflect(Line((0, 1), slope=-2))
|
1984 |
+
RegularPolygon(Point2D(4/5, 2/5), -1, 4, atan(4/3))
|
1985 |
+
|
1986 |
+
"""
|
1987 |
+
c, r, n, rot = self.args
|
1988 |
+
v = self.vertices[0]
|
1989 |
+
d = v - c
|
1990 |
+
cc = c.reflect(line)
|
1991 |
+
vv = v.reflect(line)
|
1992 |
+
dd = vv - cc
|
1993 |
+
# calculate rotation about the new center
|
1994 |
+
# which will align the vertices
|
1995 |
+
l1 = Ray((0, 0), dd)
|
1996 |
+
l2 = Ray((0, 0), d)
|
1997 |
+
ang = l1.closing_angle(l2)
|
1998 |
+
rot += ang
|
1999 |
+
# change sign of radius as point traversal is reversed
|
2000 |
+
return self.func(cc, -r, n, rot)
|
2001 |
+
|
2002 |
+
@property
|
2003 |
+
def vertices(self):
|
2004 |
+
"""The vertices of the RegularPolygon.
|
2005 |
+
|
2006 |
+
Returns
|
2007 |
+
=======
|
2008 |
+
|
2009 |
+
vertices : list
|
2010 |
+
Each vertex is a Point.
|
2011 |
+
|
2012 |
+
See Also
|
2013 |
+
========
|
2014 |
+
|
2015 |
+
sympy.geometry.point.Point
|
2016 |
+
|
2017 |
+
Examples
|
2018 |
+
========
|
2019 |
+
|
2020 |
+
>>> from sympy import RegularPolygon, Point
|
2021 |
+
>>> rp = RegularPolygon(Point(0, 0), 5, 4)
|
2022 |
+
>>> rp.vertices
|
2023 |
+
[Point2D(5, 0), Point2D(0, 5), Point2D(-5, 0), Point2D(0, -5)]
|
2024 |
+
|
2025 |
+
"""
|
2026 |
+
c = self._center
|
2027 |
+
r = abs(self._radius)
|
2028 |
+
rot = self._rot
|
2029 |
+
v = 2*S.Pi/self._n
|
2030 |
+
|
2031 |
+
return [Point(c.x + r*cos(k*v + rot), c.y + r*sin(k*v + rot))
|
2032 |
+
for k in range(self._n)]
|
2033 |
+
|
2034 |
+
def __eq__(self, o):
|
2035 |
+
if not isinstance(o, Polygon):
|
2036 |
+
return False
|
2037 |
+
elif not isinstance(o, RegularPolygon):
|
2038 |
+
return Polygon.__eq__(o, self)
|
2039 |
+
return self.args == o.args
|
2040 |
+
|
2041 |
+
def __hash__(self):
|
2042 |
+
return super().__hash__()
|
2043 |
+
|
2044 |
+
|
2045 |
+
class Triangle(Polygon):
|
2046 |
+
"""
|
2047 |
+
A polygon with three vertices and three sides.
|
2048 |
+
|
2049 |
+
Parameters
|
2050 |
+
==========
|
2051 |
+
|
2052 |
+
points : sequence of Points
|
2053 |
+
keyword: asa, sas, or sss to specify sides/angles of the triangle
|
2054 |
+
|
2055 |
+
Attributes
|
2056 |
+
==========
|
2057 |
+
|
2058 |
+
vertices
|
2059 |
+
altitudes
|
2060 |
+
orthocenter
|
2061 |
+
circumcenter
|
2062 |
+
circumradius
|
2063 |
+
circumcircle
|
2064 |
+
inradius
|
2065 |
+
incircle
|
2066 |
+
exradii
|
2067 |
+
medians
|
2068 |
+
medial
|
2069 |
+
nine_point_circle
|
2070 |
+
|
2071 |
+
Raises
|
2072 |
+
======
|
2073 |
+
|
2074 |
+
GeometryError
|
2075 |
+
If the number of vertices is not equal to three, or one of the vertices
|
2076 |
+
is not a Point, or a valid keyword is not given.
|
2077 |
+
|
2078 |
+
See Also
|
2079 |
+
========
|
2080 |
+
|
2081 |
+
sympy.geometry.point.Point, Polygon
|
2082 |
+
|
2083 |
+
Examples
|
2084 |
+
========
|
2085 |
+
|
2086 |
+
>>> from sympy import Triangle, Point
|
2087 |
+
>>> Triangle(Point(0, 0), Point(4, 0), Point(4, 3))
|
2088 |
+
Triangle(Point2D(0, 0), Point2D(4, 0), Point2D(4, 3))
|
2089 |
+
|
2090 |
+
Keywords sss, sas, or asa can be used to give the desired
|
2091 |
+
side lengths (in order) and interior angles (in degrees) that
|
2092 |
+
define the triangle:
|
2093 |
+
|
2094 |
+
>>> Triangle(sss=(3, 4, 5))
|
2095 |
+
Triangle(Point2D(0, 0), Point2D(3, 0), Point2D(3, 4))
|
2096 |
+
>>> Triangle(asa=(30, 1, 30))
|
2097 |
+
Triangle(Point2D(0, 0), Point2D(1, 0), Point2D(1/2, sqrt(3)/6))
|
2098 |
+
>>> Triangle(sas=(1, 45, 2))
|
2099 |
+
Triangle(Point2D(0, 0), Point2D(2, 0), Point2D(sqrt(2)/2, sqrt(2)/2))
|
2100 |
+
|
2101 |
+
"""
|
2102 |
+
|
2103 |
+
def __new__(cls, *args, **kwargs):
|
2104 |
+
if len(args) != 3:
|
2105 |
+
if 'sss' in kwargs:
|
2106 |
+
return _sss(*[simplify(a) for a in kwargs['sss']])
|
2107 |
+
if 'asa' in kwargs:
|
2108 |
+
return _asa(*[simplify(a) for a in kwargs['asa']])
|
2109 |
+
if 'sas' in kwargs:
|
2110 |
+
return _sas(*[simplify(a) for a in kwargs['sas']])
|
2111 |
+
msg = "Triangle instantiates with three points or a valid keyword."
|
2112 |
+
raise GeometryError(msg)
|
2113 |
+
|
2114 |
+
vertices = [Point(a, dim=2, **kwargs) for a in args]
|
2115 |
+
|
2116 |
+
# remove consecutive duplicates
|
2117 |
+
nodup = []
|
2118 |
+
for p in vertices:
|
2119 |
+
if nodup and p == nodup[-1]:
|
2120 |
+
continue
|
2121 |
+
nodup.append(p)
|
2122 |
+
if len(nodup) > 1 and nodup[-1] == nodup[0]:
|
2123 |
+
nodup.pop() # last point was same as first
|
2124 |
+
|
2125 |
+
# remove collinear points
|
2126 |
+
i = -3
|
2127 |
+
while i < len(nodup) - 3 and len(nodup) > 2:
|
2128 |
+
a, b, c = sorted(
|
2129 |
+
[nodup[i], nodup[i + 1], nodup[i + 2]], key=default_sort_key)
|
2130 |
+
if Point.is_collinear(a, b, c):
|
2131 |
+
nodup[i] = a
|
2132 |
+
nodup[i + 1] = None
|
2133 |
+
nodup.pop(i + 1)
|
2134 |
+
i += 1
|
2135 |
+
|
2136 |
+
vertices = list(filter(lambda x: x is not None, nodup))
|
2137 |
+
|
2138 |
+
if len(vertices) == 3:
|
2139 |
+
return GeometryEntity.__new__(cls, *vertices, **kwargs)
|
2140 |
+
elif len(vertices) == 2:
|
2141 |
+
return Segment(*vertices, **kwargs)
|
2142 |
+
else:
|
2143 |
+
return Point(*vertices, **kwargs)
|
2144 |
+
|
2145 |
+
@property
|
2146 |
+
def vertices(self):
|
2147 |
+
"""The triangle's vertices
|
2148 |
+
|
2149 |
+
Returns
|
2150 |
+
=======
|
2151 |
+
|
2152 |
+
vertices : tuple
|
2153 |
+
Each element in the tuple is a Point
|
2154 |
+
|
2155 |
+
See Also
|
2156 |
+
========
|
2157 |
+
|
2158 |
+
sympy.geometry.point.Point
|
2159 |
+
|
2160 |
+
Examples
|
2161 |
+
========
|
2162 |
+
|
2163 |
+
>>> from sympy import Triangle, Point
|
2164 |
+
>>> t = Triangle(Point(0, 0), Point(4, 0), Point(4, 3))
|
2165 |
+
>>> t.vertices
|
2166 |
+
(Point2D(0, 0), Point2D(4, 0), Point2D(4, 3))
|
2167 |
+
|
2168 |
+
"""
|
2169 |
+
return self.args
|
2170 |
+
|
2171 |
+
def is_similar(t1, t2):
|
2172 |
+
"""Is another triangle similar to this one.
|
2173 |
+
|
2174 |
+
Two triangles are similar if one can be uniformly scaled to the other.
|
2175 |
+
|
2176 |
+
Parameters
|
2177 |
+
==========
|
2178 |
+
|
2179 |
+
other: Triangle
|
2180 |
+
|
2181 |
+
Returns
|
2182 |
+
=======
|
2183 |
+
|
2184 |
+
is_similar : boolean
|
2185 |
+
|
2186 |
+
See Also
|
2187 |
+
========
|
2188 |
+
|
2189 |
+
sympy.geometry.entity.GeometryEntity.is_similar
|
2190 |
+
|
2191 |
+
Examples
|
2192 |
+
========
|
2193 |
+
|
2194 |
+
>>> from sympy import Triangle, Point
|
2195 |
+
>>> t1 = Triangle(Point(0, 0), Point(4, 0), Point(4, 3))
|
2196 |
+
>>> t2 = Triangle(Point(0, 0), Point(-4, 0), Point(-4, -3))
|
2197 |
+
>>> t1.is_similar(t2)
|
2198 |
+
True
|
2199 |
+
|
2200 |
+
>>> t2 = Triangle(Point(0, 0), Point(-4, 0), Point(-4, -4))
|
2201 |
+
>>> t1.is_similar(t2)
|
2202 |
+
False
|
2203 |
+
|
2204 |
+
"""
|
2205 |
+
if not isinstance(t2, Polygon):
|
2206 |
+
return False
|
2207 |
+
|
2208 |
+
s1_1, s1_2, s1_3 = [side.length for side in t1.sides]
|
2209 |
+
s2 = [side.length for side in t2.sides]
|
2210 |
+
|
2211 |
+
def _are_similar(u1, u2, u3, v1, v2, v3):
|
2212 |
+
e1 = simplify(u1/v1)
|
2213 |
+
e2 = simplify(u2/v2)
|
2214 |
+
e3 = simplify(u3/v3)
|
2215 |
+
return bool(e1 == e2) and bool(e2 == e3)
|
2216 |
+
|
2217 |
+
# There's only 6 permutations, so write them out
|
2218 |
+
return _are_similar(s1_1, s1_2, s1_3, *s2) or \
|
2219 |
+
_are_similar(s1_1, s1_3, s1_2, *s2) or \
|
2220 |
+
_are_similar(s1_2, s1_1, s1_3, *s2) or \
|
2221 |
+
_are_similar(s1_2, s1_3, s1_1, *s2) or \
|
2222 |
+
_are_similar(s1_3, s1_1, s1_2, *s2) or \
|
2223 |
+
_are_similar(s1_3, s1_2, s1_1, *s2)
|
2224 |
+
|
2225 |
+
def is_equilateral(self):
|
2226 |
+
"""Are all the sides the same length?
|
2227 |
+
|
2228 |
+
Returns
|
2229 |
+
=======
|
2230 |
+
|
2231 |
+
is_equilateral : boolean
|
2232 |
+
|
2233 |
+
See Also
|
2234 |
+
========
|
2235 |
+
|
2236 |
+
sympy.geometry.entity.GeometryEntity.is_similar, RegularPolygon
|
2237 |
+
is_isosceles, is_right, is_scalene
|
2238 |
+
|
2239 |
+
Examples
|
2240 |
+
========
|
2241 |
+
|
2242 |
+
>>> from sympy import Triangle, Point
|
2243 |
+
>>> t1 = Triangle(Point(0, 0), Point(4, 0), Point(4, 3))
|
2244 |
+
>>> t1.is_equilateral()
|
2245 |
+
False
|
2246 |
+
|
2247 |
+
>>> from sympy import sqrt
|
2248 |
+
>>> t2 = Triangle(Point(0, 0), Point(10, 0), Point(5, 5*sqrt(3)))
|
2249 |
+
>>> t2.is_equilateral()
|
2250 |
+
True
|
2251 |
+
|
2252 |
+
"""
|
2253 |
+
return not has_variety(s.length for s in self.sides)
|
2254 |
+
|
2255 |
+
def is_isosceles(self):
|
2256 |
+
"""Are two or more of the sides the same length?
|
2257 |
+
|
2258 |
+
Returns
|
2259 |
+
=======
|
2260 |
+
|
2261 |
+
is_isosceles : boolean
|
2262 |
+
|
2263 |
+
See Also
|
2264 |
+
========
|
2265 |
+
|
2266 |
+
is_equilateral, is_right, is_scalene
|
2267 |
+
|
2268 |
+
Examples
|
2269 |
+
========
|
2270 |
+
|
2271 |
+
>>> from sympy import Triangle, Point
|
2272 |
+
>>> t1 = Triangle(Point(0, 0), Point(4, 0), Point(2, 4))
|
2273 |
+
>>> t1.is_isosceles()
|
2274 |
+
True
|
2275 |
+
|
2276 |
+
"""
|
2277 |
+
return has_dups(s.length for s in self.sides)
|
2278 |
+
|
2279 |
+
def is_scalene(self):
|
2280 |
+
"""Are all the sides of the triangle of different lengths?
|
2281 |
+
|
2282 |
+
Returns
|
2283 |
+
=======
|
2284 |
+
|
2285 |
+
is_scalene : boolean
|
2286 |
+
|
2287 |
+
See Also
|
2288 |
+
========
|
2289 |
+
|
2290 |
+
is_equilateral, is_isosceles, is_right
|
2291 |
+
|
2292 |
+
Examples
|
2293 |
+
========
|
2294 |
+
|
2295 |
+
>>> from sympy import Triangle, Point
|
2296 |
+
>>> t1 = Triangle(Point(0, 0), Point(4, 0), Point(1, 4))
|
2297 |
+
>>> t1.is_scalene()
|
2298 |
+
True
|
2299 |
+
|
2300 |
+
"""
|
2301 |
+
return not has_dups(s.length for s in self.sides)
|
2302 |
+
|
2303 |
+
def is_right(self):
|
2304 |
+
"""Is the triangle right-angled.
|
2305 |
+
|
2306 |
+
Returns
|
2307 |
+
=======
|
2308 |
+
|
2309 |
+
is_right : boolean
|
2310 |
+
|
2311 |
+
See Also
|
2312 |
+
========
|
2313 |
+
|
2314 |
+
sympy.geometry.line.LinearEntity.is_perpendicular
|
2315 |
+
is_equilateral, is_isosceles, is_scalene
|
2316 |
+
|
2317 |
+
Examples
|
2318 |
+
========
|
2319 |
+
|
2320 |
+
>>> from sympy import Triangle, Point
|
2321 |
+
>>> t1 = Triangle(Point(0, 0), Point(4, 0), Point(4, 3))
|
2322 |
+
>>> t1.is_right()
|
2323 |
+
True
|
2324 |
+
|
2325 |
+
"""
|
2326 |
+
s = self.sides
|
2327 |
+
return Segment.is_perpendicular(s[0], s[1]) or \
|
2328 |
+
Segment.is_perpendicular(s[1], s[2]) or \
|
2329 |
+
Segment.is_perpendicular(s[0], s[2])
|
2330 |
+
|
2331 |
+
@property
|
2332 |
+
def altitudes(self):
|
2333 |
+
"""The altitudes of the triangle.
|
2334 |
+
|
2335 |
+
An altitude of a triangle is a segment through a vertex,
|
2336 |
+
perpendicular to the opposite side, with length being the
|
2337 |
+
height of the vertex measured from the line containing the side.
|
2338 |
+
|
2339 |
+
Returns
|
2340 |
+
=======
|
2341 |
+
|
2342 |
+
altitudes : dict
|
2343 |
+
The dictionary consists of keys which are vertices and values
|
2344 |
+
which are Segments.
|
2345 |
+
|
2346 |
+
See Also
|
2347 |
+
========
|
2348 |
+
|
2349 |
+
sympy.geometry.point.Point, sympy.geometry.line.Segment.length
|
2350 |
+
|
2351 |
+
Examples
|
2352 |
+
========
|
2353 |
+
|
2354 |
+
>>> from sympy import Point, Triangle
|
2355 |
+
>>> p1, p2, p3 = Point(0, 0), Point(1, 0), Point(0, 1)
|
2356 |
+
>>> t = Triangle(p1, p2, p3)
|
2357 |
+
>>> t.altitudes[p1]
|
2358 |
+
Segment2D(Point2D(0, 0), Point2D(1/2, 1/2))
|
2359 |
+
|
2360 |
+
"""
|
2361 |
+
s = self.sides
|
2362 |
+
v = self.vertices
|
2363 |
+
return {v[0]: s[1].perpendicular_segment(v[0]),
|
2364 |
+
v[1]: s[2].perpendicular_segment(v[1]),
|
2365 |
+
v[2]: s[0].perpendicular_segment(v[2])}
|
2366 |
+
|
2367 |
+
@property
|
2368 |
+
def orthocenter(self):
|
2369 |
+
"""The orthocenter of the triangle.
|
2370 |
+
|
2371 |
+
The orthocenter is the intersection of the altitudes of a triangle.
|
2372 |
+
It may lie inside, outside or on the triangle.
|
2373 |
+
|
2374 |
+
Returns
|
2375 |
+
=======
|
2376 |
+
|
2377 |
+
orthocenter : Point
|
2378 |
+
|
2379 |
+
See Also
|
2380 |
+
========
|
2381 |
+
|
2382 |
+
sympy.geometry.point.Point
|
2383 |
+
|
2384 |
+
Examples
|
2385 |
+
========
|
2386 |
+
|
2387 |
+
>>> from sympy import Point, Triangle
|
2388 |
+
>>> p1, p2, p3 = Point(0, 0), Point(1, 0), Point(0, 1)
|
2389 |
+
>>> t = Triangle(p1, p2, p3)
|
2390 |
+
>>> t.orthocenter
|
2391 |
+
Point2D(0, 0)
|
2392 |
+
|
2393 |
+
"""
|
2394 |
+
a = self.altitudes
|
2395 |
+
v = self.vertices
|
2396 |
+
return Line(a[v[0]]).intersection(Line(a[v[1]]))[0]
|
2397 |
+
|
2398 |
+
@property
|
2399 |
+
def circumcenter(self):
|
2400 |
+
"""The circumcenter of the triangle
|
2401 |
+
|
2402 |
+
The circumcenter is the center of the circumcircle.
|
2403 |
+
|
2404 |
+
Returns
|
2405 |
+
=======
|
2406 |
+
|
2407 |
+
circumcenter : Point
|
2408 |
+
|
2409 |
+
See Also
|
2410 |
+
========
|
2411 |
+
|
2412 |
+
sympy.geometry.point.Point
|
2413 |
+
|
2414 |
+
Examples
|
2415 |
+
========
|
2416 |
+
|
2417 |
+
>>> from sympy import Point, Triangle
|
2418 |
+
>>> p1, p2, p3 = Point(0, 0), Point(1, 0), Point(0, 1)
|
2419 |
+
>>> t = Triangle(p1, p2, p3)
|
2420 |
+
>>> t.circumcenter
|
2421 |
+
Point2D(1/2, 1/2)
|
2422 |
+
"""
|
2423 |
+
a, b, c = [x.perpendicular_bisector() for x in self.sides]
|
2424 |
+
return a.intersection(b)[0]
|
2425 |
+
|
2426 |
+
@property
|
2427 |
+
def circumradius(self):
|
2428 |
+
"""The radius of the circumcircle of the triangle.
|
2429 |
+
|
2430 |
+
Returns
|
2431 |
+
=======
|
2432 |
+
|
2433 |
+
circumradius : number of Basic instance
|
2434 |
+
|
2435 |
+
See Also
|
2436 |
+
========
|
2437 |
+
|
2438 |
+
sympy.geometry.ellipse.Circle.radius
|
2439 |
+
|
2440 |
+
Examples
|
2441 |
+
========
|
2442 |
+
|
2443 |
+
>>> from sympy import Symbol
|
2444 |
+
>>> from sympy import Point, Triangle
|
2445 |
+
>>> a = Symbol('a')
|
2446 |
+
>>> p1, p2, p3 = Point(0, 0), Point(1, 0), Point(0, a)
|
2447 |
+
>>> t = Triangle(p1, p2, p3)
|
2448 |
+
>>> t.circumradius
|
2449 |
+
sqrt(a**2/4 + 1/4)
|
2450 |
+
"""
|
2451 |
+
return Point.distance(self.circumcenter, self.vertices[0])
|
2452 |
+
|
2453 |
+
@property
|
2454 |
+
def circumcircle(self):
|
2455 |
+
"""The circle which passes through the three vertices of the triangle.
|
2456 |
+
|
2457 |
+
Returns
|
2458 |
+
=======
|
2459 |
+
|
2460 |
+
circumcircle : Circle
|
2461 |
+
|
2462 |
+
See Also
|
2463 |
+
========
|
2464 |
+
|
2465 |
+
sympy.geometry.ellipse.Circle
|
2466 |
+
|
2467 |
+
Examples
|
2468 |
+
========
|
2469 |
+
|
2470 |
+
>>> from sympy import Point, Triangle
|
2471 |
+
>>> p1, p2, p3 = Point(0, 0), Point(1, 0), Point(0, 1)
|
2472 |
+
>>> t = Triangle(p1, p2, p3)
|
2473 |
+
>>> t.circumcircle
|
2474 |
+
Circle(Point2D(1/2, 1/2), sqrt(2)/2)
|
2475 |
+
|
2476 |
+
"""
|
2477 |
+
return Circle(self.circumcenter, self.circumradius)
|
2478 |
+
|
2479 |
+
def bisectors(self):
|
2480 |
+
"""The angle bisectors of the triangle.
|
2481 |
+
|
2482 |
+
An angle bisector of a triangle is a straight line through a vertex
|
2483 |
+
which cuts the corresponding angle in half.
|
2484 |
+
|
2485 |
+
Returns
|
2486 |
+
=======
|
2487 |
+
|
2488 |
+
bisectors : dict
|
2489 |
+
Each key is a vertex (Point) and each value is the corresponding
|
2490 |
+
bisector (Segment).
|
2491 |
+
|
2492 |
+
See Also
|
2493 |
+
========
|
2494 |
+
|
2495 |
+
sympy.geometry.point.Point, sympy.geometry.line.Segment
|
2496 |
+
|
2497 |
+
Examples
|
2498 |
+
========
|
2499 |
+
|
2500 |
+
>>> from sympy import Point, Triangle, Segment
|
2501 |
+
>>> p1, p2, p3 = Point(0, 0), Point(1, 0), Point(0, 1)
|
2502 |
+
>>> t = Triangle(p1, p2, p3)
|
2503 |
+
>>> from sympy import sqrt
|
2504 |
+
>>> t.bisectors()[p2] == Segment(Point(1, 0), Point(0, sqrt(2) - 1))
|
2505 |
+
True
|
2506 |
+
|
2507 |
+
"""
|
2508 |
+
# use lines containing sides so containment check during
|
2509 |
+
# intersection calculation can be avoided, thus reducing
|
2510 |
+
# the processing time for calculating the bisectors
|
2511 |
+
s = [Line(l) for l in self.sides]
|
2512 |
+
v = self.vertices
|
2513 |
+
c = self.incenter
|
2514 |
+
l1 = Segment(v[0], Line(v[0], c).intersection(s[1])[0])
|
2515 |
+
l2 = Segment(v[1], Line(v[1], c).intersection(s[2])[0])
|
2516 |
+
l3 = Segment(v[2], Line(v[2], c).intersection(s[0])[0])
|
2517 |
+
return {v[0]: l1, v[1]: l2, v[2]: l3}
|
2518 |
+
|
2519 |
+
@property
|
2520 |
+
def incenter(self):
|
2521 |
+
"""The center of the incircle.
|
2522 |
+
|
2523 |
+
The incircle is the circle which lies inside the triangle and touches
|
2524 |
+
all three sides.
|
2525 |
+
|
2526 |
+
Returns
|
2527 |
+
=======
|
2528 |
+
|
2529 |
+
incenter : Point
|
2530 |
+
|
2531 |
+
See Also
|
2532 |
+
========
|
2533 |
+
|
2534 |
+
incircle, sympy.geometry.point.Point
|
2535 |
+
|
2536 |
+
Examples
|
2537 |
+
========
|
2538 |
+
|
2539 |
+
>>> from sympy import Point, Triangle
|
2540 |
+
>>> p1, p2, p3 = Point(0, 0), Point(1, 0), Point(0, 1)
|
2541 |
+
>>> t = Triangle(p1, p2, p3)
|
2542 |
+
>>> t.incenter
|
2543 |
+
Point2D(1 - sqrt(2)/2, 1 - sqrt(2)/2)
|
2544 |
+
|
2545 |
+
"""
|
2546 |
+
s = self.sides
|
2547 |
+
l = Matrix([s[i].length for i in [1, 2, 0]])
|
2548 |
+
p = sum(l)
|
2549 |
+
v = self.vertices
|
2550 |
+
x = simplify(l.dot(Matrix([vi.x for vi in v]))/p)
|
2551 |
+
y = simplify(l.dot(Matrix([vi.y for vi in v]))/p)
|
2552 |
+
return Point(x, y)
|
2553 |
+
|
2554 |
+
@property
|
2555 |
+
def inradius(self):
|
2556 |
+
"""The radius of the incircle.
|
2557 |
+
|
2558 |
+
Returns
|
2559 |
+
=======
|
2560 |
+
|
2561 |
+
inradius : number of Basic instance
|
2562 |
+
|
2563 |
+
See Also
|
2564 |
+
========
|
2565 |
+
|
2566 |
+
incircle, sympy.geometry.ellipse.Circle.radius
|
2567 |
+
|
2568 |
+
Examples
|
2569 |
+
========
|
2570 |
+
|
2571 |
+
>>> from sympy import Point, Triangle
|
2572 |
+
>>> p1, p2, p3 = Point(0, 0), Point(4, 0), Point(0, 3)
|
2573 |
+
>>> t = Triangle(p1, p2, p3)
|
2574 |
+
>>> t.inradius
|
2575 |
+
1
|
2576 |
+
|
2577 |
+
"""
|
2578 |
+
return simplify(2 * self.area / self.perimeter)
|
2579 |
+
|
2580 |
+
@property
|
2581 |
+
def incircle(self):
|
2582 |
+
"""The incircle of the triangle.
|
2583 |
+
|
2584 |
+
The incircle is the circle which lies inside the triangle and touches
|
2585 |
+
all three sides.
|
2586 |
+
|
2587 |
+
Returns
|
2588 |
+
=======
|
2589 |
+
|
2590 |
+
incircle : Circle
|
2591 |
+
|
2592 |
+
See Also
|
2593 |
+
========
|
2594 |
+
|
2595 |
+
sympy.geometry.ellipse.Circle
|
2596 |
+
|
2597 |
+
Examples
|
2598 |
+
========
|
2599 |
+
|
2600 |
+
>>> from sympy import Point, Triangle
|
2601 |
+
>>> p1, p2, p3 = Point(0, 0), Point(2, 0), Point(0, 2)
|
2602 |
+
>>> t = Triangle(p1, p2, p3)
|
2603 |
+
>>> t.incircle
|
2604 |
+
Circle(Point2D(2 - sqrt(2), 2 - sqrt(2)), 2 - sqrt(2))
|
2605 |
+
|
2606 |
+
"""
|
2607 |
+
return Circle(self.incenter, self.inradius)
|
2608 |
+
|
2609 |
+
@property
|
2610 |
+
def exradii(self):
|
2611 |
+
"""The radius of excircles of a triangle.
|
2612 |
+
|
2613 |
+
An excircle of the triangle is a circle lying outside the triangle,
|
2614 |
+
tangent to one of its sides and tangent to the extensions of the
|
2615 |
+
other two.
|
2616 |
+
|
2617 |
+
Returns
|
2618 |
+
=======
|
2619 |
+
|
2620 |
+
exradii : dict
|
2621 |
+
|
2622 |
+
See Also
|
2623 |
+
========
|
2624 |
+
|
2625 |
+
sympy.geometry.polygon.Triangle.inradius
|
2626 |
+
|
2627 |
+
Examples
|
2628 |
+
========
|
2629 |
+
|
2630 |
+
The exradius touches the side of the triangle to which it is keyed, e.g.
|
2631 |
+
the exradius touching side 2 is:
|
2632 |
+
|
2633 |
+
>>> from sympy import Point, Triangle
|
2634 |
+
>>> p1, p2, p3 = Point(0, 0), Point(6, 0), Point(0, 2)
|
2635 |
+
>>> t = Triangle(p1, p2, p3)
|
2636 |
+
>>> t.exradii[t.sides[2]]
|
2637 |
+
-2 + sqrt(10)
|
2638 |
+
|
2639 |
+
References
|
2640 |
+
==========
|
2641 |
+
|
2642 |
+
.. [1] https://mathworld.wolfram.com/Exradius.html
|
2643 |
+
.. [2] https://mathworld.wolfram.com/Excircles.html
|
2644 |
+
|
2645 |
+
"""
|
2646 |
+
|
2647 |
+
side = self.sides
|
2648 |
+
a = side[0].length
|
2649 |
+
b = side[1].length
|
2650 |
+
c = side[2].length
|
2651 |
+
s = (a+b+c)/2
|
2652 |
+
area = self.area
|
2653 |
+
exradii = {self.sides[0]: simplify(area/(s-a)),
|
2654 |
+
self.sides[1]: simplify(area/(s-b)),
|
2655 |
+
self.sides[2]: simplify(area/(s-c))}
|
2656 |
+
|
2657 |
+
return exradii
|
2658 |
+
|
2659 |
+
@property
|
2660 |
+
def excenters(self):
|
2661 |
+
"""Excenters of the triangle.
|
2662 |
+
|
2663 |
+
An excenter is the center of a circle that is tangent to a side of the
|
2664 |
+
triangle and the extensions of the other two sides.
|
2665 |
+
|
2666 |
+
Returns
|
2667 |
+
=======
|
2668 |
+
|
2669 |
+
excenters : dict
|
2670 |
+
|
2671 |
+
|
2672 |
+
Examples
|
2673 |
+
========
|
2674 |
+
|
2675 |
+
The excenters are keyed to the side of the triangle to which their corresponding
|
2676 |
+
excircle is tangent: The center is keyed, e.g. the excenter of a circle touching
|
2677 |
+
side 0 is:
|
2678 |
+
|
2679 |
+
>>> from sympy import Point, Triangle
|
2680 |
+
>>> p1, p2, p3 = Point(0, 0), Point(6, 0), Point(0, 2)
|
2681 |
+
>>> t = Triangle(p1, p2, p3)
|
2682 |
+
>>> t.excenters[t.sides[0]]
|
2683 |
+
Point2D(12*sqrt(10), 2/3 + sqrt(10)/3)
|
2684 |
+
|
2685 |
+
See Also
|
2686 |
+
========
|
2687 |
+
|
2688 |
+
sympy.geometry.polygon.Triangle.exradii
|
2689 |
+
|
2690 |
+
References
|
2691 |
+
==========
|
2692 |
+
|
2693 |
+
.. [1] https://mathworld.wolfram.com/Excircles.html
|
2694 |
+
|
2695 |
+
"""
|
2696 |
+
|
2697 |
+
s = self.sides
|
2698 |
+
v = self.vertices
|
2699 |
+
a = s[0].length
|
2700 |
+
b = s[1].length
|
2701 |
+
c = s[2].length
|
2702 |
+
x = [v[0].x, v[1].x, v[2].x]
|
2703 |
+
y = [v[0].y, v[1].y, v[2].y]
|
2704 |
+
|
2705 |
+
exc_coords = {
|
2706 |
+
"x1": simplify(-a*x[0]+b*x[1]+c*x[2]/(-a+b+c)),
|
2707 |
+
"x2": simplify(a*x[0]-b*x[1]+c*x[2]/(a-b+c)),
|
2708 |
+
"x3": simplify(a*x[0]+b*x[1]-c*x[2]/(a+b-c)),
|
2709 |
+
"y1": simplify(-a*y[0]+b*y[1]+c*y[2]/(-a+b+c)),
|
2710 |
+
"y2": simplify(a*y[0]-b*y[1]+c*y[2]/(a-b+c)),
|
2711 |
+
"y3": simplify(a*y[0]+b*y[1]-c*y[2]/(a+b-c))
|
2712 |
+
}
|
2713 |
+
|
2714 |
+
excenters = {
|
2715 |
+
s[0]: Point(exc_coords["x1"], exc_coords["y1"]),
|
2716 |
+
s[1]: Point(exc_coords["x2"], exc_coords["y2"]),
|
2717 |
+
s[2]: Point(exc_coords["x3"], exc_coords["y3"])
|
2718 |
+
}
|
2719 |
+
|
2720 |
+
return excenters
|
2721 |
+
|
2722 |
+
@property
|
2723 |
+
def medians(self):
|
2724 |
+
"""The medians of the triangle.
|
2725 |
+
|
2726 |
+
A median of a triangle is a straight line through a vertex and the
|
2727 |
+
midpoint of the opposite side, and divides the triangle into two
|
2728 |
+
equal areas.
|
2729 |
+
|
2730 |
+
Returns
|
2731 |
+
=======
|
2732 |
+
|
2733 |
+
medians : dict
|
2734 |
+
Each key is a vertex (Point) and each value is the median (Segment)
|
2735 |
+
at that point.
|
2736 |
+
|
2737 |
+
See Also
|
2738 |
+
========
|
2739 |
+
|
2740 |
+
sympy.geometry.point.Point.midpoint, sympy.geometry.line.Segment.midpoint
|
2741 |
+
|
2742 |
+
Examples
|
2743 |
+
========
|
2744 |
+
|
2745 |
+
>>> from sympy import Point, Triangle
|
2746 |
+
>>> p1, p2, p3 = Point(0, 0), Point(1, 0), Point(0, 1)
|
2747 |
+
>>> t = Triangle(p1, p2, p3)
|
2748 |
+
>>> t.medians[p1]
|
2749 |
+
Segment2D(Point2D(0, 0), Point2D(1/2, 1/2))
|
2750 |
+
|
2751 |
+
"""
|
2752 |
+
s = self.sides
|
2753 |
+
v = self.vertices
|
2754 |
+
return {v[0]: Segment(v[0], s[1].midpoint),
|
2755 |
+
v[1]: Segment(v[1], s[2].midpoint),
|
2756 |
+
v[2]: Segment(v[2], s[0].midpoint)}
|
2757 |
+
|
2758 |
+
@property
|
2759 |
+
def medial(self):
|
2760 |
+
"""The medial triangle of the triangle.
|
2761 |
+
|
2762 |
+
The triangle which is formed from the midpoints of the three sides.
|
2763 |
+
|
2764 |
+
Returns
|
2765 |
+
=======
|
2766 |
+
|
2767 |
+
medial : Triangle
|
2768 |
+
|
2769 |
+
See Also
|
2770 |
+
========
|
2771 |
+
|
2772 |
+
sympy.geometry.line.Segment.midpoint
|
2773 |
+
|
2774 |
+
Examples
|
2775 |
+
========
|
2776 |
+
|
2777 |
+
>>> from sympy import Point, Triangle
|
2778 |
+
>>> p1, p2, p3 = Point(0, 0), Point(1, 0), Point(0, 1)
|
2779 |
+
>>> t = Triangle(p1, p2, p3)
|
2780 |
+
>>> t.medial
|
2781 |
+
Triangle(Point2D(1/2, 0), Point2D(1/2, 1/2), Point2D(0, 1/2))
|
2782 |
+
|
2783 |
+
"""
|
2784 |
+
s = self.sides
|
2785 |
+
return Triangle(s[0].midpoint, s[1].midpoint, s[2].midpoint)
|
2786 |
+
|
2787 |
+
@property
|
2788 |
+
def nine_point_circle(self):
|
2789 |
+
"""The nine-point circle of the triangle.
|
2790 |
+
|
2791 |
+
Nine-point circle is the circumcircle of the medial triangle, which
|
2792 |
+
passes through the feet of altitudes and the middle points of segments
|
2793 |
+
connecting the vertices and the orthocenter.
|
2794 |
+
|
2795 |
+
Returns
|
2796 |
+
=======
|
2797 |
+
|
2798 |
+
nine_point_circle : Circle
|
2799 |
+
|
2800 |
+
See also
|
2801 |
+
========
|
2802 |
+
|
2803 |
+
sympy.geometry.line.Segment.midpoint
|
2804 |
+
sympy.geometry.polygon.Triangle.medial
|
2805 |
+
sympy.geometry.polygon.Triangle.orthocenter
|
2806 |
+
|
2807 |
+
Examples
|
2808 |
+
========
|
2809 |
+
|
2810 |
+
>>> from sympy import Point, Triangle
|
2811 |
+
>>> p1, p2, p3 = Point(0, 0), Point(1, 0), Point(0, 1)
|
2812 |
+
>>> t = Triangle(p1, p2, p3)
|
2813 |
+
>>> t.nine_point_circle
|
2814 |
+
Circle(Point2D(1/4, 1/4), sqrt(2)/4)
|
2815 |
+
|
2816 |
+
"""
|
2817 |
+
return Circle(*self.medial.vertices)
|
2818 |
+
|
2819 |
+
@property
|
2820 |
+
def eulerline(self):
|
2821 |
+
"""The Euler line of the triangle.
|
2822 |
+
|
2823 |
+
The line which passes through circumcenter, centroid and orthocenter.
|
2824 |
+
|
2825 |
+
Returns
|
2826 |
+
=======
|
2827 |
+
|
2828 |
+
eulerline : Line (or Point for equilateral triangles in which case all
|
2829 |
+
centers coincide)
|
2830 |
+
|
2831 |
+
Examples
|
2832 |
+
========
|
2833 |
+
|
2834 |
+
>>> from sympy import Point, Triangle
|
2835 |
+
>>> p1, p2, p3 = Point(0, 0), Point(1, 0), Point(0, 1)
|
2836 |
+
>>> t = Triangle(p1, p2, p3)
|
2837 |
+
>>> t.eulerline
|
2838 |
+
Line2D(Point2D(0, 0), Point2D(1/2, 1/2))
|
2839 |
+
|
2840 |
+
"""
|
2841 |
+
if self.is_equilateral():
|
2842 |
+
return self.orthocenter
|
2843 |
+
return Line(self.orthocenter, self.circumcenter)
|
2844 |
+
|
2845 |
+
def rad(d):
|
2846 |
+
"""Return the radian value for the given degrees (pi = 180 degrees)."""
|
2847 |
+
return d*pi/180
|
2848 |
+
|
2849 |
+
|
2850 |
+
def deg(r):
|
2851 |
+
"""Return the degree value for the given radians (pi = 180 degrees)."""
|
2852 |
+
return r/pi*180
|
2853 |
+
|
2854 |
+
|
2855 |
+
def _slope(d):
|
2856 |
+
rv = tan(rad(d))
|
2857 |
+
return rv
|
2858 |
+
|
2859 |
+
|
2860 |
+
def _asa(d1, l, d2):
|
2861 |
+
"""Return triangle having side with length l on the x-axis."""
|
2862 |
+
xy = Line((0, 0), slope=_slope(d1)).intersection(
|
2863 |
+
Line((l, 0), slope=_slope(180 - d2)))[0]
|
2864 |
+
return Triangle((0, 0), (l, 0), xy)
|
2865 |
+
|
2866 |
+
|
2867 |
+
def _sss(l1, l2, l3):
|
2868 |
+
"""Return triangle having side of length l1 on the x-axis."""
|
2869 |
+
c1 = Circle((0, 0), l3)
|
2870 |
+
c2 = Circle((l1, 0), l2)
|
2871 |
+
inter = [a for a in c1.intersection(c2) if a.y.is_nonnegative]
|
2872 |
+
if not inter:
|
2873 |
+
return None
|
2874 |
+
pt = inter[0]
|
2875 |
+
return Triangle((0, 0), (l1, 0), pt)
|
2876 |
+
|
2877 |
+
|
2878 |
+
def _sas(l1, d, l2):
|
2879 |
+
"""Return triangle having side with length l2 on the x-axis."""
|
2880 |
+
p1 = Point(0, 0)
|
2881 |
+
p2 = Point(l2, 0)
|
2882 |
+
p3 = Point(cos(rad(d))*l1, sin(rad(d))*l1)
|
2883 |
+
return Triangle(p1, p2, p3)
|
env-llmeval/lib/python3.10/site-packages/sympy/geometry/tests/__init__.py
ADDED
File without changes
|
env-llmeval/lib/python3.10/site-packages/sympy/geometry/tests/__pycache__/__init__.cpython-310.pyc
ADDED
Binary file (185 Bytes). View file
|
|
env-llmeval/lib/python3.10/site-packages/sympy/geometry/tests/__pycache__/test_curve.cpython-310.pyc
ADDED
Binary file (5.25 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/sympy/geometry/tests/__pycache__/test_ellipse.cpython-310.pyc
ADDED
Binary file (23.6 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/sympy/geometry/tests/__pycache__/test_entity.cpython-310.pyc
ADDED
Binary file (4.41 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/sympy/geometry/tests/__pycache__/test_geometrysets.cpython-310.pyc
ADDED
Binary file (1.62 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/sympy/geometry/tests/__pycache__/test_line.cpython-310.pyc
ADDED
Binary file (31.1 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/sympy/geometry/tests/__pycache__/test_parabola.cpython-310.pyc
ADDED
Binary file (5.34 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/sympy/geometry/tests/__pycache__/test_plane.cpython-310.pyc
ADDED
Binary file (9.57 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/sympy/geometry/tests/__pycache__/test_point.cpython-310.pyc
ADDED
Binary file (15.7 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/sympy/geometry/tests/__pycache__/test_polygon.cpython-310.pyc
ADDED
Binary file (22.9 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/sympy/geometry/tests/__pycache__/test_util.cpython-310.pyc
ADDED
Binary file (6.49 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/sympy/geometry/tests/test_curve.py
ADDED
@@ -0,0 +1,120 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from sympy.core.containers import Tuple
|
2 |
+
from sympy.core.numbers import (Rational, pi)
|
3 |
+
from sympy.core.singleton import S
|
4 |
+
from sympy.core.symbol import (Symbol, symbols)
|
5 |
+
from sympy.functions.elementary.hyperbolic import asinh
|
6 |
+
from sympy.functions.elementary.miscellaneous import sqrt
|
7 |
+
from sympy.geometry import Curve, Line, Point, Ellipse, Ray, Segment, Circle, Polygon, RegularPolygon
|
8 |
+
from sympy.testing.pytest import raises, slow
|
9 |
+
|
10 |
+
|
11 |
+
def test_curve():
|
12 |
+
x = Symbol('x', real=True)
|
13 |
+
s = Symbol('s')
|
14 |
+
z = Symbol('z')
|
15 |
+
|
16 |
+
# this curve is independent of the indicated parameter
|
17 |
+
c = Curve([2*s, s**2], (z, 0, 2))
|
18 |
+
|
19 |
+
assert c.parameter == z
|
20 |
+
assert c.functions == (2*s, s**2)
|
21 |
+
assert c.arbitrary_point() == Point(2*s, s**2)
|
22 |
+
assert c.arbitrary_point(z) == Point(2*s, s**2)
|
23 |
+
|
24 |
+
# this is how it is normally used
|
25 |
+
c = Curve([2*s, s**2], (s, 0, 2))
|
26 |
+
|
27 |
+
assert c.parameter == s
|
28 |
+
assert c.functions == (2*s, s**2)
|
29 |
+
t = Symbol('t')
|
30 |
+
# the t returned as assumptions
|
31 |
+
assert c.arbitrary_point() != Point(2*t, t**2)
|
32 |
+
t = Symbol('t', real=True)
|
33 |
+
# now t has the same assumptions so the test passes
|
34 |
+
assert c.arbitrary_point() == Point(2*t, t**2)
|
35 |
+
assert c.arbitrary_point(z) == Point(2*z, z**2)
|
36 |
+
assert c.arbitrary_point(c.parameter) == Point(2*s, s**2)
|
37 |
+
assert c.arbitrary_point(None) == Point(2*s, s**2)
|
38 |
+
assert c.plot_interval() == [t, 0, 2]
|
39 |
+
assert c.plot_interval(z) == [z, 0, 2]
|
40 |
+
|
41 |
+
assert Curve([x, x], (x, 0, 1)).rotate(pi/2) == Curve([-x, x], (x, 0, 1))
|
42 |
+
assert Curve([x, x], (x, 0, 1)).rotate(pi/2, (1, 2)).scale(2, 3).translate(
|
43 |
+
1, 3).arbitrary_point(s) == \
|
44 |
+
Line((0, 0), (1, 1)).rotate(pi/2, (1, 2)).scale(2, 3).translate(
|
45 |
+
1, 3).arbitrary_point(s) == \
|
46 |
+
Point(-2*s + 7, 3*s + 6)
|
47 |
+
|
48 |
+
raises(ValueError, lambda: Curve((s), (s, 1, 2)))
|
49 |
+
raises(ValueError, lambda: Curve((x, x * 2), (1, x)))
|
50 |
+
|
51 |
+
raises(ValueError, lambda: Curve((s, s + t), (s, 1, 2)).arbitrary_point())
|
52 |
+
raises(ValueError, lambda: Curve((s, s + t), (t, 1, 2)).arbitrary_point(s))
|
53 |
+
|
54 |
+
|
55 |
+
@slow
|
56 |
+
def test_free_symbols():
|
57 |
+
a, b, c, d, e, f, s = symbols('a:f,s')
|
58 |
+
assert Point(a, b).free_symbols == {a, b}
|
59 |
+
assert Line((a, b), (c, d)).free_symbols == {a, b, c, d}
|
60 |
+
assert Ray((a, b), (c, d)).free_symbols == {a, b, c, d}
|
61 |
+
assert Ray((a, b), angle=c).free_symbols == {a, b, c}
|
62 |
+
assert Segment((a, b), (c, d)).free_symbols == {a, b, c, d}
|
63 |
+
assert Line((a, b), slope=c).free_symbols == {a, b, c}
|
64 |
+
assert Curve((a*s, b*s), (s, c, d)).free_symbols == {a, b, c, d}
|
65 |
+
assert Ellipse((a, b), c, d).free_symbols == {a, b, c, d}
|
66 |
+
assert Ellipse((a, b), c, eccentricity=d).free_symbols == \
|
67 |
+
{a, b, c, d}
|
68 |
+
assert Ellipse((a, b), vradius=c, eccentricity=d).free_symbols == \
|
69 |
+
{a, b, c, d}
|
70 |
+
assert Circle((a, b), c).free_symbols == {a, b, c}
|
71 |
+
assert Circle((a, b), (c, d), (e, f)).free_symbols == \
|
72 |
+
{e, d, c, b, f, a}
|
73 |
+
assert Polygon((a, b), (c, d), (e, f)).free_symbols == \
|
74 |
+
{e, b, d, f, a, c}
|
75 |
+
assert RegularPolygon((a, b), c, d, e).free_symbols == {e, a, b, c, d}
|
76 |
+
|
77 |
+
|
78 |
+
def test_transform():
|
79 |
+
x = Symbol('x', real=True)
|
80 |
+
y = Symbol('y', real=True)
|
81 |
+
c = Curve((x, x**2), (x, 0, 1))
|
82 |
+
cout = Curve((2*x - 4, 3*x**2 - 10), (x, 0, 1))
|
83 |
+
pts = [Point(0, 0), Point(S.Half, Rational(1, 4)), Point(1, 1)]
|
84 |
+
pts_out = [Point(-4, -10), Point(-3, Rational(-37, 4)), Point(-2, -7)]
|
85 |
+
|
86 |
+
assert c.scale(2, 3, (4, 5)) == cout
|
87 |
+
assert [c.subs(x, xi/2) for xi in Tuple(0, 1, 2)] == pts
|
88 |
+
assert [cout.subs(x, xi/2) for xi in Tuple(0, 1, 2)] == pts_out
|
89 |
+
assert Curve((x + y, 3*x), (x, 0, 1)).subs(y, S.Half) == \
|
90 |
+
Curve((x + S.Half, 3*x), (x, 0, 1))
|
91 |
+
assert Curve((x, 3*x), (x, 0, 1)).translate(4, 5) == \
|
92 |
+
Curve((x + 4, 3*x + 5), (x, 0, 1))
|
93 |
+
|
94 |
+
|
95 |
+
def test_length():
|
96 |
+
t = Symbol('t', real=True)
|
97 |
+
|
98 |
+
c1 = Curve((t, 0), (t, 0, 1))
|
99 |
+
assert c1.length == 1
|
100 |
+
|
101 |
+
c2 = Curve((t, t), (t, 0, 1))
|
102 |
+
assert c2.length == sqrt(2)
|
103 |
+
|
104 |
+
c3 = Curve((t ** 2, t), (t, 2, 5))
|
105 |
+
assert c3.length == -sqrt(17) - asinh(4) / 4 + asinh(10) / 4 + 5 * sqrt(101) / 2
|
106 |
+
|
107 |
+
|
108 |
+
def test_parameter_value():
|
109 |
+
t = Symbol('t')
|
110 |
+
C = Curve([2*t, t**2], (t, 0, 2))
|
111 |
+
assert C.parameter_value((2, 1), t) == {t: 1}
|
112 |
+
raises(ValueError, lambda: C.parameter_value((2, 0), t))
|
113 |
+
|
114 |
+
|
115 |
+
def test_issue_17997():
|
116 |
+
t, s = symbols('t s')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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from sympy.core import expand
|
2 |
+
from sympy.core.numbers import (Rational, oo, pi)
|
3 |
+
from sympy.core.relational import Eq
|
4 |
+
from sympy.core.singleton import S
|
5 |
+
from sympy.core.symbol import (Symbol, symbols)
|
6 |
+
from sympy.functions.elementary.complexes import Abs
|
7 |
+
from sympy.functions.elementary.miscellaneous import sqrt
|
8 |
+
from sympy.functions.elementary.trigonometric import sec
|
9 |
+
from sympy.geometry.line import Segment2D
|
10 |
+
from sympy.geometry.point import Point2D
|
11 |
+
from sympy.geometry import (Circle, Ellipse, GeometryError, Line, Point,
|
12 |
+
Polygon, Ray, RegularPolygon, Segment,
|
13 |
+
Triangle, intersection)
|
14 |
+
from sympy.testing.pytest import raises, slow
|
15 |
+
from sympy.integrals.integrals import integrate
|
16 |
+
from sympy.functions.special.elliptic_integrals import elliptic_e
|
17 |
+
from sympy.functions.elementary.miscellaneous import Max
|
18 |
+
|
19 |
+
|
20 |
+
def test_ellipse_equation_using_slope():
|
21 |
+
from sympy.abc import x, y
|
22 |
+
|
23 |
+
e1 = Ellipse(Point(1, 0), 3, 2)
|
24 |
+
assert str(e1.equation(_slope=1)) == str((-x + y + 1)**2/8 + (x + y - 1)**2/18 - 1)
|
25 |
+
|
26 |
+
e2 = Ellipse(Point(0, 0), 4, 1)
|
27 |
+
assert str(e2.equation(_slope=1)) == str((-x + y)**2/2 + (x + y)**2/32 - 1)
|
28 |
+
|
29 |
+
e3 = Ellipse(Point(1, 5), 6, 2)
|
30 |
+
assert str(e3.equation(_slope=2)) == str((-2*x + y - 3)**2/20 + (x + 2*y - 11)**2/180 - 1)
|
31 |
+
|
32 |
+
|
33 |
+
def test_object_from_equation():
|
34 |
+
from sympy.abc import x, y, a, b, c, d, e
|
35 |
+
assert Circle(x**2 + y**2 + 3*x + 4*y - 8) == Circle(Point2D(S(-3) / 2, -2), sqrt(57) / 2)
|
36 |
+
assert Circle(x**2 + y**2 + 6*x + 8*y + 25) == Circle(Point2D(-3, -4), 0)
|
37 |
+
assert Circle(a**2 + b**2 + 6*a + 8*b + 25, x='a', y='b') == Circle(Point2D(-3, -4), 0)
|
38 |
+
assert Circle(x**2 + y**2 - 25) == Circle(Point2D(0, 0), 5)
|
39 |
+
assert Circle(x**2 + y**2) == Circle(Point2D(0, 0), 0)
|
40 |
+
assert Circle(a**2 + b**2, x='a', y='b') == Circle(Point2D(0, 0), 0)
|
41 |
+
assert Circle(x**2 + y**2 + 6*x + 8) == Circle(Point2D(-3, 0), 1)
|
42 |
+
assert Circle(x**2 + y**2 + 6*y + 8) == Circle(Point2D(0, -3), 1)
|
43 |
+
assert Circle((x - 1)**2 + y**2 - 9) == Circle(Point2D(1, 0), 3)
|
44 |
+
assert Circle(6*(x**2) + 6*(y**2) + 6*x + 8*y - 25) == Circle(Point2D(Rational(-1, 2), Rational(-2, 3)), 5*sqrt(7)/6)
|
45 |
+
assert Circle(Eq(a**2 + b**2, 25), x='a', y=b) == Circle(Point2D(0, 0), 5)
|
46 |
+
raises(GeometryError, lambda: Circle(x**2 + y**2 + 3*x + 4*y + 26))
|
47 |
+
raises(GeometryError, lambda: Circle(x**2 + y**2 + 25))
|
48 |
+
raises(GeometryError, lambda: Circle(a**2 + b**2 + 25, x='a', y='b'))
|
49 |
+
raises(GeometryError, lambda: Circle(x**2 + 6*y + 8))
|
50 |
+
raises(GeometryError, lambda: Circle(6*(x ** 2) + 4*(y**2) + 6*x + 8*y + 25))
|
51 |
+
raises(ValueError, lambda: Circle(a**2 + b**2 + 3*a + 4*b - 8))
|
52 |
+
# .equation() adds 'real=True' assumption; '==' would fail if assumptions differed
|
53 |
+
x, y = symbols('x y', real=True)
|
54 |
+
eq = a*x**2 + a*y**2 + c*x + d*y + e
|
55 |
+
assert expand(Circle(eq).equation()*a) == eq
|
56 |
+
|
57 |
+
|
58 |
+
@slow
|
59 |
+
def test_ellipse_geom():
|
60 |
+
x = Symbol('x', real=True)
|
61 |
+
y = Symbol('y', real=True)
|
62 |
+
t = Symbol('t', real=True)
|
63 |
+
y1 = Symbol('y1', real=True)
|
64 |
+
half = S.Half
|
65 |
+
p1 = Point(0, 0)
|
66 |
+
p2 = Point(1, 1)
|
67 |
+
p4 = Point(0, 1)
|
68 |
+
|
69 |
+
e1 = Ellipse(p1, 1, 1)
|
70 |
+
e2 = Ellipse(p2, half, 1)
|
71 |
+
e3 = Ellipse(p1, y1, y1)
|
72 |
+
c1 = Circle(p1, 1)
|
73 |
+
c2 = Circle(p2, 1)
|
74 |
+
c3 = Circle(Point(sqrt(2), sqrt(2)), 1)
|
75 |
+
l1 = Line(p1, p2)
|
76 |
+
|
77 |
+
# Test creation with three points
|
78 |
+
cen, rad = Point(3*half, 2), 5*half
|
79 |
+
assert Circle(Point(0, 0), Point(3, 0), Point(0, 4)) == Circle(cen, rad)
|
80 |
+
assert Circle(Point(0, 0), Point(1, 1), Point(2, 2)) == Segment2D(Point2D(0, 0), Point2D(2, 2))
|
81 |
+
|
82 |
+
raises(ValueError, lambda: Ellipse(None, None, None, 1))
|
83 |
+
raises(ValueError, lambda: Ellipse())
|
84 |
+
raises(GeometryError, lambda: Circle(Point(0, 0)))
|
85 |
+
raises(GeometryError, lambda: Circle(Symbol('x')*Symbol('y')))
|
86 |
+
|
87 |
+
# Basic Stuff
|
88 |
+
assert Ellipse(None, 1, 1).center == Point(0, 0)
|
89 |
+
assert e1 == c1
|
90 |
+
assert e1 != e2
|
91 |
+
assert e1 != l1
|
92 |
+
assert p4 in e1
|
93 |
+
assert e1 in e1
|
94 |
+
assert e2 in e2
|
95 |
+
assert 1 not in e2
|
96 |
+
assert p2 not in e2
|
97 |
+
assert e1.area == pi
|
98 |
+
assert e2.area == pi/2
|
99 |
+
assert e3.area == pi*y1*abs(y1)
|
100 |
+
assert c1.area == e1.area
|
101 |
+
assert c1.circumference == e1.circumference
|
102 |
+
assert e3.circumference == 2*pi*y1
|
103 |
+
assert e1.plot_interval() == e2.plot_interval() == [t, -pi, pi]
|
104 |
+
assert e1.plot_interval(x) == e2.plot_interval(x) == [x, -pi, pi]
|
105 |
+
|
106 |
+
assert c1.minor == 1
|
107 |
+
assert c1.major == 1
|
108 |
+
assert c1.hradius == 1
|
109 |
+
assert c1.vradius == 1
|
110 |
+
|
111 |
+
assert Ellipse((1, 1), 0, 0) == Point(1, 1)
|
112 |
+
assert Ellipse((1, 1), 1, 0) == Segment(Point(0, 1), Point(2, 1))
|
113 |
+
assert Ellipse((1, 1), 0, 1) == Segment(Point(1, 0), Point(1, 2))
|
114 |
+
|
115 |
+
# Private Functions
|
116 |
+
assert hash(c1) == hash(Circle(Point(1, 0), Point(0, 1), Point(0, -1)))
|
117 |
+
assert c1 in e1
|
118 |
+
assert (Line(p1, p2) in e1) is False
|
119 |
+
assert e1.__cmp__(e1) == 0
|
120 |
+
assert e1.__cmp__(Point(0, 0)) > 0
|
121 |
+
|
122 |
+
# Encloses
|
123 |
+
assert e1.encloses(Segment(Point(-0.5, -0.5), Point(0.5, 0.5))) is True
|
124 |
+
assert e1.encloses(Line(p1, p2)) is False
|
125 |
+
assert e1.encloses(Ray(p1, p2)) is False
|
126 |
+
assert e1.encloses(e1) is False
|
127 |
+
assert e1.encloses(
|
128 |
+
Polygon(Point(-0.5, -0.5), Point(-0.5, 0.5), Point(0.5, 0.5))) is True
|
129 |
+
assert e1.encloses(RegularPolygon(p1, 0.5, 3)) is True
|
130 |
+
assert e1.encloses(RegularPolygon(p1, 5, 3)) is False
|
131 |
+
assert e1.encloses(RegularPolygon(p2, 5, 3)) is False
|
132 |
+
|
133 |
+
assert e2.arbitrary_point() in e2
|
134 |
+
raises(ValueError, lambda: Ellipse(Point(x, y), 1, 1).arbitrary_point(parameter='x'))
|
135 |
+
|
136 |
+
# Foci
|
137 |
+
f1, f2 = Point(sqrt(12), 0), Point(-sqrt(12), 0)
|
138 |
+
ef = Ellipse(Point(0, 0), 4, 2)
|
139 |
+
assert ef.foci in [(f1, f2), (f2, f1)]
|
140 |
+
|
141 |
+
# Tangents
|
142 |
+
v = sqrt(2) / 2
|
143 |
+
p1_1 = Point(v, v)
|
144 |
+
p1_2 = p2 + Point(half, 0)
|
145 |
+
p1_3 = p2 + Point(0, 1)
|
146 |
+
assert e1.tangent_lines(p4) == c1.tangent_lines(p4)
|
147 |
+
assert e2.tangent_lines(p1_2) == [Line(Point(Rational(3, 2), 1), Point(Rational(3, 2), S.Half))]
|
148 |
+
assert e2.tangent_lines(p1_3) == [Line(Point(1, 2), Point(Rational(5, 4), 2))]
|
149 |
+
assert c1.tangent_lines(p1_1) != [Line(p1_1, Point(0, sqrt(2)))]
|
150 |
+
assert c1.tangent_lines(p1) == []
|
151 |
+
assert e2.is_tangent(Line(p1_2, p2 + Point(half, 1)))
|
152 |
+
assert e2.is_tangent(Line(p1_3, p2 + Point(half, 1)))
|
153 |
+
assert c1.is_tangent(Line(p1_1, Point(0, sqrt(2))))
|
154 |
+
assert e1.is_tangent(Line(Point(0, 0), Point(1, 1))) is False
|
155 |
+
assert c1.is_tangent(e1) is True
|
156 |
+
assert c1.is_tangent(Ellipse(Point(2, 0), 1, 1)) is True
|
157 |
+
assert c1.is_tangent(
|
158 |
+
Polygon(Point(1, 1), Point(1, -1), Point(2, 0))) is True
|
159 |
+
assert c1.is_tangent(
|
160 |
+
Polygon(Point(1, 1), Point(1, 0), Point(2, 0))) is False
|
161 |
+
assert Circle(Point(5, 5), 3).is_tangent(Circle(Point(0, 5), 1)) is False
|
162 |
+
|
163 |
+
assert Ellipse(Point(5, 5), 2, 1).tangent_lines(Point(0, 0)) == \
|
164 |
+
[Line(Point(0, 0), Point(Rational(77, 25), Rational(132, 25))),
|
165 |
+
Line(Point(0, 0), Point(Rational(33, 5), Rational(22, 5)))]
|
166 |
+
assert Ellipse(Point(5, 5), 2, 1).tangent_lines(Point(3, 4)) == \
|
167 |
+
[Line(Point(3, 4), Point(4, 4)), Line(Point(3, 4), Point(3, 5))]
|
168 |
+
assert Circle(Point(5, 5), 2).tangent_lines(Point(3, 3)) == \
|
169 |
+
[Line(Point(3, 3), Point(4, 3)), Line(Point(3, 3), Point(3, 4))]
|
170 |
+
assert Circle(Point(5, 5), 2).tangent_lines(Point(5 - 2*sqrt(2), 5)) == \
|
171 |
+
[Line(Point(5 - 2*sqrt(2), 5), Point(5 - sqrt(2), 5 - sqrt(2))),
|
172 |
+
Line(Point(5 - 2*sqrt(2), 5), Point(5 - sqrt(2), 5 + sqrt(2))), ]
|
173 |
+
assert Circle(Point(5, 5), 5).tangent_lines(Point(4, 0)) == \
|
174 |
+
[Line(Point(4, 0), Point(Rational(40, 13), Rational(5, 13))),
|
175 |
+
Line(Point(4, 0), Point(5, 0))]
|
176 |
+
assert Circle(Point(5, 5), 5).tangent_lines(Point(0, 6)) == \
|
177 |
+
[Line(Point(0, 6), Point(0, 7)),
|
178 |
+
Line(Point(0, 6), Point(Rational(5, 13), Rational(90, 13)))]
|
179 |
+
|
180 |
+
# for numerical calculations, we shouldn't demand exact equality,
|
181 |
+
# so only test up to the desired precision
|
182 |
+
def lines_close(l1, l2, prec):
|
183 |
+
""" tests whether l1 and 12 are within 10**(-prec)
|
184 |
+
of each other """
|
185 |
+
return abs(l1.p1 - l2.p1) < 10**(-prec) and abs(l1.p2 - l2.p2) < 10**(-prec)
|
186 |
+
def line_list_close(ll1, ll2, prec):
|
187 |
+
return all(lines_close(l1, l2, prec) for l1, l2 in zip(ll1, ll2))
|
188 |
+
|
189 |
+
e = Ellipse(Point(0, 0), 2, 1)
|
190 |
+
assert e.normal_lines(Point(0, 0)) == \
|
191 |
+
[Line(Point(0, 0), Point(0, 1)), Line(Point(0, 0), Point(1, 0))]
|
192 |
+
assert e.normal_lines(Point(1, 0)) == \
|
193 |
+
[Line(Point(0, 0), Point(1, 0))]
|
194 |
+
assert e.normal_lines((0, 1)) == \
|
195 |
+
[Line(Point(0, 0), Point(0, 1))]
|
196 |
+
assert line_list_close(e.normal_lines(Point(1, 1), 2), [
|
197 |
+
Line(Point(Rational(-51, 26), Rational(-1, 5)), Point(Rational(-25, 26), Rational(17, 83))),
|
198 |
+
Line(Point(Rational(28, 29), Rational(-7, 8)), Point(Rational(57, 29), Rational(-9, 2)))], 2)
|
199 |
+
# test the failure of Poly.intervals and checks a point on the boundary
|
200 |
+
p = Point(sqrt(3), S.Half)
|
201 |
+
assert p in e
|
202 |
+
assert line_list_close(e.normal_lines(p, 2), [
|
203 |
+
Line(Point(Rational(-341, 171), Rational(-1, 13)), Point(Rational(-170, 171), Rational(5, 64))),
|
204 |
+
Line(Point(Rational(26, 15), Rational(-1, 2)), Point(Rational(41, 15), Rational(-43, 26)))], 2)
|
205 |
+
# be sure to use the slope that isn't undefined on boundary
|
206 |
+
e = Ellipse((0, 0), 2, 2*sqrt(3)/3)
|
207 |
+
assert line_list_close(e.normal_lines((1, 1), 2), [
|
208 |
+
Line(Point(Rational(-64, 33), Rational(-20, 71)), Point(Rational(-31, 33), Rational(2, 13))),
|
209 |
+
Line(Point(1, -1), Point(2, -4))], 2)
|
210 |
+
# general ellipse fails except under certain conditions
|
211 |
+
e = Ellipse((0, 0), x, 1)
|
212 |
+
assert e.normal_lines((x + 1, 0)) == [Line(Point(0, 0), Point(1, 0))]
|
213 |
+
raises(NotImplementedError, lambda: e.normal_lines((x + 1, 1)))
|
214 |
+
# Properties
|
215 |
+
major = 3
|
216 |
+
minor = 1
|
217 |
+
e4 = Ellipse(p2, minor, major)
|
218 |
+
assert e4.focus_distance == sqrt(major**2 - minor**2)
|
219 |
+
ecc = e4.focus_distance / major
|
220 |
+
assert e4.eccentricity == ecc
|
221 |
+
assert e4.periapsis == major*(1 - ecc)
|
222 |
+
assert e4.apoapsis == major*(1 + ecc)
|
223 |
+
assert e4.semilatus_rectum == major*(1 - ecc ** 2)
|
224 |
+
# independent of orientation
|
225 |
+
e4 = Ellipse(p2, major, minor)
|
226 |
+
assert e4.focus_distance == sqrt(major**2 - minor**2)
|
227 |
+
ecc = e4.focus_distance / major
|
228 |
+
assert e4.eccentricity == ecc
|
229 |
+
assert e4.periapsis == major*(1 - ecc)
|
230 |
+
assert e4.apoapsis == major*(1 + ecc)
|
231 |
+
|
232 |
+
# Intersection
|
233 |
+
l1 = Line(Point(1, -5), Point(1, 5))
|
234 |
+
l2 = Line(Point(-5, -1), Point(5, -1))
|
235 |
+
l3 = Line(Point(-1, -1), Point(1, 1))
|
236 |
+
l4 = Line(Point(-10, 0), Point(0, 10))
|
237 |
+
pts_c1_l3 = [Point(sqrt(2)/2, sqrt(2)/2), Point(-sqrt(2)/2, -sqrt(2)/2)]
|
238 |
+
|
239 |
+
assert intersection(e2, l4) == []
|
240 |
+
assert intersection(c1, Point(1, 0)) == [Point(1, 0)]
|
241 |
+
assert intersection(c1, l1) == [Point(1, 0)]
|
242 |
+
assert intersection(c1, l2) == [Point(0, -1)]
|
243 |
+
assert intersection(c1, l3) in [pts_c1_l3, [pts_c1_l3[1], pts_c1_l3[0]]]
|
244 |
+
assert intersection(c1, c2) == [Point(0, 1), Point(1, 0)]
|
245 |
+
assert intersection(c1, c3) == [Point(sqrt(2)/2, sqrt(2)/2)]
|
246 |
+
assert e1.intersection(l1) == [Point(1, 0)]
|
247 |
+
assert e2.intersection(l4) == []
|
248 |
+
assert e1.intersection(Circle(Point(0, 2), 1)) == [Point(0, 1)]
|
249 |
+
assert e1.intersection(Circle(Point(5, 0), 1)) == []
|
250 |
+
assert e1.intersection(Ellipse(Point(2, 0), 1, 1)) == [Point(1, 0)]
|
251 |
+
assert e1.intersection(Ellipse(Point(5, 0), 1, 1)) == []
|
252 |
+
assert e1.intersection(Point(2, 0)) == []
|
253 |
+
assert e1.intersection(e1) == e1
|
254 |
+
assert intersection(Ellipse(Point(0, 0), 2, 1), Ellipse(Point(3, 0), 1, 2)) == [Point(2, 0)]
|
255 |
+
assert intersection(Circle(Point(0, 0), 2), Circle(Point(3, 0), 1)) == [Point(2, 0)]
|
256 |
+
assert intersection(Circle(Point(0, 0), 2), Circle(Point(7, 0), 1)) == []
|
257 |
+
assert intersection(Ellipse(Point(0, 0), 5, 17), Ellipse(Point(4, 0), 1, 0.2)) == [Point(5, 0)]
|
258 |
+
assert intersection(Ellipse(Point(0, 0), 5, 17), Ellipse(Point(4, 0), 0.999, 0.2)) == []
|
259 |
+
assert Circle((0, 0), S.Half).intersection(
|
260 |
+
Triangle((-1, 0), (1, 0), (0, 1))) == [
|
261 |
+
Point(Rational(-1, 2), 0), Point(S.Half, 0)]
|
262 |
+
raises(TypeError, lambda: intersection(e2, Line((0, 0, 0), (0, 0, 1))))
|
263 |
+
raises(TypeError, lambda: intersection(e2, Rational(12)))
|
264 |
+
raises(TypeError, lambda: Ellipse.intersection(e2, 1))
|
265 |
+
# some special case intersections
|
266 |
+
csmall = Circle(p1, 3)
|
267 |
+
cbig = Circle(p1, 5)
|
268 |
+
cout = Circle(Point(5, 5), 1)
|
269 |
+
# one circle inside of another
|
270 |
+
assert csmall.intersection(cbig) == []
|
271 |
+
# separate circles
|
272 |
+
assert csmall.intersection(cout) == []
|
273 |
+
# coincident circles
|
274 |
+
assert csmall.intersection(csmall) == csmall
|
275 |
+
|
276 |
+
v = sqrt(2)
|
277 |
+
t1 = Triangle(Point(0, v), Point(0, -v), Point(v, 0))
|
278 |
+
points = intersection(t1, c1)
|
279 |
+
assert len(points) == 4
|
280 |
+
assert Point(0, 1) in points
|
281 |
+
assert Point(0, -1) in points
|
282 |
+
assert Point(v/2, v/2) in points
|
283 |
+
assert Point(v/2, -v/2) in points
|
284 |
+
|
285 |
+
circ = Circle(Point(0, 0), 5)
|
286 |
+
elip = Ellipse(Point(0, 0), 5, 20)
|
287 |
+
assert intersection(circ, elip) in \
|
288 |
+
[[Point(5, 0), Point(-5, 0)], [Point(-5, 0), Point(5, 0)]]
|
289 |
+
assert elip.tangent_lines(Point(0, 0)) == []
|
290 |
+
elip = Ellipse(Point(0, 0), 3, 2)
|
291 |
+
assert elip.tangent_lines(Point(3, 0)) == \
|
292 |
+
[Line(Point(3, 0), Point(3, -12))]
|
293 |
+
|
294 |
+
e1 = Ellipse(Point(0, 0), 5, 10)
|
295 |
+
e2 = Ellipse(Point(2, 1), 4, 8)
|
296 |
+
a = Rational(53, 17)
|
297 |
+
c = 2*sqrt(3991)/17
|
298 |
+
ans = [Point(a - c/8, a/2 + c), Point(a + c/8, a/2 - c)]
|
299 |
+
assert e1.intersection(e2) == ans
|
300 |
+
e2 = Ellipse(Point(x, y), 4, 8)
|
301 |
+
c = sqrt(3991)
|
302 |
+
ans = [Point(-c/68 + a, c*Rational(2, 17) + a/2), Point(c/68 + a, c*Rational(-2, 17) + a/2)]
|
303 |
+
assert [p.subs({x: 2, y:1}) for p in e1.intersection(e2)] == ans
|
304 |
+
|
305 |
+
# Combinations of above
|
306 |
+
assert e3.is_tangent(e3.tangent_lines(p1 + Point(y1, 0))[0])
|
307 |
+
|
308 |
+
e = Ellipse((1, 2), 3, 2)
|
309 |
+
assert e.tangent_lines(Point(10, 0)) == \
|
310 |
+
[Line(Point(10, 0), Point(1, 0)),
|
311 |
+
Line(Point(10, 0), Point(Rational(14, 5), Rational(18, 5)))]
|
312 |
+
|
313 |
+
# encloses_point
|
314 |
+
e = Ellipse((0, 0), 1, 2)
|
315 |
+
assert e.encloses_point(e.center)
|
316 |
+
assert e.encloses_point(e.center + Point(0, e.vradius - Rational(1, 10)))
|
317 |
+
assert e.encloses_point(e.center + Point(e.hradius - Rational(1, 10), 0))
|
318 |
+
assert e.encloses_point(e.center + Point(e.hradius, 0)) is False
|
319 |
+
assert e.encloses_point(
|
320 |
+
e.center + Point(e.hradius + Rational(1, 10), 0)) is False
|
321 |
+
e = Ellipse((0, 0), 2, 1)
|
322 |
+
assert e.encloses_point(e.center)
|
323 |
+
assert e.encloses_point(e.center + Point(0, e.vradius - Rational(1, 10)))
|
324 |
+
assert e.encloses_point(e.center + Point(e.hradius - Rational(1, 10), 0))
|
325 |
+
assert e.encloses_point(e.center + Point(e.hradius, 0)) is False
|
326 |
+
assert e.encloses_point(
|
327 |
+
e.center + Point(e.hradius + Rational(1, 10), 0)) is False
|
328 |
+
assert c1.encloses_point(Point(1, 0)) is False
|
329 |
+
assert c1.encloses_point(Point(0.3, 0.4)) is True
|
330 |
+
|
331 |
+
assert e.scale(2, 3) == Ellipse((0, 0), 4, 3)
|
332 |
+
assert e.scale(3, 6) == Ellipse((0, 0), 6, 6)
|
333 |
+
assert e.rotate(pi) == e
|
334 |
+
assert e.rotate(pi, (1, 2)) == Ellipse(Point(2, 4), 2, 1)
|
335 |
+
raises(NotImplementedError, lambda: e.rotate(pi/3))
|
336 |
+
|
337 |
+
# Circle rotation tests (Issue #11743)
|
338 |
+
# Link - https://github.com/sympy/sympy/issues/11743
|
339 |
+
cir = Circle(Point(1, 0), 1)
|
340 |
+
assert cir.rotate(pi/2) == Circle(Point(0, 1), 1)
|
341 |
+
assert cir.rotate(pi/3) == Circle(Point(S.Half, sqrt(3)/2), 1)
|
342 |
+
assert cir.rotate(pi/3, Point(1, 0)) == Circle(Point(1, 0), 1)
|
343 |
+
assert cir.rotate(pi/3, Point(0, 1)) == Circle(Point(S.Half + sqrt(3)/2, S.Half + sqrt(3)/2), 1)
|
344 |
+
|
345 |
+
|
346 |
+
def test_construction():
|
347 |
+
e1 = Ellipse(hradius=2, vradius=1, eccentricity=None)
|
348 |
+
assert e1.eccentricity == sqrt(3)/2
|
349 |
+
|
350 |
+
e2 = Ellipse(hradius=2, vradius=None, eccentricity=sqrt(3)/2)
|
351 |
+
assert e2.vradius == 1
|
352 |
+
|
353 |
+
e3 = Ellipse(hradius=None, vradius=1, eccentricity=sqrt(3)/2)
|
354 |
+
assert e3.hradius == 2
|
355 |
+
|
356 |
+
# filter(None, iterator) filters out anything falsey, including 0
|
357 |
+
# eccentricity would be filtered out in this case and the constructor would throw an error
|
358 |
+
e4 = Ellipse(Point(0, 0), hradius=1, eccentricity=0)
|
359 |
+
assert e4.vradius == 1
|
360 |
+
|
361 |
+
#tests for eccentricity > 1
|
362 |
+
raises(GeometryError, lambda: Ellipse(Point(3, 1), hradius=3, eccentricity = S(3)/2))
|
363 |
+
raises(GeometryError, lambda: Ellipse(Point(3, 1), hradius=3, eccentricity=sec(5)))
|
364 |
+
raises(GeometryError, lambda: Ellipse(Point(3, 1), hradius=3, eccentricity=S.Pi-S(2)))
|
365 |
+
|
366 |
+
#tests for eccentricity = 1
|
367 |
+
#if vradius is not defined
|
368 |
+
assert Ellipse(None, 1, None, 1).length == 2
|
369 |
+
#if hradius is not defined
|
370 |
+
raises(GeometryError, lambda: Ellipse(None, None, 1, eccentricity = 1))
|
371 |
+
|
372 |
+
#tests for eccentricity < 0
|
373 |
+
raises(GeometryError, lambda: Ellipse(Point(3, 1), hradius=3, eccentricity = -3))
|
374 |
+
raises(GeometryError, lambda: Ellipse(Point(3, 1), hradius=3, eccentricity = -0.5))
|
375 |
+
|
376 |
+
def test_ellipse_random_point():
|
377 |
+
y1 = Symbol('y1', real=True)
|
378 |
+
e3 = Ellipse(Point(0, 0), y1, y1)
|
379 |
+
rx, ry = Symbol('rx'), Symbol('ry')
|
380 |
+
for ind in range(0, 5):
|
381 |
+
r = e3.random_point()
|
382 |
+
# substitution should give zero*y1**2
|
383 |
+
assert e3.equation(rx, ry).subs(zip((rx, ry), r.args)).equals(0)
|
384 |
+
# test for the case with seed
|
385 |
+
r = e3.random_point(seed=1)
|
386 |
+
assert e3.equation(rx, ry).subs(zip((rx, ry), r.args)).equals(0)
|
387 |
+
|
388 |
+
|
389 |
+
def test_repr():
|
390 |
+
assert repr(Circle((0, 1), 2)) == 'Circle(Point2D(0, 1), 2)'
|
391 |
+
|
392 |
+
|
393 |
+
def test_transform():
|
394 |
+
c = Circle((1, 1), 2)
|
395 |
+
assert c.scale(-1) == Circle((-1, 1), 2)
|
396 |
+
assert c.scale(y=-1) == Circle((1, -1), 2)
|
397 |
+
assert c.scale(2) == Ellipse((2, 1), 4, 2)
|
398 |
+
|
399 |
+
assert Ellipse((0, 0), 2, 3).scale(2, 3, (4, 5)) == \
|
400 |
+
Ellipse(Point(-4, -10), 4, 9)
|
401 |
+
assert Circle((0, 0), 2).scale(2, 3, (4, 5)) == \
|
402 |
+
Ellipse(Point(-4, -10), 4, 6)
|
403 |
+
assert Ellipse((0, 0), 2, 3).scale(3, 3, (4, 5)) == \
|
404 |
+
Ellipse(Point(-8, -10), 6, 9)
|
405 |
+
assert Circle((0, 0), 2).scale(3, 3, (4, 5)) == \
|
406 |
+
Circle(Point(-8, -10), 6)
|
407 |
+
assert Circle(Point(-8, -10), 6).scale(Rational(1, 3), Rational(1, 3), (4, 5)) == \
|
408 |
+
Circle((0, 0), 2)
|
409 |
+
assert Circle((0, 0), 2).translate(4, 5) == \
|
410 |
+
Circle((4, 5), 2)
|
411 |
+
assert Circle((0, 0), 2).scale(3, 3) == \
|
412 |
+
Circle((0, 0), 6)
|
413 |
+
|
414 |
+
|
415 |
+
def test_bounds():
|
416 |
+
e1 = Ellipse(Point(0, 0), 3, 5)
|
417 |
+
e2 = Ellipse(Point(2, -2), 7, 7)
|
418 |
+
c1 = Circle(Point(2, -2), 7)
|
419 |
+
c2 = Circle(Point(-2, 0), Point(0, 2), Point(2, 0))
|
420 |
+
assert e1.bounds == (-3, -5, 3, 5)
|
421 |
+
assert e2.bounds == (-5, -9, 9, 5)
|
422 |
+
assert c1.bounds == (-5, -9, 9, 5)
|
423 |
+
assert c2.bounds == (-2, -2, 2, 2)
|
424 |
+
|
425 |
+
|
426 |
+
def test_reflect():
|
427 |
+
b = Symbol('b')
|
428 |
+
m = Symbol('m')
|
429 |
+
l = Line((0, b), slope=m)
|
430 |
+
t1 = Triangle((0, 0), (1, 0), (2, 3))
|
431 |
+
assert t1.area == -t1.reflect(l).area
|
432 |
+
e = Ellipse((1, 0), 1, 2)
|
433 |
+
assert e.area == -e.reflect(Line((1, 0), slope=0)).area
|
434 |
+
assert e.area == -e.reflect(Line((1, 0), slope=oo)).area
|
435 |
+
raises(NotImplementedError, lambda: e.reflect(Line((1, 0), slope=m)))
|
436 |
+
assert Circle((0, 1), 1).reflect(Line((0, 0), (1, 1))) == Circle(Point2D(1, 0), -1)
|
437 |
+
|
438 |
+
|
439 |
+
def test_is_tangent():
|
440 |
+
e1 = Ellipse(Point(0, 0), 3, 5)
|
441 |
+
c1 = Circle(Point(2, -2), 7)
|
442 |
+
assert e1.is_tangent(Point(0, 0)) is False
|
443 |
+
assert e1.is_tangent(Point(3, 0)) is False
|
444 |
+
assert e1.is_tangent(e1) is True
|
445 |
+
assert e1.is_tangent(Ellipse((0, 0), 1, 2)) is False
|
446 |
+
assert e1.is_tangent(Ellipse((0, 0), 3, 2)) is True
|
447 |
+
assert c1.is_tangent(Ellipse((2, -2), 7, 1)) is True
|
448 |
+
assert c1.is_tangent(Circle((11, -2), 2)) is True
|
449 |
+
assert c1.is_tangent(Circle((7, -2), 2)) is True
|
450 |
+
assert c1.is_tangent(Ray((-5, -2), (-15, -20))) is False
|
451 |
+
assert c1.is_tangent(Ray((-3, -2), (-15, -20))) is False
|
452 |
+
assert c1.is_tangent(Ray((-3, -22), (15, 20))) is False
|
453 |
+
assert c1.is_tangent(Ray((9, 20), (9, -20))) is True
|
454 |
+
assert e1.is_tangent(Segment((2, 2), (-7, 7))) is False
|
455 |
+
assert e1.is_tangent(Segment((0, 0), (1, 2))) is False
|
456 |
+
assert c1.is_tangent(Segment((0, 0), (-5, -2))) is False
|
457 |
+
assert e1.is_tangent(Segment((3, 0), (12, 12))) is False
|
458 |
+
assert e1.is_tangent(Segment((12, 12), (3, 0))) is False
|
459 |
+
assert e1.is_tangent(Segment((-3, 0), (3, 0))) is False
|
460 |
+
assert e1.is_tangent(Segment((-3, 5), (3, 5))) is True
|
461 |
+
assert e1.is_tangent(Line((10, 0), (10, 10))) is False
|
462 |
+
assert e1.is_tangent(Line((0, 0), (1, 1))) is False
|
463 |
+
assert e1.is_tangent(Line((-3, 0), (-2.99, -0.001))) is False
|
464 |
+
assert e1.is_tangent(Line((-3, 0), (-3, 1))) is True
|
465 |
+
assert e1.is_tangent(Polygon((0, 0), (5, 5), (5, -5))) is False
|
466 |
+
assert e1.is_tangent(Polygon((-100, -50), (-40, -334), (-70, -52))) is False
|
467 |
+
assert e1.is_tangent(Polygon((-3, 0), (3, 0), (0, 1))) is False
|
468 |
+
assert e1.is_tangent(Polygon((-3, 0), (3, 0), (0, 5))) is False
|
469 |
+
assert e1.is_tangent(Polygon((-3, 0), (0, -5), (3, 0), (0, 5))) is False
|
470 |
+
assert e1.is_tangent(Polygon((-3, -5), (-3, 5), (3, 5), (3, -5))) is True
|
471 |
+
assert c1.is_tangent(Polygon((-3, -5), (-3, 5), (3, 5), (3, -5))) is False
|
472 |
+
assert e1.is_tangent(Polygon((0, 0), (3, 0), (7, 7), (0, 5))) is False
|
473 |
+
assert e1.is_tangent(Polygon((3, 12), (3, -12), (6, 5))) is True
|
474 |
+
assert e1.is_tangent(Polygon((3, 12), (3, -12), (0, -5), (0, 5))) is False
|
475 |
+
assert e1.is_tangent(Polygon((3, 0), (5, 7), (6, -5))) is False
|
476 |
+
raises(TypeError, lambda: e1.is_tangent(Point(0, 0, 0)))
|
477 |
+
raises(TypeError, lambda: e1.is_tangent(Rational(5)))
|
478 |
+
|
479 |
+
|
480 |
+
def test_parameter_value():
|
481 |
+
t = Symbol('t')
|
482 |
+
e = Ellipse(Point(0, 0), 3, 5)
|
483 |
+
assert e.parameter_value((3, 0), t) == {t: 0}
|
484 |
+
raises(ValueError, lambda: e.parameter_value((4, 0), t))
|
485 |
+
|
486 |
+
|
487 |
+
@slow
|
488 |
+
def test_second_moment_of_area():
|
489 |
+
x, y = symbols('x, y')
|
490 |
+
e = Ellipse(Point(0, 0), 5, 4)
|
491 |
+
I_yy = 2*4*integrate(sqrt(25 - x**2)*x**2, (x, -5, 5))/5
|
492 |
+
I_xx = 2*5*integrate(sqrt(16 - y**2)*y**2, (y, -4, 4))/4
|
493 |
+
Y = 3*sqrt(1 - x**2/5**2)
|
494 |
+
I_xy = integrate(integrate(y, (y, -Y, Y))*x, (x, -5, 5))
|
495 |
+
assert I_yy == e.second_moment_of_area()[1]
|
496 |
+
assert I_xx == e.second_moment_of_area()[0]
|
497 |
+
assert I_xy == e.second_moment_of_area()[2]
|
498 |
+
#checking for other point
|
499 |
+
t1 = e.second_moment_of_area(Point(6,5))
|
500 |
+
t2 = (580*pi, 845*pi, 600*pi)
|
501 |
+
assert t1==t2
|
502 |
+
|
503 |
+
|
504 |
+
def test_section_modulus_and_polar_second_moment_of_area():
|
505 |
+
d = Symbol('d', positive=True)
|
506 |
+
c = Circle((3, 7), 8)
|
507 |
+
assert c.polar_second_moment_of_area() == 2048*pi
|
508 |
+
assert c.section_modulus() == (128*pi, 128*pi)
|
509 |
+
c = Circle((2, 9), d/2)
|
510 |
+
assert c.polar_second_moment_of_area() == pi*d**3*Abs(d)/64 + pi*d*Abs(d)**3/64
|
511 |
+
assert c.section_modulus() == (pi*d**3/S(32), pi*d**3/S(32))
|
512 |
+
|
513 |
+
a, b = symbols('a, b', positive=True)
|
514 |
+
e = Ellipse((4, 6), a, b)
|
515 |
+
assert e.section_modulus() == (pi*a*b**2/S(4), pi*a**2*b/S(4))
|
516 |
+
assert e.polar_second_moment_of_area() == pi*a**3*b/S(4) + pi*a*b**3/S(4)
|
517 |
+
e = e.rotate(pi/2) # no change in polar and section modulus
|
518 |
+
assert e.section_modulus() == (pi*a**2*b/S(4), pi*a*b**2/S(4))
|
519 |
+
assert e.polar_second_moment_of_area() == pi*a**3*b/S(4) + pi*a*b**3/S(4)
|
520 |
+
|
521 |
+
e = Ellipse((a, b), 2, 6)
|
522 |
+
assert e.section_modulus() == (18*pi, 6*pi)
|
523 |
+
assert e.polar_second_moment_of_area() == 120*pi
|
524 |
+
|
525 |
+
e = Ellipse(Point(0, 0), 2, 2)
|
526 |
+
assert e.section_modulus() == (2*pi, 2*pi)
|
527 |
+
assert e.section_modulus(Point(2, 2)) == (2*pi, 2*pi)
|
528 |
+
assert e.section_modulus((2, 2)) == (2*pi, 2*pi)
|
529 |
+
|
530 |
+
|
531 |
+
def test_circumference():
|
532 |
+
M = Symbol('M')
|
533 |
+
m = Symbol('m')
|
534 |
+
assert Ellipse(Point(0, 0), M, m).circumference == 4 * M * elliptic_e((M ** 2 - m ** 2) / M**2)
|
535 |
+
|
536 |
+
assert Ellipse(Point(0, 0), 5, 4).circumference == 20 * elliptic_e(S(9) / 25)
|
537 |
+
|
538 |
+
# circle
|
539 |
+
assert Ellipse(None, 1, None, 0).circumference == 2*pi
|
540 |
+
|
541 |
+
# test numerically
|
542 |
+
assert abs(Ellipse(None, hradius=5, vradius=3).circumference.evalf(16) - 25.52699886339813) < 1e-10
|
543 |
+
|
544 |
+
|
545 |
+
def test_issue_15259():
|
546 |
+
assert Circle((1, 2), 0) == Point(1, 2)
|
547 |
+
|
548 |
+
|
549 |
+
def test_issue_15797_equals():
|
550 |
+
Ri = 0.024127189424130748
|
551 |
+
Ci = (0.0864931002830291, 0.0819863295239654)
|
552 |
+
A = Point(0, 0.0578591400998346)
|
553 |
+
c = Circle(Ci, Ri) # evaluated
|
554 |
+
assert c.is_tangent(c.tangent_lines(A)[0]) == True
|
555 |
+
assert c.center.x.is_Rational
|
556 |
+
assert c.center.y.is_Rational
|
557 |
+
assert c.radius.is_Rational
|
558 |
+
u = Circle(Ci, Ri, evaluate=False) # unevaluated
|
559 |
+
assert u.center.x.is_Float
|
560 |
+
assert u.center.y.is_Float
|
561 |
+
assert u.radius.is_Float
|
562 |
+
|
563 |
+
|
564 |
+
def test_auxiliary_circle():
|
565 |
+
x, y, a, b = symbols('x y a b')
|
566 |
+
e = Ellipse((x, y), a, b)
|
567 |
+
# the general result
|
568 |
+
assert e.auxiliary_circle() == Circle((x, y), Max(a, b))
|
569 |
+
# a special case where Ellipse is a Circle
|
570 |
+
assert Circle((3, 4), 8).auxiliary_circle() == Circle((3, 4), 8)
|
571 |
+
|
572 |
+
|
573 |
+
def test_director_circle():
|
574 |
+
x, y, a, b = symbols('x y a b')
|
575 |
+
e = Ellipse((x, y), a, b)
|
576 |
+
# the general result
|
577 |
+
assert e.director_circle() == Circle((x, y), sqrt(a**2 + b**2))
|
578 |
+
# a special case where Ellipse is a Circle
|
579 |
+
assert Circle((3, 4), 8).director_circle() == Circle((3, 4), 8*sqrt(2))
|
580 |
+
|
581 |
+
|
582 |
+
def test_evolute():
|
583 |
+
#ellipse centered at h,k
|
584 |
+
x, y, h, k = symbols('x y h k',real = True)
|
585 |
+
a, b = symbols('a b')
|
586 |
+
e = Ellipse(Point(h, k), a, b)
|
587 |
+
t1 = (e.hradius*(x - e.center.x))**Rational(2, 3)
|
588 |
+
t2 = (e.vradius*(y - e.center.y))**Rational(2, 3)
|
589 |
+
E = t1 + t2 - (e.hradius**2 - e.vradius**2)**Rational(2, 3)
|
590 |
+
assert e.evolute() == E
|
591 |
+
#Numerical Example
|
592 |
+
e = Ellipse(Point(1, 1), 6, 3)
|
593 |
+
t1 = (6*(x - 1))**Rational(2, 3)
|
594 |
+
t2 = (3*(y - 1))**Rational(2, 3)
|
595 |
+
E = t1 + t2 - (27)**Rational(2, 3)
|
596 |
+
assert e.evolute() == E
|
597 |
+
|
598 |
+
|
599 |
+
def test_svg():
|
600 |
+
e1 = Ellipse(Point(1, 0), 3, 2)
|
601 |
+
assert e1._svg(2, "#FFAAFF") == '<ellipse fill="#FFAAFF" stroke="#555555" stroke-width="4.0" opacity="0.6" cx="1.00000000000000" cy="0" rx="3.00000000000000" ry="2.00000000000000"/>'
|
env-llmeval/lib/python3.10/site-packages/sympy/geometry/tests/test_entity.py
ADDED
@@ -0,0 +1,120 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from sympy.core.numbers import (Rational, pi)
|
2 |
+
from sympy.core.singleton import S
|
3 |
+
from sympy.core.symbol import Symbol
|
4 |
+
from sympy.geometry import (Circle, Ellipse, Point, Line, Parabola,
|
5 |
+
Polygon, Ray, RegularPolygon, Segment, Triangle, Plane, Curve)
|
6 |
+
from sympy.geometry.entity import scale, GeometryEntity
|
7 |
+
from sympy.testing.pytest import raises
|
8 |
+
|
9 |
+
|
10 |
+
def test_entity():
|
11 |
+
x = Symbol('x', real=True)
|
12 |
+
y = Symbol('y', real=True)
|
13 |
+
|
14 |
+
assert GeometryEntity(x, y) in GeometryEntity(x, y)
|
15 |
+
raises(NotImplementedError, lambda: Point(0, 0) in GeometryEntity(x, y))
|
16 |
+
|
17 |
+
assert GeometryEntity(x, y) == GeometryEntity(x, y)
|
18 |
+
assert GeometryEntity(x, y).equals(GeometryEntity(x, y))
|
19 |
+
|
20 |
+
c = Circle((0, 0), 5)
|
21 |
+
assert GeometryEntity.encloses(c, Point(0, 0))
|
22 |
+
assert GeometryEntity.encloses(c, Segment((0, 0), (1, 1)))
|
23 |
+
assert GeometryEntity.encloses(c, Line((0, 0), (1, 1))) is False
|
24 |
+
assert GeometryEntity.encloses(c, Circle((0, 0), 4))
|
25 |
+
assert GeometryEntity.encloses(c, Polygon(Point(0, 0), Point(1, 0), Point(0, 1)))
|
26 |
+
assert GeometryEntity.encloses(c, RegularPolygon(Point(8, 8), 1, 3)) is False
|
27 |
+
|
28 |
+
|
29 |
+
def test_svg():
|
30 |
+
a = Symbol('a')
|
31 |
+
b = Symbol('b')
|
32 |
+
d = Symbol('d')
|
33 |
+
|
34 |
+
entity = Circle(Point(a, b), d)
|
35 |
+
assert entity._repr_svg_() is None
|
36 |
+
|
37 |
+
entity = Circle(Point(0, 0), S.Infinity)
|
38 |
+
assert entity._repr_svg_() is None
|
39 |
+
|
40 |
+
|
41 |
+
def test_subs():
|
42 |
+
x = Symbol('x', real=True)
|
43 |
+
y = Symbol('y', real=True)
|
44 |
+
p = Point(x, 2)
|
45 |
+
q = Point(1, 1)
|
46 |
+
r = Point(3, 4)
|
47 |
+
for o in [p,
|
48 |
+
Segment(p, q),
|
49 |
+
Ray(p, q),
|
50 |
+
Line(p, q),
|
51 |
+
Triangle(p, q, r),
|
52 |
+
RegularPolygon(p, 3, 6),
|
53 |
+
Polygon(p, q, r, Point(5, 4)),
|
54 |
+
Circle(p, 3),
|
55 |
+
Ellipse(p, 3, 4)]:
|
56 |
+
assert 'y' in str(o.subs(x, y))
|
57 |
+
assert p.subs({x: 1}) == Point(1, 2)
|
58 |
+
assert Point(1, 2).subs(Point(1, 2), Point(3, 4)) == Point(3, 4)
|
59 |
+
assert Point(1, 2).subs((1, 2), Point(3, 4)) == Point(3, 4)
|
60 |
+
assert Point(1, 2).subs(Point(1, 2), Point(3, 4)) == Point(3, 4)
|
61 |
+
assert Point(1, 2).subs({(1, 2)}) == Point(2, 2)
|
62 |
+
raises(ValueError, lambda: Point(1, 2).subs(1))
|
63 |
+
raises(ValueError, lambda: Point(1, 1).subs((Point(1, 1), Point(1,
|
64 |
+
2)), 1, 2))
|
65 |
+
|
66 |
+
|
67 |
+
def test_transform():
|
68 |
+
assert scale(1, 2, (3, 4)).tolist() == \
|
69 |
+
[[1, 0, 0], [0, 2, 0], [0, -4, 1]]
|
70 |
+
|
71 |
+
|
72 |
+
def test_reflect_entity_overrides():
|
73 |
+
x = Symbol('x', real=True)
|
74 |
+
y = Symbol('y', real=True)
|
75 |
+
b = Symbol('b')
|
76 |
+
m = Symbol('m')
|
77 |
+
l = Line((0, b), slope=m)
|
78 |
+
p = Point(x, y)
|
79 |
+
r = p.reflect(l)
|
80 |
+
c = Circle((x, y), 3)
|
81 |
+
cr = c.reflect(l)
|
82 |
+
assert cr == Circle(r, -3)
|
83 |
+
assert c.area == -cr.area
|
84 |
+
|
85 |
+
pent = RegularPolygon((1, 2), 1, 5)
|
86 |
+
slope = S.ComplexInfinity
|
87 |
+
while slope is S.ComplexInfinity:
|
88 |
+
slope = Rational(*(x._random()/2).as_real_imag())
|
89 |
+
l = Line(pent.vertices[1], slope=slope)
|
90 |
+
rpent = pent.reflect(l)
|
91 |
+
assert rpent.center == pent.center.reflect(l)
|
92 |
+
rvert = [i.reflect(l) for i in pent.vertices]
|
93 |
+
for v in rpent.vertices:
|
94 |
+
for i in range(len(rvert)):
|
95 |
+
ri = rvert[i]
|
96 |
+
if ri.equals(v):
|
97 |
+
rvert.remove(ri)
|
98 |
+
break
|
99 |
+
assert not rvert
|
100 |
+
assert pent.area.equals(-rpent.area)
|
101 |
+
|
102 |
+
|
103 |
+
def test_geometry_EvalfMixin():
|
104 |
+
x = pi
|
105 |
+
t = Symbol('t')
|
106 |
+
for g in [
|
107 |
+
Point(x, x),
|
108 |
+
Plane(Point(0, x, 0), (0, 0, x)),
|
109 |
+
Curve((x*t, x), (t, 0, x)),
|
110 |
+
Ellipse((x, x), x, -x),
|
111 |
+
Circle((x, x), x),
|
112 |
+
Line((0, x), (x, 0)),
|
113 |
+
Segment((0, x), (x, 0)),
|
114 |
+
Ray((0, x), (x, 0)),
|
115 |
+
Parabola((0, x), Line((-x, 0), (x, 0))),
|
116 |
+
Polygon((0, 0), (0, x), (x, 0), (x, x)),
|
117 |
+
RegularPolygon((0, x), x, 4, x),
|
118 |
+
Triangle((0, 0), (x, 0), (x, x)),
|
119 |
+
]:
|
120 |
+
assert str(g).replace('pi', '3.1') == str(g.n(2))
|
env-llmeval/lib/python3.10/site-packages/sympy/geometry/tests/test_geometrysets.py
ADDED
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from sympy.core.numbers import Rational
|
2 |
+
from sympy.core.singleton import S
|
3 |
+
from sympy.geometry import Circle, Line, Point, Polygon, Segment
|
4 |
+
from sympy.sets import FiniteSet, Union, Intersection, EmptySet
|
5 |
+
|
6 |
+
|
7 |
+
def test_booleans():
|
8 |
+
""" test basic unions and intersections """
|
9 |
+
half = S.Half
|
10 |
+
|
11 |
+
p1, p2, p3, p4 = map(Point, [(0, 0), (1, 0), (5, 1), (0, 1)])
|
12 |
+
p5, p6, p7 = map(Point, [(3, 2), (1, -1), (0, 2)])
|
13 |
+
l1 = Line(Point(0,0), Point(1,1))
|
14 |
+
l2 = Line(Point(half, half), Point(5,5))
|
15 |
+
l3 = Line(p2, p3)
|
16 |
+
l4 = Line(p3, p4)
|
17 |
+
poly1 = Polygon(p1, p2, p3, p4)
|
18 |
+
poly2 = Polygon(p5, p6, p7)
|
19 |
+
poly3 = Polygon(p1, p2, p5)
|
20 |
+
assert Union(l1, l2).equals(l1)
|
21 |
+
assert Intersection(l1, l2).equals(l1)
|
22 |
+
assert Intersection(l1, l4) == FiniteSet(Point(1,1))
|
23 |
+
assert Intersection(Union(l1, l4), l3) == FiniteSet(Point(Rational(-1, 3), Rational(-1, 3)), Point(5, 1))
|
24 |
+
assert Intersection(l1, FiniteSet(Point(7,-7))) == EmptySet
|
25 |
+
assert Intersection(Circle(Point(0,0), 3), Line(p1,p2)) == FiniteSet(Point(-3,0), Point(3,0))
|
26 |
+
assert Intersection(l1, FiniteSet(p1)) == FiniteSet(p1)
|
27 |
+
assert Union(l1, FiniteSet(p1)) == l1
|
28 |
+
|
29 |
+
fs = FiniteSet(Point(Rational(1, 3), 1), Point(Rational(2, 3), 0), Point(Rational(9, 5), Rational(1, 5)), Point(Rational(7, 3), 1))
|
30 |
+
# test the intersection of polygons
|
31 |
+
assert Intersection(poly1, poly2) == fs
|
32 |
+
# make sure if we union polygons with subsets, the subsets go away
|
33 |
+
assert Union(poly1, poly2, fs) == Union(poly1, poly2)
|
34 |
+
# make sure that if we union with a FiniteSet that isn't a subset,
|
35 |
+
# that the points in the intersection stop being listed
|
36 |
+
assert Union(poly1, FiniteSet(Point(0,0), Point(3,5))) == Union(poly1, FiniteSet(Point(3,5)))
|
37 |
+
# intersect two polygons that share an edge
|
38 |
+
assert Intersection(poly1, poly3) == Union(FiniteSet(Point(Rational(3, 2), 1), Point(2, 1)), Segment(Point(0, 0), Point(1, 0)))
|
env-llmeval/lib/python3.10/site-packages/sympy/geometry/tests/test_line.py
ADDED
@@ -0,0 +1,852 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from sympy.core.numbers import (Float, Rational, oo, pi)
|
2 |
+
from sympy.core.relational import Eq
|
3 |
+
from sympy.core.singleton import S
|
4 |
+
from sympy.core.symbol import (Symbol, symbols)
|
5 |
+
from sympy.functions.elementary.miscellaneous import sqrt
|
6 |
+
from sympy.functions.elementary.trigonometric import (acos, cos, sin)
|
7 |
+
from sympy.sets import EmptySet
|
8 |
+
from sympy.simplify.simplify import simplify
|
9 |
+
from sympy.functions.elementary.trigonometric import tan
|
10 |
+
from sympy.geometry import (Circle, GeometryError, Line, Point, Ray,
|
11 |
+
Segment, Triangle, intersection, Point3D, Line3D, Ray3D, Segment3D,
|
12 |
+
Point2D, Line2D)
|
13 |
+
from sympy.geometry.line import Undecidable
|
14 |
+
from sympy.geometry.polygon import _asa as asa
|
15 |
+
from sympy.utilities.iterables import cartes
|
16 |
+
from sympy.testing.pytest import raises, warns
|
17 |
+
|
18 |
+
|
19 |
+
x = Symbol('x', real=True)
|
20 |
+
y = Symbol('y', real=True)
|
21 |
+
z = Symbol('z', real=True)
|
22 |
+
k = Symbol('k', real=True)
|
23 |
+
x1 = Symbol('x1', real=True)
|
24 |
+
y1 = Symbol('y1', real=True)
|
25 |
+
t = Symbol('t', real=True)
|
26 |
+
a, b = symbols('a,b', real=True)
|
27 |
+
m = symbols('m', real=True)
|
28 |
+
|
29 |
+
|
30 |
+
def test_object_from_equation():
|
31 |
+
from sympy.abc import x, y, a, b
|
32 |
+
assert Line(3*x + y + 18) == Line2D(Point2D(0, -18), Point2D(1, -21))
|
33 |
+
assert Line(3*x + 5 * y + 1) == Line2D(
|
34 |
+
Point2D(0, Rational(-1, 5)), Point2D(1, Rational(-4, 5)))
|
35 |
+
assert Line(3*a + b + 18, x="a", y="b") == Line2D(
|
36 |
+
Point2D(0, -18), Point2D(1, -21))
|
37 |
+
assert Line(3*x + y) == Line2D(Point2D(0, 0), Point2D(1, -3))
|
38 |
+
assert Line(x + y) == Line2D(Point2D(0, 0), Point2D(1, -1))
|
39 |
+
assert Line(Eq(3*a + b, -18), x="a", y=b) == Line2D(
|
40 |
+
Point2D(0, -18), Point2D(1, -21))
|
41 |
+
# issue 22361
|
42 |
+
assert Line(x - 1) == Line2D(Point2D(1, 0), Point2D(1, 1))
|
43 |
+
assert Line(2*x - 2, y=x) == Line2D(Point2D(0, 1), Point2D(1, 1))
|
44 |
+
assert Line(y) == Line2D(Point2D(0, 0), Point2D(1, 0))
|
45 |
+
assert Line(2*y, x=y) == Line2D(Point2D(0, 0), Point2D(0, 1))
|
46 |
+
assert Line(y, x=y) == Line2D(Point2D(0, 0), Point2D(0, 1))
|
47 |
+
raises(ValueError, lambda: Line(x / y))
|
48 |
+
raises(ValueError, lambda: Line(a / b, x='a', y='b'))
|
49 |
+
raises(ValueError, lambda: Line(y / x))
|
50 |
+
raises(ValueError, lambda: Line(b / a, x='a', y='b'))
|
51 |
+
raises(ValueError, lambda: Line((x + 1)**2 + y))
|
52 |
+
|
53 |
+
|
54 |
+
def feq(a, b):
|
55 |
+
"""Test if two floating point values are 'equal'."""
|
56 |
+
t_float = Float("1.0E-10")
|
57 |
+
return -t_float < a - b < t_float
|
58 |
+
|
59 |
+
|
60 |
+
def test_angle_between():
|
61 |
+
a = Point(1, 2, 3, 4)
|
62 |
+
b = a.orthogonal_direction
|
63 |
+
o = a.origin
|
64 |
+
assert feq(Line.angle_between(Line(Point(0, 0), Point(1, 1)),
|
65 |
+
Line(Point(0, 0), Point(5, 0))).evalf(), pi.evalf() / 4)
|
66 |
+
assert Line(a, o).angle_between(Line(b, o)) == pi / 2
|
67 |
+
z = Point3D(0, 0, 0)
|
68 |
+
assert Line3D.angle_between(Line3D(z, Point3D(1, 1, 1)),
|
69 |
+
Line3D(z, Point3D(5, 0, 0))) == acos(sqrt(3) / 3)
|
70 |
+
# direction of points is used to determine angle
|
71 |
+
assert Line3D.angle_between(Line3D(z, Point3D(1, 1, 1)),
|
72 |
+
Line3D(Point3D(5, 0, 0), z)) == acos(-sqrt(3) / 3)
|
73 |
+
|
74 |
+
|
75 |
+
def test_closing_angle():
|
76 |
+
a = Ray((0, 0), angle=0)
|
77 |
+
b = Ray((1, 2), angle=pi/2)
|
78 |
+
assert a.closing_angle(b) == -pi/2
|
79 |
+
assert b.closing_angle(a) == pi/2
|
80 |
+
assert a.closing_angle(a) == 0
|
81 |
+
|
82 |
+
|
83 |
+
def test_smallest_angle():
|
84 |
+
a = Line(Point(1, 1), Point(1, 2))
|
85 |
+
b = Line(Point(1, 1),Point(2, 3))
|
86 |
+
assert a.smallest_angle_between(b) == acos(2*sqrt(5)/5)
|
87 |
+
|
88 |
+
|
89 |
+
def test_svg():
|
90 |
+
a = Line(Point(1, 1),Point(1, 2))
|
91 |
+
assert a._svg() == '<path fill-rule="evenodd" fill="#66cc99" stroke="#555555" stroke-width="2.0" opacity="0.6" d="M 1.00000000000000,1.00000000000000 L 1.00000000000000,2.00000000000000" marker-start="url(#markerReverseArrow)" marker-end="url(#markerArrow)"/>'
|
92 |
+
a = Segment(Point(1, 0),Point(1, 1))
|
93 |
+
assert a._svg() == '<path fill-rule="evenodd" fill="#66cc99" stroke="#555555" stroke-width="2.0" opacity="0.6" d="M 1.00000000000000,0 L 1.00000000000000,1.00000000000000" />'
|
94 |
+
a = Ray(Point(2, 3), Point(3, 5))
|
95 |
+
assert a._svg() == '<path fill-rule="evenodd" fill="#66cc99" stroke="#555555" stroke-width="2.0" opacity="0.6" d="M 2.00000000000000,3.00000000000000 L 3.00000000000000,5.00000000000000" marker-start="url(#markerCircle)" marker-end="url(#markerArrow)"/>'
|
96 |
+
|
97 |
+
|
98 |
+
def test_arbitrary_point():
|
99 |
+
l1 = Line3D(Point3D(0, 0, 0), Point3D(1, 1, 1))
|
100 |
+
l2 = Line(Point(x1, x1), Point(y1, y1))
|
101 |
+
assert l2.arbitrary_point() in l2
|
102 |
+
assert Ray((1, 1), angle=pi / 4).arbitrary_point() == \
|
103 |
+
Point(t + 1, t + 1)
|
104 |
+
assert Segment((1, 1), (2, 3)).arbitrary_point() == Point(1 + t, 1 + 2 * t)
|
105 |
+
assert l1.perpendicular_segment(l1.arbitrary_point()) == l1.arbitrary_point()
|
106 |
+
assert Ray3D((1, 1, 1), direction_ratio=[1, 2, 3]).arbitrary_point() == \
|
107 |
+
Point3D(t + 1, 2 * t + 1, 3 * t + 1)
|
108 |
+
assert Segment3D(Point3D(0, 0, 0), Point3D(1, 1, 1)).midpoint == \
|
109 |
+
Point3D(S.Half, S.Half, S.Half)
|
110 |
+
assert Segment3D(Point3D(x1, x1, x1), Point3D(y1, y1, y1)).length == sqrt(3) * sqrt((x1 - y1) ** 2)
|
111 |
+
assert Segment3D((1, 1, 1), (2, 3, 4)).arbitrary_point() == \
|
112 |
+
Point3D(t + 1, 2 * t + 1, 3 * t + 1)
|
113 |
+
raises(ValueError, (lambda: Line((x, 1), (2, 3)).arbitrary_point(x)))
|
114 |
+
|
115 |
+
|
116 |
+
def test_are_concurrent_2d():
|
117 |
+
l1 = Line(Point(0, 0), Point(1, 1))
|
118 |
+
l2 = Line(Point(x1, x1), Point(x1, 1 + x1))
|
119 |
+
assert Line.are_concurrent(l1) is False
|
120 |
+
assert Line.are_concurrent(l1, l2)
|
121 |
+
assert Line.are_concurrent(l1, l1, l1, l2)
|
122 |
+
assert Line.are_concurrent(l1, l2, Line(Point(5, x1), Point(Rational(-3, 5), x1)))
|
123 |
+
assert Line.are_concurrent(l1, Line(Point(0, 0), Point(-x1, x1)), l2) is False
|
124 |
+
|
125 |
+
|
126 |
+
def test_are_concurrent_3d():
|
127 |
+
p1 = Point3D(0, 0, 0)
|
128 |
+
l1 = Line(p1, Point3D(1, 1, 1))
|
129 |
+
parallel_1 = Line3D(Point3D(0, 0, 0), Point3D(1, 0, 0))
|
130 |
+
parallel_2 = Line3D(Point3D(0, 1, 0), Point3D(1, 1, 0))
|
131 |
+
assert Line3D.are_concurrent(l1) is False
|
132 |
+
assert Line3D.are_concurrent(l1, Line(Point3D(x1, x1, x1), Point3D(y1, y1, y1))) is False
|
133 |
+
assert Line3D.are_concurrent(l1, Line3D(p1, Point3D(x1, x1, x1)),
|
134 |
+
Line(Point3D(x1, x1, x1), Point3D(x1, 1 + x1, 1))) is True
|
135 |
+
assert Line3D.are_concurrent(parallel_1, parallel_2) is False
|
136 |
+
|
137 |
+
|
138 |
+
def test_arguments():
|
139 |
+
"""Functions accepting `Point` objects in `geometry`
|
140 |
+
should also accept tuples, lists, and generators and
|
141 |
+
automatically convert them to points."""
|
142 |
+
from sympy.utilities.iterables import subsets
|
143 |
+
|
144 |
+
singles2d = ((1, 2), [1, 3], Point(1, 5))
|
145 |
+
doubles2d = subsets(singles2d, 2)
|
146 |
+
l2d = Line(Point2D(1, 2), Point2D(2, 3))
|
147 |
+
singles3d = ((1, 2, 3), [1, 2, 4], Point(1, 2, 6))
|
148 |
+
doubles3d = subsets(singles3d, 2)
|
149 |
+
l3d = Line(Point3D(1, 2, 3), Point3D(1, 1, 2))
|
150 |
+
singles4d = ((1, 2, 3, 4), [1, 2, 3, 5], Point(1, 2, 3, 7))
|
151 |
+
doubles4d = subsets(singles4d, 2)
|
152 |
+
l4d = Line(Point(1, 2, 3, 4), Point(2, 2, 2, 2))
|
153 |
+
# test 2D
|
154 |
+
test_single = ['contains', 'distance', 'equals', 'parallel_line', 'perpendicular_line', 'perpendicular_segment',
|
155 |
+
'projection', 'intersection']
|
156 |
+
for p in doubles2d:
|
157 |
+
Line2D(*p)
|
158 |
+
for func in test_single:
|
159 |
+
for p in singles2d:
|
160 |
+
getattr(l2d, func)(p)
|
161 |
+
# test 3D
|
162 |
+
for p in doubles3d:
|
163 |
+
Line3D(*p)
|
164 |
+
for func in test_single:
|
165 |
+
for p in singles3d:
|
166 |
+
getattr(l3d, func)(p)
|
167 |
+
# test 4D
|
168 |
+
for p in doubles4d:
|
169 |
+
Line(*p)
|
170 |
+
for func in test_single:
|
171 |
+
for p in singles4d:
|
172 |
+
getattr(l4d, func)(p)
|
173 |
+
|
174 |
+
|
175 |
+
def test_basic_properties_2d():
|
176 |
+
p1 = Point(0, 0)
|
177 |
+
p2 = Point(1, 1)
|
178 |
+
p10 = Point(2000, 2000)
|
179 |
+
p_r3 = Ray(p1, p2).random_point()
|
180 |
+
p_r4 = Ray(p2, p1).random_point()
|
181 |
+
|
182 |
+
l1 = Line(p1, p2)
|
183 |
+
l3 = Line(Point(x1, x1), Point(x1, 1 + x1))
|
184 |
+
l4 = Line(p1, Point(1, 0))
|
185 |
+
|
186 |
+
r1 = Ray(p1, Point(0, 1))
|
187 |
+
r2 = Ray(Point(0, 1), p1)
|
188 |
+
|
189 |
+
s1 = Segment(p1, p10)
|
190 |
+
p_s1 = s1.random_point()
|
191 |
+
|
192 |
+
assert Line((1, 1), slope=1) == Line((1, 1), (2, 2))
|
193 |
+
assert Line((1, 1), slope=oo) == Line((1, 1), (1, 2))
|
194 |
+
assert Line((1, 1), slope=oo).bounds == (1, 1, 1, 2)
|
195 |
+
assert Line((1, 1), slope=-oo) == Line((1, 1), (1, 2))
|
196 |
+
assert Line(p1, p2).scale(2, 1) == Line(p1, Point(2, 1))
|
197 |
+
assert Line(p1, p2) == Line(p1, p2)
|
198 |
+
assert Line(p1, p2) != Line(p2, p1)
|
199 |
+
assert l1 != Line(Point(x1, x1), Point(y1, y1))
|
200 |
+
assert l1 != l3
|
201 |
+
assert Line(p1, p10) != Line(p10, p1)
|
202 |
+
assert Line(p1, p10) != p1
|
203 |
+
assert p1 in l1 # is p1 on the line l1?
|
204 |
+
assert p1 not in l3
|
205 |
+
assert s1 in Line(p1, p10)
|
206 |
+
assert Ray(Point(0, 0), Point(0, 1)) in Ray(Point(0, 0), Point(0, 2))
|
207 |
+
assert Ray(Point(0, 0), Point(0, 2)) in Ray(Point(0, 0), Point(0, 1))
|
208 |
+
assert Ray(Point(0, 0), Point(0, 2)).xdirection == S.Zero
|
209 |
+
assert Ray(Point(0, 0), Point(1, 2)).xdirection == S.Infinity
|
210 |
+
assert Ray(Point(0, 0), Point(-1, 2)).xdirection == S.NegativeInfinity
|
211 |
+
assert Ray(Point(0, 0), Point(2, 0)).ydirection == S.Zero
|
212 |
+
assert Ray(Point(0, 0), Point(2, 2)).ydirection == S.Infinity
|
213 |
+
assert Ray(Point(0, 0), Point(2, -2)).ydirection == S.NegativeInfinity
|
214 |
+
assert (r1 in s1) is False
|
215 |
+
assert Segment(p1, p2) in s1
|
216 |
+
assert Ray(Point(x1, x1), Point(x1, 1 + x1)) != Ray(p1, Point(-1, 5))
|
217 |
+
assert Segment(p1, p2).midpoint == Point(S.Half, S.Half)
|
218 |
+
assert Segment(p1, Point(-x1, x1)).length == sqrt(2 * (x1 ** 2))
|
219 |
+
|
220 |
+
assert l1.slope == 1
|
221 |
+
assert l3.slope is oo
|
222 |
+
assert l4.slope == 0
|
223 |
+
assert Line(p1, Point(0, 1)).slope is oo
|
224 |
+
assert Line(r1.source, r1.random_point()).slope == r1.slope
|
225 |
+
assert Line(r2.source, r2.random_point()).slope == r2.slope
|
226 |
+
assert Segment(Point(0, -1), Segment(p1, Point(0, 1)).random_point()).slope == Segment(p1, Point(0, 1)).slope
|
227 |
+
|
228 |
+
assert l4.coefficients == (0, 1, 0)
|
229 |
+
assert Line((-x, x), (-x + 1, x - 1)).coefficients == (1, 1, 0)
|
230 |
+
assert Line(p1, Point(0, 1)).coefficients == (1, 0, 0)
|
231 |
+
# issue 7963
|
232 |
+
r = Ray((0, 0), angle=x)
|
233 |
+
assert r.subs(x, 3 * pi / 4) == Ray((0, 0), (-1, 1))
|
234 |
+
assert r.subs(x, 5 * pi / 4) == Ray((0, 0), (-1, -1))
|
235 |
+
assert r.subs(x, -pi / 4) == Ray((0, 0), (1, -1))
|
236 |
+
assert r.subs(x, pi / 2) == Ray((0, 0), (0, 1))
|
237 |
+
assert r.subs(x, -pi / 2) == Ray((0, 0), (0, -1))
|
238 |
+
|
239 |
+
for ind in range(0, 5):
|
240 |
+
assert l3.random_point() in l3
|
241 |
+
|
242 |
+
assert p_r3.x >= p1.x and p_r3.y >= p1.y
|
243 |
+
assert p_r4.x <= p2.x and p_r4.y <= p2.y
|
244 |
+
assert p1.x <= p_s1.x <= p10.x and p1.y <= p_s1.y <= p10.y
|
245 |
+
assert hash(s1) != hash(Segment(p10, p1))
|
246 |
+
|
247 |
+
assert s1.plot_interval() == [t, 0, 1]
|
248 |
+
assert Line(p1, p10).plot_interval() == [t, -5, 5]
|
249 |
+
assert Ray((0, 0), angle=pi / 4).plot_interval() == [t, 0, 10]
|
250 |
+
|
251 |
+
|
252 |
+
def test_basic_properties_3d():
|
253 |
+
p1 = Point3D(0, 0, 0)
|
254 |
+
p2 = Point3D(1, 1, 1)
|
255 |
+
p3 = Point3D(x1, x1, x1)
|
256 |
+
p5 = Point3D(x1, 1 + x1, 1)
|
257 |
+
|
258 |
+
l1 = Line3D(p1, p2)
|
259 |
+
l3 = Line3D(p3, p5)
|
260 |
+
|
261 |
+
r1 = Ray3D(p1, Point3D(-1, 5, 0))
|
262 |
+
r3 = Ray3D(p1, p2)
|
263 |
+
|
264 |
+
s1 = Segment3D(p1, p2)
|
265 |
+
|
266 |
+
assert Line3D((1, 1, 1), direction_ratio=[2, 3, 4]) == Line3D(Point3D(1, 1, 1), Point3D(3, 4, 5))
|
267 |
+
assert Line3D((1, 1, 1), direction_ratio=[1, 5, 7]) == Line3D(Point3D(1, 1, 1), Point3D(2, 6, 8))
|
268 |
+
assert Line3D((1, 1, 1), direction_ratio=[1, 2, 3]) == Line3D(Point3D(1, 1, 1), Point3D(2, 3, 4))
|
269 |
+
assert Line3D(Point3D(0, 0, 0), Point3D(1, 0, 0)).direction_cosine == [1, 0, 0]
|
270 |
+
assert Line3D(Line3D(p1, Point3D(0, 1, 0))) == Line3D(p1, Point3D(0, 1, 0))
|
271 |
+
assert Ray3D(Line3D(Point3D(0, 0, 0), Point3D(1, 0, 0))) == Ray3D(p1, Point3D(1, 0, 0))
|
272 |
+
assert Line3D(p1, p2) != Line3D(p2, p1)
|
273 |
+
assert l1 != l3
|
274 |
+
assert l1 != Line3D(p3, Point3D(y1, y1, y1))
|
275 |
+
assert r3 != r1
|
276 |
+
assert Ray3D(Point3D(0, 0, 0), Point3D(1, 1, 1)) in Ray3D(Point3D(0, 0, 0), Point3D(2, 2, 2))
|
277 |
+
assert Ray3D(Point3D(0, 0, 0), Point3D(2, 2, 2)) in Ray3D(Point3D(0, 0, 0), Point3D(1, 1, 1))
|
278 |
+
assert Ray3D(Point3D(0, 0, 0), Point3D(2, 2, 2)).xdirection == S.Infinity
|
279 |
+
assert Ray3D(Point3D(0, 0, 0), Point3D(2, 2, 2)).ydirection == S.Infinity
|
280 |
+
assert Ray3D(Point3D(0, 0, 0), Point3D(2, 2, 2)).zdirection == S.Infinity
|
281 |
+
assert Ray3D(Point3D(0, 0, 0), Point3D(-2, 2, 2)).xdirection == S.NegativeInfinity
|
282 |
+
assert Ray3D(Point3D(0, 0, 0), Point3D(2, -2, 2)).ydirection == S.NegativeInfinity
|
283 |
+
assert Ray3D(Point3D(0, 0, 0), Point3D(2, 2, -2)).zdirection == S.NegativeInfinity
|
284 |
+
assert Ray3D(Point3D(0, 0, 0), Point3D(0, 2, 2)).xdirection == S.Zero
|
285 |
+
assert Ray3D(Point3D(0, 0, 0), Point3D(2, 0, 2)).ydirection == S.Zero
|
286 |
+
assert Ray3D(Point3D(0, 0, 0), Point3D(2, 2, 0)).zdirection == S.Zero
|
287 |
+
assert p1 in l1
|
288 |
+
assert p1 not in l3
|
289 |
+
|
290 |
+
assert l1.direction_ratio == [1, 1, 1]
|
291 |
+
|
292 |
+
assert s1.midpoint == Point3D(S.Half, S.Half, S.Half)
|
293 |
+
# Test zdirection
|
294 |
+
assert Ray3D(p1, Point3D(0, 0, -1)).zdirection is S.NegativeInfinity
|
295 |
+
|
296 |
+
|
297 |
+
def test_contains():
|
298 |
+
p1 = Point(0, 0)
|
299 |
+
|
300 |
+
r = Ray(p1, Point(4, 4))
|
301 |
+
r1 = Ray3D(p1, Point3D(0, 0, -1))
|
302 |
+
r2 = Ray3D(p1, Point3D(0, 1, 0))
|
303 |
+
r3 = Ray3D(p1, Point3D(0, 0, 1))
|
304 |
+
|
305 |
+
l = Line(Point(0, 1), Point(3, 4))
|
306 |
+
# Segment contains
|
307 |
+
assert Point(0, (a + b) / 2) in Segment((0, a), (0, b))
|
308 |
+
assert Point((a + b) / 2, 0) in Segment((a, 0), (b, 0))
|
309 |
+
assert Point3D(0, 1, 0) in Segment3D((0, 1, 0), (0, 1, 0))
|
310 |
+
assert Point3D(1, 0, 0) in Segment3D((1, 0, 0), (1, 0, 0))
|
311 |
+
assert Segment3D(Point3D(0, 0, 0), Point3D(1, 0, 0)).contains([]) is True
|
312 |
+
assert Segment3D(Point3D(0, 0, 0), Point3D(1, 0, 0)).contains(
|
313 |
+
Segment3D(Point3D(2, 2, 2), Point3D(3, 2, 2))) is False
|
314 |
+
# Line contains
|
315 |
+
assert l.contains(Point(0, 1)) is True
|
316 |
+
assert l.contains((0, 1)) is True
|
317 |
+
assert l.contains((0, 0)) is False
|
318 |
+
# Ray contains
|
319 |
+
assert r.contains(p1) is True
|
320 |
+
assert r.contains((1, 1)) is True
|
321 |
+
assert r.contains((1, 3)) is False
|
322 |
+
assert r.contains(Segment((1, 1), (2, 2))) is True
|
323 |
+
assert r.contains(Segment((1, 2), (2, 5))) is False
|
324 |
+
assert r.contains(Ray((2, 2), (3, 3))) is True
|
325 |
+
assert r.contains(Ray((2, 2), (3, 5))) is False
|
326 |
+
assert r1.contains(Segment3D(p1, Point3D(0, 0, -10))) is True
|
327 |
+
assert r1.contains(Segment3D(Point3D(1, 1, 1), Point3D(2, 2, 2))) is False
|
328 |
+
assert r2.contains(Point3D(0, 0, 0)) is True
|
329 |
+
assert r3.contains(Point3D(0, 0, 0)) is True
|
330 |
+
assert Ray3D(Point3D(1, 1, 1), Point3D(1, 0, 0)).contains([]) is False
|
331 |
+
assert Line3D((0, 0, 0), (x, y, z)).contains((2 * x, 2 * y, 2 * z))
|
332 |
+
with warns(UserWarning, test_stacklevel=False):
|
333 |
+
assert Line3D(p1, Point3D(0, 1, 0)).contains(Point(1.0, 1.0)) is False
|
334 |
+
|
335 |
+
with warns(UserWarning, test_stacklevel=False):
|
336 |
+
assert r3.contains(Point(1.0, 1.0)) is False
|
337 |
+
|
338 |
+
|
339 |
+
def test_contains_nonreal_symbols():
|
340 |
+
u, v, w, z = symbols('u, v, w, z')
|
341 |
+
l = Segment(Point(u, w), Point(v, z))
|
342 |
+
p = Point(u*Rational(2, 3) + v/3, w*Rational(2, 3) + z/3)
|
343 |
+
assert l.contains(p)
|
344 |
+
|
345 |
+
|
346 |
+
def test_distance_2d():
|
347 |
+
p1 = Point(0, 0)
|
348 |
+
p2 = Point(1, 1)
|
349 |
+
half = S.Half
|
350 |
+
|
351 |
+
s1 = Segment(Point(0, 0), Point(1, 1))
|
352 |
+
s2 = Segment(Point(half, half), Point(1, 0))
|
353 |
+
|
354 |
+
r = Ray(p1, p2)
|
355 |
+
|
356 |
+
assert s1.distance(Point(0, 0)) == 0
|
357 |
+
assert s1.distance((0, 0)) == 0
|
358 |
+
assert s2.distance(Point(0, 0)) == 2 ** half / 2
|
359 |
+
assert s2.distance(Point(Rational(3) / 2, Rational(3) / 2)) == 2 ** half
|
360 |
+
assert Line(p1, p2).distance(Point(-1, 1)) == sqrt(2)
|
361 |
+
assert Line(p1, p2).distance(Point(1, -1)) == sqrt(2)
|
362 |
+
assert Line(p1, p2).distance(Point(2, 2)) == 0
|
363 |
+
assert Line(p1, p2).distance((-1, 1)) == sqrt(2)
|
364 |
+
assert Line((0, 0), (0, 1)).distance(p1) == 0
|
365 |
+
assert Line((0, 0), (0, 1)).distance(p2) == 1
|
366 |
+
assert Line((0, 0), (1, 0)).distance(p1) == 0
|
367 |
+
assert Line((0, 0), (1, 0)).distance(p2) == 1
|
368 |
+
assert r.distance(Point(-1, -1)) == sqrt(2)
|
369 |
+
assert r.distance(Point(1, 1)) == 0
|
370 |
+
assert r.distance(Point(-1, 1)) == sqrt(2)
|
371 |
+
assert Ray((1, 1), (2, 2)).distance(Point(1.5, 3)) == 3 * sqrt(2) / 4
|
372 |
+
assert r.distance((1, 1)) == 0
|
373 |
+
|
374 |
+
|
375 |
+
def test_dimension_normalization():
|
376 |
+
with warns(UserWarning, test_stacklevel=False):
|
377 |
+
assert Ray((1, 1), (2, 1, 2)) == Ray((1, 1, 0), (2, 1, 2))
|
378 |
+
|
379 |
+
|
380 |
+
def test_distance_3d():
|
381 |
+
p1, p2 = Point3D(0, 0, 0), Point3D(1, 1, 1)
|
382 |
+
p3 = Point3D(Rational(3) / 2, Rational(3) / 2, Rational(3) / 2)
|
383 |
+
|
384 |
+
s1 = Segment3D(Point3D(0, 0, 0), Point3D(1, 1, 1))
|
385 |
+
s2 = Segment3D(Point3D(S.Half, S.Half, S.Half), Point3D(1, 0, 1))
|
386 |
+
|
387 |
+
r = Ray3D(p1, p2)
|
388 |
+
|
389 |
+
assert s1.distance(p1) == 0
|
390 |
+
assert s2.distance(p1) == sqrt(3) / 2
|
391 |
+
assert s2.distance(p3) == 2 * sqrt(6) / 3
|
392 |
+
assert s1.distance((0, 0, 0)) == 0
|
393 |
+
assert s2.distance((0, 0, 0)) == sqrt(3) / 2
|
394 |
+
assert s1.distance(p1) == 0
|
395 |
+
assert s2.distance(p1) == sqrt(3) / 2
|
396 |
+
assert s2.distance(p3) == 2 * sqrt(6) / 3
|
397 |
+
assert s1.distance((0, 0, 0)) == 0
|
398 |
+
assert s2.distance((0, 0, 0)) == sqrt(3) / 2
|
399 |
+
# Line to point
|
400 |
+
assert Line3D(p1, p2).distance(Point3D(-1, 1, 1)) == 2 * sqrt(6) / 3
|
401 |
+
assert Line3D(p1, p2).distance(Point3D(1, -1, 1)) == 2 * sqrt(6) / 3
|
402 |
+
assert Line3D(p1, p2).distance(Point3D(2, 2, 2)) == 0
|
403 |
+
assert Line3D(p1, p2).distance((2, 2, 2)) == 0
|
404 |
+
assert Line3D(p1, p2).distance((1, -1, 1)) == 2 * sqrt(6) / 3
|
405 |
+
assert Line3D((0, 0, 0), (0, 1, 0)).distance(p1) == 0
|
406 |
+
assert Line3D((0, 0, 0), (0, 1, 0)).distance(p2) == sqrt(2)
|
407 |
+
assert Line3D((0, 0, 0), (1, 0, 0)).distance(p1) == 0
|
408 |
+
assert Line3D((0, 0, 0), (1, 0, 0)).distance(p2) == sqrt(2)
|
409 |
+
# Ray to point
|
410 |
+
assert r.distance(Point3D(-1, -1, -1)) == sqrt(3)
|
411 |
+
assert r.distance(Point3D(1, 1, 1)) == 0
|
412 |
+
assert r.distance((-1, -1, -1)) == sqrt(3)
|
413 |
+
assert r.distance((1, 1, 1)) == 0
|
414 |
+
assert Ray3D((0, 0, 0), (1, 1, 2)).distance((-1, -1, 2)) == 4 * sqrt(3) / 3
|
415 |
+
assert Ray3D((1, 1, 1), (2, 2, 2)).distance(Point3D(1.5, -3, -1)) == Rational(9) / 2
|
416 |
+
assert Ray3D((1, 1, 1), (2, 2, 2)).distance(Point3D(1.5, 3, 1)) == sqrt(78) / 6
|
417 |
+
|
418 |
+
|
419 |
+
def test_equals():
|
420 |
+
p1 = Point(0, 0)
|
421 |
+
p2 = Point(1, 1)
|
422 |
+
|
423 |
+
l1 = Line(p1, p2)
|
424 |
+
l2 = Line((0, 5), slope=m)
|
425 |
+
l3 = Line(Point(x1, x1), Point(x1, 1 + x1))
|
426 |
+
|
427 |
+
assert l1.perpendicular_line(p1.args).equals(Line(Point(0, 0), Point(1, -1)))
|
428 |
+
assert l1.perpendicular_line(p1).equals(Line(Point(0, 0), Point(1, -1)))
|
429 |
+
assert Line(Point(x1, x1), Point(y1, y1)).parallel_line(Point(-x1, x1)). \
|
430 |
+
equals(Line(Point(-x1, x1), Point(-y1, 2 * x1 - y1)))
|
431 |
+
assert l3.parallel_line(p1.args).equals(Line(Point(0, 0), Point(0, -1)))
|
432 |
+
assert l3.parallel_line(p1).equals(Line(Point(0, 0), Point(0, -1)))
|
433 |
+
assert (l2.distance(Point(2, 3)) - 2 * abs(m + 1) / sqrt(m ** 2 + 1)).equals(0)
|
434 |
+
assert Line3D(p1, Point3D(0, 1, 0)).equals(Point(1.0, 1.0)) is False
|
435 |
+
assert Line3D(Point3D(0, 0, 0), Point3D(1, 0, 0)).equals(Line3D(Point3D(-5, 0, 0), Point3D(-1, 0, 0))) is True
|
436 |
+
assert Line3D(Point3D(0, 0, 0), Point3D(1, 0, 0)).equals(Line3D(p1, Point3D(0, 1, 0))) is False
|
437 |
+
assert Ray3D(p1, Point3D(0, 0, -1)).equals(Point(1.0, 1.0)) is False
|
438 |
+
assert Ray3D(p1, Point3D(0, 0, -1)).equals(Ray3D(p1, Point3D(0, 0, -1))) is True
|
439 |
+
assert Line3D((0, 0), (t, t)).perpendicular_line(Point(0, 1, 0)).equals(
|
440 |
+
Line3D(Point3D(0, 1, 0), Point3D(S.Half, S.Half, 0)))
|
441 |
+
assert Line3D((0, 0), (t, t)).perpendicular_segment(Point(0, 1, 0)).equals(Segment3D((0, 1), (S.Half, S.Half)))
|
442 |
+
assert Line3D(p1, Point3D(0, 1, 0)).equals(Point(1.0, 1.0)) is False
|
443 |
+
|
444 |
+
|
445 |
+
def test_equation():
|
446 |
+
p1 = Point(0, 0)
|
447 |
+
p2 = Point(1, 1)
|
448 |
+
l1 = Line(p1, p2)
|
449 |
+
l3 = Line(Point(x1, x1), Point(x1, 1 + x1))
|
450 |
+
|
451 |
+
assert simplify(l1.equation()) in (x - y, y - x)
|
452 |
+
assert simplify(l3.equation()) in (x - x1, x1 - x)
|
453 |
+
assert simplify(l1.equation()) in (x - y, y - x)
|
454 |
+
assert simplify(l3.equation()) in (x - x1, x1 - x)
|
455 |
+
|
456 |
+
assert Line(p1, Point(1, 0)).equation(x=x, y=y) == y
|
457 |
+
assert Line(p1, Point(0, 1)).equation() == x
|
458 |
+
assert Line(Point(2, 0), Point(2, 1)).equation() == x - 2
|
459 |
+
assert Line(p2, Point(2, 1)).equation() == y - 1
|
460 |
+
|
461 |
+
assert Line3D(Point(x1, x1, x1), Point(y1, y1, y1)
|
462 |
+
).equation() == (-x + y, -x + z)
|
463 |
+
assert Line3D(Point(1, 2, 3), Point(2, 3, 4)
|
464 |
+
).equation() == (-x + y - 1, -x + z - 2)
|
465 |
+
assert Line3D(Point(1, 2, 3), Point(1, 3, 4)
|
466 |
+
).equation() == (x - 1, -y + z - 1)
|
467 |
+
assert Line3D(Point(1, 2, 3), Point(2, 2, 4)
|
468 |
+
).equation() == (y - 2, -x + z - 2)
|
469 |
+
assert Line3D(Point(1, 2, 3), Point(2, 3, 3)
|
470 |
+
).equation() == (-x + y - 1, z - 3)
|
471 |
+
assert Line3D(Point(1, 2, 3), Point(1, 2, 4)
|
472 |
+
).equation() == (x - 1, y - 2)
|
473 |
+
assert Line3D(Point(1, 2, 3), Point(1, 3, 3)
|
474 |
+
).equation() == (x - 1, z - 3)
|
475 |
+
assert Line3D(Point(1, 2, 3), Point(2, 2, 3)
|
476 |
+
).equation() == (y - 2, z - 3)
|
477 |
+
|
478 |
+
|
479 |
+
def test_intersection_2d():
|
480 |
+
p1 = Point(0, 0)
|
481 |
+
p2 = Point(1, 1)
|
482 |
+
p3 = Point(x1, x1)
|
483 |
+
p4 = Point(y1, y1)
|
484 |
+
|
485 |
+
l1 = Line(p1, p2)
|
486 |
+
l3 = Line(Point(0, 0), Point(3, 4))
|
487 |
+
|
488 |
+
r1 = Ray(Point(1, 1), Point(2, 2))
|
489 |
+
r2 = Ray(Point(0, 0), Point(3, 4))
|
490 |
+
r4 = Ray(p1, p2)
|
491 |
+
r6 = Ray(Point(0, 1), Point(1, 2))
|
492 |
+
r7 = Ray(Point(0.5, 0.5), Point(1, 1))
|
493 |
+
|
494 |
+
s1 = Segment(p1, p2)
|
495 |
+
s2 = Segment(Point(0.25, 0.25), Point(0.5, 0.5))
|
496 |
+
s3 = Segment(Point(0, 0), Point(3, 4))
|
497 |
+
|
498 |
+
assert intersection(l1, p1) == [p1]
|
499 |
+
assert intersection(l1, Point(x1, 1 + x1)) == []
|
500 |
+
assert intersection(l1, Line(p3, p4)) in [[l1], [Line(p3, p4)]]
|
501 |
+
assert intersection(l1, l1.parallel_line(Point(x1, 1 + x1))) == []
|
502 |
+
assert intersection(l3, l3) == [l3]
|
503 |
+
assert intersection(l3, r2) == [r2]
|
504 |
+
assert intersection(l3, s3) == [s3]
|
505 |
+
assert intersection(s3, l3) == [s3]
|
506 |
+
assert intersection(Segment(Point(-10, 10), Point(10, 10)), Segment(Point(-5, -5), Point(-5, 5))) == []
|
507 |
+
assert intersection(r2, l3) == [r2]
|
508 |
+
assert intersection(r1, Ray(Point(2, 2), Point(0, 0))) == [Segment(Point(1, 1), Point(2, 2))]
|
509 |
+
assert intersection(r1, Ray(Point(1, 1), Point(-1, -1))) == [Point(1, 1)]
|
510 |
+
assert intersection(r1, Segment(Point(0, 0), Point(2, 2))) == [Segment(Point(1, 1), Point(2, 2))]
|
511 |
+
|
512 |
+
assert r4.intersection(s2) == [s2]
|
513 |
+
assert r4.intersection(Segment(Point(2, 3), Point(3, 4))) == []
|
514 |
+
assert r4.intersection(Segment(Point(-1, -1), Point(0.5, 0.5))) == [Segment(p1, Point(0.5, 0.5))]
|
515 |
+
assert r4.intersection(Ray(p2, p1)) == [s1]
|
516 |
+
assert Ray(p2, p1).intersection(r6) == []
|
517 |
+
assert r4.intersection(r7) == r7.intersection(r4) == [r7]
|
518 |
+
assert Ray3D((0, 0), (3, 0)).intersection(Ray3D((1, 0), (3, 0))) == [Ray3D((1, 0), (3, 0))]
|
519 |
+
assert Ray3D((1, 0), (3, 0)).intersection(Ray3D((0, 0), (3, 0))) == [Ray3D((1, 0), (3, 0))]
|
520 |
+
assert Ray(Point(0, 0), Point(0, 4)).intersection(Ray(Point(0, 1), Point(0, -1))) == \
|
521 |
+
[Segment(Point(0, 0), Point(0, 1))]
|
522 |
+
|
523 |
+
assert Segment3D((0, 0), (3, 0)).intersection(
|
524 |
+
Segment3D((1, 0), (2, 0))) == [Segment3D((1, 0), (2, 0))]
|
525 |
+
assert Segment3D((1, 0), (2, 0)).intersection(
|
526 |
+
Segment3D((0, 0), (3, 0))) == [Segment3D((1, 0), (2, 0))]
|
527 |
+
assert Segment3D((0, 0), (3, 0)).intersection(
|
528 |
+
Segment3D((3, 0), (4, 0))) == [Point3D((3, 0))]
|
529 |
+
assert Segment3D((0, 0), (3, 0)).intersection(
|
530 |
+
Segment3D((2, 0), (5, 0))) == [Segment3D((2, 0), (3, 0))]
|
531 |
+
assert Segment3D((0, 0), (3, 0)).intersection(
|
532 |
+
Segment3D((-2, 0), (1, 0))) == [Segment3D((0, 0), (1, 0))]
|
533 |
+
assert Segment3D((0, 0), (3, 0)).intersection(
|
534 |
+
Segment3D((-2, 0), (0, 0))) == [Point3D(0, 0)]
|
535 |
+
assert s1.intersection(Segment(Point(1, 1), Point(2, 2))) == [Point(1, 1)]
|
536 |
+
assert s1.intersection(Segment(Point(0.5, 0.5), Point(1.5, 1.5))) == [Segment(Point(0.5, 0.5), p2)]
|
537 |
+
assert s1.intersection(Segment(Point(4, 4), Point(5, 5))) == []
|
538 |
+
assert s1.intersection(Segment(Point(-1, -1), p1)) == [p1]
|
539 |
+
assert s1.intersection(Segment(Point(-1, -1), Point(0.5, 0.5))) == [Segment(p1, Point(0.5, 0.5))]
|
540 |
+
assert s1.intersection(Line(Point(1, 0), Point(2, 1))) == []
|
541 |
+
assert s1.intersection(s2) == [s2]
|
542 |
+
assert s2.intersection(s1) == [s2]
|
543 |
+
|
544 |
+
assert asa(120, 8, 52) == \
|
545 |
+
Triangle(
|
546 |
+
Point(0, 0),
|
547 |
+
Point(8, 0),
|
548 |
+
Point(-4 * cos(19 * pi / 90) / sin(2 * pi / 45),
|
549 |
+
4 * sqrt(3) * cos(19 * pi / 90) / sin(2 * pi / 45)))
|
550 |
+
assert Line((0, 0), (1, 1)).intersection(Ray((1, 0), (1, 2))) == [Point(1, 1)]
|
551 |
+
assert Line((0, 0), (1, 1)).intersection(Segment((1, 0), (1, 2))) == [Point(1, 1)]
|
552 |
+
assert Ray((0, 0), (1, 1)).intersection(Ray((1, 0), (1, 2))) == [Point(1, 1)]
|
553 |
+
assert Ray((0, 0), (1, 1)).intersection(Segment((1, 0), (1, 2))) == [Point(1, 1)]
|
554 |
+
assert Ray((0, 0), (10, 10)).contains(Segment((1, 1), (2, 2))) is True
|
555 |
+
assert Segment((1, 1), (2, 2)) in Line((0, 0), (10, 10))
|
556 |
+
assert s1.intersection(Ray((1, 1), (4, 4))) == [Point(1, 1)]
|
557 |
+
|
558 |
+
# This test is disabled because it hangs after rref changes which simplify
|
559 |
+
# intermediate results and return a different representation from when the
|
560 |
+
# test was written.
|
561 |
+
# # 16628 - this should be fast
|
562 |
+
# p0 = Point2D(Rational(249, 5), Rational(497999, 10000))
|
563 |
+
# p1 = Point2D((-58977084786*sqrt(405639795226) + 2030690077184193 +
|
564 |
+
# 20112207807*sqrt(630547164901) + 99600*sqrt(255775022850776494562626))
|
565 |
+
# /(2000*sqrt(255775022850776494562626) + 1991998000*sqrt(405639795226)
|
566 |
+
# + 1991998000*sqrt(630547164901) + 1622561172902000),
|
567 |
+
# (-498000*sqrt(255775022850776494562626) - 995999*sqrt(630547164901) +
|
568 |
+
# 90004251917891999 +
|
569 |
+
# 496005510002*sqrt(405639795226))/(10000*sqrt(255775022850776494562626)
|
570 |
+
# + 9959990000*sqrt(405639795226) + 9959990000*sqrt(630547164901) +
|
571 |
+
# 8112805864510000))
|
572 |
+
# p2 = Point2D(Rational(497, 10), Rational(-497, 10))
|
573 |
+
# p3 = Point2D(Rational(-497, 10), Rational(-497, 10))
|
574 |
+
# l = Line(p0, p1)
|
575 |
+
# s = Segment(p2, p3)
|
576 |
+
# n = (-52673223862*sqrt(405639795226) - 15764156209307469 -
|
577 |
+
# 9803028531*sqrt(630547164901) +
|
578 |
+
# 33200*sqrt(255775022850776494562626))
|
579 |
+
# d = sqrt(405639795226) + 315274080450 + 498000*sqrt(
|
580 |
+
# 630547164901) + sqrt(255775022850776494562626)
|
581 |
+
# assert intersection(l, s) == [
|
582 |
+
# Point2D(n/d*Rational(3, 2000), Rational(-497, 10))]
|
583 |
+
|
584 |
+
|
585 |
+
def test_line_intersection():
|
586 |
+
# see also test_issue_11238 in test_matrices.py
|
587 |
+
x0 = tan(pi*Rational(13, 45))
|
588 |
+
x1 = sqrt(3)
|
589 |
+
x2 = x0**2
|
590 |
+
x, y = [8*x0/(x0 + x1), (24*x0 - 8*x1*x2)/(x2 - 3)]
|
591 |
+
assert Line(Point(0, 0), Point(1, -sqrt(3))).contains(Point(x, y)) is True
|
592 |
+
|
593 |
+
|
594 |
+
def test_intersection_3d():
|
595 |
+
p1 = Point3D(0, 0, 0)
|
596 |
+
p2 = Point3D(1, 1, 1)
|
597 |
+
|
598 |
+
l1 = Line3D(p1, p2)
|
599 |
+
l2 = Line3D(Point3D(0, 0, 0), Point3D(3, 4, 0))
|
600 |
+
|
601 |
+
r1 = Ray3D(Point3D(1, 1, 1), Point3D(2, 2, 2))
|
602 |
+
r2 = Ray3D(Point3D(0, 0, 0), Point3D(3, 4, 0))
|
603 |
+
|
604 |
+
s1 = Segment3D(Point3D(0, 0, 0), Point3D(3, 4, 0))
|
605 |
+
|
606 |
+
assert intersection(l1, p1) == [p1]
|
607 |
+
assert intersection(l1, Point3D(x1, 1 + x1, 1)) == []
|
608 |
+
assert intersection(l1, l1.parallel_line(p1)) == [Line3D(Point3D(0, 0, 0), Point3D(1, 1, 1))]
|
609 |
+
assert intersection(l2, r2) == [r2]
|
610 |
+
assert intersection(l2, s1) == [s1]
|
611 |
+
assert intersection(r2, l2) == [r2]
|
612 |
+
assert intersection(r1, Ray3D(Point3D(1, 1, 1), Point3D(-1, -1, -1))) == [Point3D(1, 1, 1)]
|
613 |
+
assert intersection(r1, Segment3D(Point3D(0, 0, 0), Point3D(2, 2, 2))) == [
|
614 |
+
Segment3D(Point3D(1, 1, 1), Point3D(2, 2, 2))]
|
615 |
+
assert intersection(Ray3D(Point3D(1, 0, 0), Point3D(-1, 0, 0)), Ray3D(Point3D(0, 1, 0), Point3D(0, -1, 0))) \
|
616 |
+
== [Point3D(0, 0, 0)]
|
617 |
+
assert intersection(r1, Ray3D(Point3D(2, 2, 2), Point3D(0, 0, 0))) == \
|
618 |
+
[Segment3D(Point3D(1, 1, 1), Point3D(2, 2, 2))]
|
619 |
+
assert intersection(s1, r2) == [s1]
|
620 |
+
|
621 |
+
assert Line3D(Point3D(4, 0, 1), Point3D(0, 4, 1)).intersection(Line3D(Point3D(0, 0, 1), Point3D(4, 4, 1))) == \
|
622 |
+
[Point3D(2, 2, 1)]
|
623 |
+
assert Line3D((0, 1, 2), (0, 2, 3)).intersection(Line3D((0, 1, 2), (0, 1, 1))) == [Point3D(0, 1, 2)]
|
624 |
+
assert Line3D((0, 0), (t, t)).intersection(Line3D((0, 1), (t, t))) == \
|
625 |
+
[Point3D(t, t)]
|
626 |
+
|
627 |
+
assert Ray3D(Point3D(0, 0, 0), Point3D(0, 4, 0)).intersection(Ray3D(Point3D(0, 1, 1), Point3D(0, -1, 1))) == []
|
628 |
+
|
629 |
+
|
630 |
+
def test_is_parallel():
|
631 |
+
p1 = Point3D(0, 0, 0)
|
632 |
+
p2 = Point3D(1, 1, 1)
|
633 |
+
p3 = Point3D(x1, x1, x1)
|
634 |
+
|
635 |
+
l2 = Line(Point(x1, x1), Point(y1, y1))
|
636 |
+
l2_1 = Line(Point(x1, x1), Point(x1, 1 + x1))
|
637 |
+
|
638 |
+
assert Line.is_parallel(Line(Point(0, 0), Point(1, 1)), l2)
|
639 |
+
assert Line.is_parallel(l2, Line(Point(x1, x1), Point(x1, 1 + x1))) is False
|
640 |
+
assert Line.is_parallel(l2, l2.parallel_line(Point(-x1, x1)))
|
641 |
+
assert Line.is_parallel(l2_1, l2_1.parallel_line(Point(0, 0)))
|
642 |
+
assert Line3D(p1, p2).is_parallel(Line3D(p1, p2)) # same as in 2D
|
643 |
+
assert Line3D(Point3D(4, 0, 1), Point3D(0, 4, 1)).is_parallel(Line3D(Point3D(0, 0, 1), Point3D(4, 4, 1))) is False
|
644 |
+
assert Line3D(p1, p2).parallel_line(p3) == Line3D(Point3D(x1, x1, x1),
|
645 |
+
Point3D(x1 + 1, x1 + 1, x1 + 1))
|
646 |
+
assert Line3D(p1, p2).parallel_line(p3.args) == \
|
647 |
+
Line3D(Point3D(x1, x1, x1), Point3D(x1 + 1, x1 + 1, x1 + 1))
|
648 |
+
assert Line3D(Point3D(4, 0, 1), Point3D(0, 4, 1)).is_parallel(Line3D(Point3D(0, 0, 1), Point3D(4, 4, 1))) is False
|
649 |
+
|
650 |
+
|
651 |
+
def test_is_perpendicular():
|
652 |
+
p1 = Point(0, 0)
|
653 |
+
p2 = Point(1, 1)
|
654 |
+
|
655 |
+
l1 = Line(p1, p2)
|
656 |
+
l2 = Line(Point(x1, x1), Point(y1, y1))
|
657 |
+
l1_1 = Line(p1, Point(-x1, x1))
|
658 |
+
# 2D
|
659 |
+
assert Line.is_perpendicular(l1, l1_1)
|
660 |
+
assert Line.is_perpendicular(l1, l2) is False
|
661 |
+
p = l1.random_point()
|
662 |
+
assert l1.perpendicular_segment(p) == p
|
663 |
+
# 3D
|
664 |
+
assert Line3D.is_perpendicular(Line3D(Point3D(0, 0, 0), Point3D(1, 0, 0)),
|
665 |
+
Line3D(Point3D(0, 0, 0), Point3D(0, 1, 0))) is True
|
666 |
+
assert Line3D.is_perpendicular(Line3D(Point3D(0, 0, 0), Point3D(1, 0, 0)),
|
667 |
+
Line3D(Point3D(0, 1, 0), Point3D(1, 1, 0))) is False
|
668 |
+
assert Line3D.is_perpendicular(Line3D(Point3D(0, 0, 0), Point3D(1, 1, 1)),
|
669 |
+
Line3D(Point3D(x1, x1, x1), Point3D(y1, y1, y1))) is False
|
670 |
+
|
671 |
+
|
672 |
+
def test_is_similar():
|
673 |
+
p1 = Point(2000, 2000)
|
674 |
+
p2 = p1.scale(2, 2)
|
675 |
+
|
676 |
+
r1 = Ray3D(Point3D(1, 1, 1), Point3D(1, 0, 0))
|
677 |
+
r2 = Ray(Point(0, 0), Point(0, 1))
|
678 |
+
|
679 |
+
s1 = Segment(Point(0, 0), p1)
|
680 |
+
|
681 |
+
assert s1.is_similar(Segment(p1, p2))
|
682 |
+
assert s1.is_similar(r2) is False
|
683 |
+
assert r1.is_similar(Line3D(Point3D(1, 1, 1), Point3D(1, 0, 0))) is True
|
684 |
+
assert r1.is_similar(Line3D(Point3D(0, 0, 0), Point3D(0, 1, 0))) is False
|
685 |
+
|
686 |
+
|
687 |
+
def test_length():
|
688 |
+
s2 = Segment3D(Point3D(x1, x1, x1), Point3D(y1, y1, y1))
|
689 |
+
assert Line(Point(0, 0), Point(1, 1)).length is oo
|
690 |
+
assert s2.length == sqrt(3) * sqrt((x1 - y1) ** 2)
|
691 |
+
assert Line3D(Point3D(0, 0, 0), Point3D(1, 1, 1)).length is oo
|
692 |
+
|
693 |
+
|
694 |
+
def test_projection():
|
695 |
+
p1 = Point(0, 0)
|
696 |
+
p2 = Point3D(0, 0, 0)
|
697 |
+
p3 = Point(-x1, x1)
|
698 |
+
|
699 |
+
l1 = Line(p1, Point(1, 1))
|
700 |
+
l2 = Line3D(Point3D(0, 0, 0), Point3D(1, 0, 0))
|
701 |
+
l3 = Line3D(p2, Point3D(1, 1, 1))
|
702 |
+
|
703 |
+
r1 = Ray(Point(1, 1), Point(2, 2))
|
704 |
+
|
705 |
+
s1 = Segment(Point2D(0, 0), Point2D(0, 1))
|
706 |
+
s2 = Segment(Point2D(1, 0), Point2D(2, 1/2))
|
707 |
+
|
708 |
+
assert Line(Point(x1, x1), Point(y1, y1)).projection(Point(y1, y1)) == Point(y1, y1)
|
709 |
+
assert Line(Point(x1, x1), Point(x1, 1 + x1)).projection(Point(1, 1)) == Point(x1, 1)
|
710 |
+
assert Segment(Point(-2, 2), Point(0, 4)).projection(r1) == Segment(Point(-1, 3), Point(0, 4))
|
711 |
+
assert Segment(Point(0, 4), Point(-2, 2)).projection(r1) == Segment(Point(0, 4), Point(-1, 3))
|
712 |
+
assert s2.projection(s1) == EmptySet
|
713 |
+
assert l1.projection(p3) == p1
|
714 |
+
assert l1.projection(Ray(p1, Point(-1, 5))) == Ray(Point(0, 0), Point(2, 2))
|
715 |
+
assert l1.projection(Ray(p1, Point(-1, 1))) == p1
|
716 |
+
assert r1.projection(Ray(Point(1, 1), Point(-1, -1))) == Point(1, 1)
|
717 |
+
assert r1.projection(Ray(Point(0, 4), Point(-1, -5))) == Segment(Point(1, 1), Point(2, 2))
|
718 |
+
assert r1.projection(Segment(Point(-1, 5), Point(-5, -10))) == Segment(Point(1, 1), Point(2, 2))
|
719 |
+
assert r1.projection(Ray(Point(1, 1), Point(-1, -1))) == Point(1, 1)
|
720 |
+
assert r1.projection(Ray(Point(0, 4), Point(-1, -5))) == Segment(Point(1, 1), Point(2, 2))
|
721 |
+
assert r1.projection(Segment(Point(-1, 5), Point(-5, -10))) == Segment(Point(1, 1), Point(2, 2))
|
722 |
+
|
723 |
+
assert l3.projection(Ray3D(p2, Point3D(-1, 5, 0))) == Ray3D(Point3D(0, 0, 0), Point3D(Rational(4, 3), Rational(4, 3), Rational(4, 3)))
|
724 |
+
assert l3.projection(Ray3D(p2, Point3D(-1, 1, 1))) == Ray3D(Point3D(0, 0, 0), Point3D(Rational(1, 3), Rational(1, 3), Rational(1, 3)))
|
725 |
+
assert l2.projection(Point3D(5, 5, 0)) == Point3D(5, 0)
|
726 |
+
assert l2.projection(Line3D(Point3D(0, 1, 0), Point3D(1, 1, 0))).equals(l2)
|
727 |
+
|
728 |
+
|
729 |
+
def test_perpendicular_line():
|
730 |
+
# 3d - requires a particular orthogonal to be selected
|
731 |
+
p1, p2, p3 = Point(0, 0, 0), Point(2, 3, 4), Point(-2, 2, 0)
|
732 |
+
l = Line(p1, p2)
|
733 |
+
p = l.perpendicular_line(p3)
|
734 |
+
assert p.p1 == p3
|
735 |
+
assert p.p2 in l
|
736 |
+
# 2d - does not require special selection
|
737 |
+
p1, p2, p3 = Point(0, 0), Point(2, 3), Point(-2, 2)
|
738 |
+
l = Line(p1, p2)
|
739 |
+
p = l.perpendicular_line(p3)
|
740 |
+
assert p.p1 == p3
|
741 |
+
# p is directed from l to p3
|
742 |
+
assert p.direction.unit == (p3 - l.projection(p3)).unit
|
743 |
+
|
744 |
+
|
745 |
+
def test_perpendicular_bisector():
|
746 |
+
s1 = Segment(Point(0, 0), Point(1, 1))
|
747 |
+
aline = Line(Point(S.Half, S.Half), Point(Rational(3, 2), Rational(-1, 2)))
|
748 |
+
on_line = Segment(Point(S.Half, S.Half), Point(Rational(3, 2), Rational(-1, 2))).midpoint
|
749 |
+
|
750 |
+
assert s1.perpendicular_bisector().equals(aline)
|
751 |
+
assert s1.perpendicular_bisector(on_line).equals(Segment(s1.midpoint, on_line))
|
752 |
+
assert s1.perpendicular_bisector(on_line + (1, 0)).equals(aline)
|
753 |
+
|
754 |
+
|
755 |
+
def test_raises():
|
756 |
+
d, e = symbols('a,b', real=True)
|
757 |
+
s = Segment((d, 0), (e, 0))
|
758 |
+
|
759 |
+
raises(TypeError, lambda: Line((1, 1), 1))
|
760 |
+
raises(ValueError, lambda: Line(Point(0, 0), Point(0, 0)))
|
761 |
+
raises(Undecidable, lambda: Point(2 * d, 0) in s)
|
762 |
+
raises(ValueError, lambda: Ray3D(Point(1.0, 1.0)))
|
763 |
+
raises(ValueError, lambda: Line3D(Point3D(0, 0, 0), Point3D(0, 0, 0)))
|
764 |
+
raises(TypeError, lambda: Line3D((1, 1), 1))
|
765 |
+
raises(ValueError, lambda: Line3D(Point3D(0, 0, 0)))
|
766 |
+
raises(TypeError, lambda: Ray((1, 1), 1))
|
767 |
+
raises(GeometryError, lambda: Line(Point(0, 0), Point(1, 0))
|
768 |
+
.projection(Circle(Point(0, 0), 1)))
|
769 |
+
|
770 |
+
|
771 |
+
def test_ray_generation():
|
772 |
+
assert Ray((1, 1), angle=pi / 4) == Ray((1, 1), (2, 2))
|
773 |
+
assert Ray((1, 1), angle=pi / 2) == Ray((1, 1), (1, 2))
|
774 |
+
assert Ray((1, 1), angle=-pi / 2) == Ray((1, 1), (1, 0))
|
775 |
+
assert Ray((1, 1), angle=-3 * pi / 2) == Ray((1, 1), (1, 2))
|
776 |
+
assert Ray((1, 1), angle=5 * pi / 2) == Ray((1, 1), (1, 2))
|
777 |
+
assert Ray((1, 1), angle=5.0 * pi / 2) == Ray((1, 1), (1, 2))
|
778 |
+
assert Ray((1, 1), angle=pi) == Ray((1, 1), (0, 1))
|
779 |
+
assert Ray((1, 1), angle=3.0 * pi) == Ray((1, 1), (0, 1))
|
780 |
+
assert Ray((1, 1), angle=4.0 * pi) == Ray((1, 1), (2, 1))
|
781 |
+
assert Ray((1, 1), angle=0) == Ray((1, 1), (2, 1))
|
782 |
+
assert Ray((1, 1), angle=4.05 * pi) == Ray(Point(1, 1),
|
783 |
+
Point(2, -sqrt(5) * sqrt(2 * sqrt(5) + 10) / 4 - sqrt(
|
784 |
+
2 * sqrt(5) + 10) / 4 + 2 + sqrt(5)))
|
785 |
+
assert Ray((1, 1), angle=4.02 * pi) == Ray(Point(1, 1),
|
786 |
+
Point(2, 1 + tan(4.02 * pi)))
|
787 |
+
assert Ray((1, 1), angle=5) == Ray((1, 1), (2, 1 + tan(5)))
|
788 |
+
|
789 |
+
assert Ray3D((1, 1, 1), direction_ratio=[4, 4, 4]) == Ray3D(Point3D(1, 1, 1), Point3D(5, 5, 5))
|
790 |
+
assert Ray3D((1, 1, 1), direction_ratio=[1, 2, 3]) == Ray3D(Point3D(1, 1, 1), Point3D(2, 3, 4))
|
791 |
+
assert Ray3D((1, 1, 1), direction_ratio=[1, 1, 1]) == Ray3D(Point3D(1, 1, 1), Point3D(2, 2, 2))
|
792 |
+
|
793 |
+
|
794 |
+
def test_issue_7814():
|
795 |
+
circle = Circle(Point(x, 0), y)
|
796 |
+
line = Line(Point(k, z), slope=0)
|
797 |
+
_s = sqrt((y - z)*(y + z))
|
798 |
+
assert line.intersection(circle) == [Point2D(x + _s, z), Point2D(x - _s, z)]
|
799 |
+
|
800 |
+
|
801 |
+
def test_issue_2941():
|
802 |
+
def _check():
|
803 |
+
for f, g in cartes(*[(Line, Ray, Segment)] * 2):
|
804 |
+
l1 = f(a, b)
|
805 |
+
l2 = g(c, d)
|
806 |
+
assert l1.intersection(l2) == l2.intersection(l1)
|
807 |
+
# intersect at end point
|
808 |
+
c, d = (-2, -2), (-2, 0)
|
809 |
+
a, b = (0, 0), (1, 1)
|
810 |
+
_check()
|
811 |
+
# midline intersection
|
812 |
+
c, d = (-2, -3), (-2, 0)
|
813 |
+
_check()
|
814 |
+
|
815 |
+
|
816 |
+
def test_parameter_value():
|
817 |
+
t = Symbol('t')
|
818 |
+
p1, p2 = Point(0, 1), Point(5, 6)
|
819 |
+
l = Line(p1, p2)
|
820 |
+
assert l.parameter_value((5, 6), t) == {t: 1}
|
821 |
+
raises(ValueError, lambda: l.parameter_value((0, 0), t))
|
822 |
+
|
823 |
+
|
824 |
+
def test_bisectors():
|
825 |
+
r1 = Line3D(Point3D(0, 0, 0), Point3D(1, 0, 0))
|
826 |
+
r2 = Line3D(Point3D(0, 0, 0), Point3D(0, 1, 0))
|
827 |
+
bisections = r1.bisectors(r2)
|
828 |
+
assert bisections == [Line3D(Point3D(0, 0, 0), Point3D(1, 1, 0)),
|
829 |
+
Line3D(Point3D(0, 0, 0), Point3D(1, -1, 0))]
|
830 |
+
ans = [Line3D(Point3D(0, 0, 0), Point3D(1, 0, 1)),
|
831 |
+
Line3D(Point3D(0, 0, 0), Point3D(-1, 0, 1))]
|
832 |
+
l1 = (0, 0, 0), (0, 0, 1)
|
833 |
+
l2 = (0, 0), (1, 0)
|
834 |
+
for a, b in cartes((Line, Segment, Ray), repeat=2):
|
835 |
+
assert a(*l1).bisectors(b(*l2)) == ans
|
836 |
+
|
837 |
+
|
838 |
+
def test_issue_8615():
|
839 |
+
a = Line3D(Point3D(6, 5, 0), Point3D(6, -6, 0))
|
840 |
+
b = Line3D(Point3D(6, -1, 19/10), Point3D(6, -1, 0))
|
841 |
+
assert a.intersection(b) == [Point3D(6, -1, 0)]
|
842 |
+
|
843 |
+
|
844 |
+
def test_issue_12598():
|
845 |
+
r1 = Ray(Point(0, 1), Point(0.98, 0.79).n(2))
|
846 |
+
r2 = Ray(Point(0, 0), Point(0.71, 0.71).n(2))
|
847 |
+
assert str(r1.intersection(r2)[0]) == 'Point2D(0.82, 0.82)'
|
848 |
+
l1 = Line((0, 0), (1, 1))
|
849 |
+
l2 = Segment((-1, 1), (0, -1)).n(2)
|
850 |
+
assert str(l1.intersection(l2)[0]) == 'Point2D(-0.33, -0.33)'
|
851 |
+
l2 = Segment((-1, 1), (-1/2, 1/2)).n(2)
|
852 |
+
assert not l1.intersection(l2)
|
env-llmeval/lib/python3.10/site-packages/sympy/geometry/tests/test_parabola.py
ADDED
@@ -0,0 +1,143 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from sympy.core.numbers import (Rational, oo)
|
2 |
+
from sympy.core.singleton import S
|
3 |
+
from sympy.core.symbol import symbols
|
4 |
+
from sympy.functions.elementary.complexes import sign
|
5 |
+
from sympy.functions.elementary.miscellaneous import sqrt
|
6 |
+
from sympy.geometry.ellipse import (Circle, Ellipse)
|
7 |
+
from sympy.geometry.line import (Line, Ray2D, Segment2D)
|
8 |
+
from sympy.geometry.parabola import Parabola
|
9 |
+
from sympy.geometry.point import (Point, Point2D)
|
10 |
+
from sympy.testing.pytest import raises
|
11 |
+
|
12 |
+
from sympy.abc import x, y
|
13 |
+
|
14 |
+
def test_parabola_geom():
|
15 |
+
a, b = symbols('a b')
|
16 |
+
p1 = Point(0, 0)
|
17 |
+
p2 = Point(3, 7)
|
18 |
+
p3 = Point(0, 4)
|
19 |
+
p4 = Point(6, 0)
|
20 |
+
p5 = Point(a, a)
|
21 |
+
d1 = Line(Point(4, 0), Point(4, 9))
|
22 |
+
d2 = Line(Point(7, 6), Point(3, 6))
|
23 |
+
d3 = Line(Point(4, 0), slope=oo)
|
24 |
+
d4 = Line(Point(7, 6), slope=0)
|
25 |
+
d5 = Line(Point(b, a), slope=oo)
|
26 |
+
d6 = Line(Point(a, b), slope=0)
|
27 |
+
|
28 |
+
half = S.Half
|
29 |
+
|
30 |
+
pa1 = Parabola(None, d2)
|
31 |
+
pa2 = Parabola(directrix=d1)
|
32 |
+
pa3 = Parabola(p1, d1)
|
33 |
+
pa4 = Parabola(p2, d2)
|
34 |
+
pa5 = Parabola(p2, d4)
|
35 |
+
pa6 = Parabola(p3, d2)
|
36 |
+
pa7 = Parabola(p2, d1)
|
37 |
+
pa8 = Parabola(p4, d1)
|
38 |
+
pa9 = Parabola(p4, d3)
|
39 |
+
pa10 = Parabola(p5, d5)
|
40 |
+
pa11 = Parabola(p5, d6)
|
41 |
+
d = Line(Point(3, 7), Point(2, 9))
|
42 |
+
pa12 = Parabola(Point(7, 8), d)
|
43 |
+
pa12r = Parabola(Point(7, 8).reflect(d), d)
|
44 |
+
|
45 |
+
raises(ValueError, lambda:
|
46 |
+
Parabola(Point(7, 8, 9), Line(Point(6, 7), Point(7, 7))))
|
47 |
+
raises(ValueError, lambda:
|
48 |
+
Parabola(Point(0, 2), Line(Point(7, 2), Point(6, 2))))
|
49 |
+
raises(ValueError, lambda: Parabola(Point(7, 8), Point(3, 8)))
|
50 |
+
|
51 |
+
# Basic Stuff
|
52 |
+
assert pa1.focus == Point(0, 0)
|
53 |
+
assert pa1.ambient_dimension == S(2)
|
54 |
+
assert pa2 == pa3
|
55 |
+
assert pa4 != pa7
|
56 |
+
assert pa6 != pa7
|
57 |
+
assert pa6.focus == Point2D(0, 4)
|
58 |
+
assert pa6.focal_length == 1
|
59 |
+
assert pa6.p_parameter == -1
|
60 |
+
assert pa6.vertex == Point2D(0, 5)
|
61 |
+
assert pa6.eccentricity == 1
|
62 |
+
assert pa7.focus == Point2D(3, 7)
|
63 |
+
assert pa7.focal_length == half
|
64 |
+
assert pa7.p_parameter == -half
|
65 |
+
assert pa7.vertex == Point2D(7*half, 7)
|
66 |
+
assert pa4.focal_length == half
|
67 |
+
assert pa4.p_parameter == half
|
68 |
+
assert pa4.vertex == Point2D(3, 13*half)
|
69 |
+
assert pa8.focal_length == 1
|
70 |
+
assert pa8.p_parameter == 1
|
71 |
+
assert pa8.vertex == Point2D(5, 0)
|
72 |
+
assert pa4.focal_length == pa5.focal_length
|
73 |
+
assert pa4.p_parameter == pa5.p_parameter
|
74 |
+
assert pa4.vertex == pa5.vertex
|
75 |
+
assert pa4.equation() == pa5.equation()
|
76 |
+
assert pa8.focal_length == pa9.focal_length
|
77 |
+
assert pa8.p_parameter == pa9.p_parameter
|
78 |
+
assert pa8.vertex == pa9.vertex
|
79 |
+
assert pa8.equation() == pa9.equation()
|
80 |
+
assert pa10.focal_length == pa11.focal_length == sqrt((a - b) ** 2) / 2 # if a, b real == abs(a - b)/2
|
81 |
+
assert pa11.vertex == Point(*pa10.vertex[::-1]) == Point(a,
|
82 |
+
a - sqrt((a - b)**2)*sign(a - b)/2) # change axis x->y, y->x on pa10
|
83 |
+
aos = pa12.axis_of_symmetry
|
84 |
+
assert aos == Line(Point(7, 8), Point(5, 7))
|
85 |
+
assert pa12.directrix == Line(Point(3, 7), Point(2, 9))
|
86 |
+
assert pa12.directrix.angle_between(aos) == S.Pi/2
|
87 |
+
assert pa12.eccentricity == 1
|
88 |
+
assert pa12.equation(x, y) == (x - 7)**2 + (y - 8)**2 - (-2*x - y + 13)**2/5
|
89 |
+
assert pa12.focal_length == 9*sqrt(5)/10
|
90 |
+
assert pa12.focus == Point(7, 8)
|
91 |
+
assert pa12.p_parameter == 9*sqrt(5)/10
|
92 |
+
assert pa12.vertex == Point2D(S(26)/5, S(71)/10)
|
93 |
+
assert pa12r.focal_length == 9*sqrt(5)/10
|
94 |
+
assert pa12r.focus == Point(-S(1)/5, S(22)/5)
|
95 |
+
assert pa12r.p_parameter == -9*sqrt(5)/10
|
96 |
+
assert pa12r.vertex == Point(S(8)/5, S(53)/10)
|
97 |
+
|
98 |
+
|
99 |
+
def test_parabola_intersection():
|
100 |
+
l1 = Line(Point(1, -2), Point(-1,-2))
|
101 |
+
l2 = Line(Point(1, 2), Point(-1,2))
|
102 |
+
l3 = Line(Point(1, 0), Point(-1,0))
|
103 |
+
|
104 |
+
p1 = Point(0,0)
|
105 |
+
p2 = Point(0, -2)
|
106 |
+
p3 = Point(120, -12)
|
107 |
+
parabola1 = Parabola(p1, l1)
|
108 |
+
|
109 |
+
# parabola with parabola
|
110 |
+
assert parabola1.intersection(parabola1) == [parabola1]
|
111 |
+
assert parabola1.intersection(Parabola(p1, l2)) == [Point2D(-2, 0), Point2D(2, 0)]
|
112 |
+
assert parabola1.intersection(Parabola(p2, l3)) == [Point2D(0, -1)]
|
113 |
+
assert parabola1.intersection(Parabola(Point(16, 0), l1)) == [Point2D(8, 15)]
|
114 |
+
assert parabola1.intersection(Parabola(Point(0, 16), l1)) == [Point2D(-6, 8), Point2D(6, 8)]
|
115 |
+
assert parabola1.intersection(Parabola(p3, l3)) == []
|
116 |
+
# parabola with point
|
117 |
+
assert parabola1.intersection(p1) == []
|
118 |
+
assert parabola1.intersection(Point2D(0, -1)) == [Point2D(0, -1)]
|
119 |
+
assert parabola1.intersection(Point2D(4, 3)) == [Point2D(4, 3)]
|
120 |
+
# parabola with line
|
121 |
+
assert parabola1.intersection(Line(Point2D(-7, 3), Point(12, 3))) == [Point2D(-4, 3), Point2D(4, 3)]
|
122 |
+
assert parabola1.intersection(Line(Point(-4, -1), Point(4, -1))) == [Point(0, -1)]
|
123 |
+
assert parabola1.intersection(Line(Point(2, 0), Point(0, -2))) == [Point2D(2, 0)]
|
124 |
+
raises(TypeError, lambda: parabola1.intersection(Line(Point(0, 0, 0), Point(1, 1, 1))))
|
125 |
+
# parabola with segment
|
126 |
+
assert parabola1.intersection(Segment2D((-4, -5), (4, 3))) == [Point2D(0, -1), Point2D(4, 3)]
|
127 |
+
assert parabola1.intersection(Segment2D((0, -5), (0, 6))) == [Point2D(0, -1)]
|
128 |
+
assert parabola1.intersection(Segment2D((-12, -65), (14, -68))) == []
|
129 |
+
# parabola with ray
|
130 |
+
assert parabola1.intersection(Ray2D((-4, -5), (4, 3))) == [Point2D(0, -1), Point2D(4, 3)]
|
131 |
+
assert parabola1.intersection(Ray2D((0, 7), (1, 14))) == [Point2D(14 + 2*sqrt(57), 105 + 14*sqrt(57))]
|
132 |
+
assert parabola1.intersection(Ray2D((0, 7), (0, 14))) == []
|
133 |
+
# parabola with ellipse/circle
|
134 |
+
assert parabola1.intersection(Circle(p1, 2)) == [Point2D(-2, 0), Point2D(2, 0)]
|
135 |
+
assert parabola1.intersection(Circle(p2, 1)) == [Point2D(0, -1)]
|
136 |
+
assert parabola1.intersection(Ellipse(p2, 2, 1)) == [Point2D(0, -1)]
|
137 |
+
assert parabola1.intersection(Ellipse(Point(0, 19), 5, 7)) == []
|
138 |
+
assert parabola1.intersection(Ellipse((0, 3), 12, 4)) == [
|
139 |
+
Point2D(0, -1),
|
140 |
+
Point2D(-4*sqrt(17)/3, Rational(59, 9)),
|
141 |
+
Point2D(4*sqrt(17)/3, Rational(59, 9))]
|
142 |
+
# parabola with unsupported type
|
143 |
+
raises(TypeError, lambda: parabola1.intersection(2))
|
env-llmeval/lib/python3.10/site-packages/sympy/geometry/tests/test_plane.py
ADDED
@@ -0,0 +1,268 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from sympy.core.numbers import (Rational, pi)
|
2 |
+
from sympy.core.singleton import S
|
3 |
+
from sympy.core.symbol import (Dummy, symbols)
|
4 |
+
from sympy.functions.elementary.miscellaneous import sqrt
|
5 |
+
from sympy.functions.elementary.trigonometric import (asin, cos, sin)
|
6 |
+
from sympy.geometry import Line, Point, Ray, Segment, Point3D, Line3D, Ray3D, Segment3D, Plane, Circle
|
7 |
+
from sympy.geometry.util import are_coplanar
|
8 |
+
from sympy.testing.pytest import raises
|
9 |
+
|
10 |
+
|
11 |
+
def test_plane():
|
12 |
+
x, y, z, u, v = symbols('x y z u v', real=True)
|
13 |
+
p1 = Point3D(0, 0, 0)
|
14 |
+
p2 = Point3D(1, 1, 1)
|
15 |
+
p3 = Point3D(1, 2, 3)
|
16 |
+
pl3 = Plane(p1, p2, p3)
|
17 |
+
pl4 = Plane(p1, normal_vector=(1, 1, 1))
|
18 |
+
pl4b = Plane(p1, p2)
|
19 |
+
pl5 = Plane(p3, normal_vector=(1, 2, 3))
|
20 |
+
pl6 = Plane(Point3D(2, 3, 7), normal_vector=(2, 2, 2))
|
21 |
+
pl7 = Plane(Point3D(1, -5, -6), normal_vector=(1, -2, 1))
|
22 |
+
pl8 = Plane(p1, normal_vector=(0, 0, 1))
|
23 |
+
pl9 = Plane(p1, normal_vector=(0, 12, 0))
|
24 |
+
pl10 = Plane(p1, normal_vector=(-2, 0, 0))
|
25 |
+
pl11 = Plane(p2, normal_vector=(0, 0, 1))
|
26 |
+
l1 = Line3D(Point3D(5, 0, 0), Point3D(1, -1, 1))
|
27 |
+
l2 = Line3D(Point3D(0, -2, 0), Point3D(3, 1, 1))
|
28 |
+
l3 = Line3D(Point3D(0, -1, 0), Point3D(5, -1, 9))
|
29 |
+
|
30 |
+
raises(ValueError, lambda: Plane(p1, p1, p1))
|
31 |
+
|
32 |
+
assert Plane(p1, p2, p3) != Plane(p1, p3, p2)
|
33 |
+
assert Plane(p1, p2, p3).is_coplanar(Plane(p1, p3, p2))
|
34 |
+
assert Plane(p1, p2, p3).is_coplanar(p1)
|
35 |
+
assert Plane(p1, p2, p3).is_coplanar(Circle(p1, 1)) is False
|
36 |
+
assert Plane(p1, normal_vector=(0, 0, 1)).is_coplanar(Circle(p1, 1))
|
37 |
+
|
38 |
+
assert pl3 == Plane(Point3D(0, 0, 0), normal_vector=(1, -2, 1))
|
39 |
+
assert pl3 != pl4
|
40 |
+
assert pl4 == pl4b
|
41 |
+
assert pl5 == Plane(Point3D(1, 2, 3), normal_vector=(1, 2, 3))
|
42 |
+
|
43 |
+
assert pl5.equation(x, y, z) == x + 2*y + 3*z - 14
|
44 |
+
assert pl3.equation(x, y, z) == x - 2*y + z
|
45 |
+
|
46 |
+
assert pl3.p1 == p1
|
47 |
+
assert pl4.p1 == p1
|
48 |
+
assert pl5.p1 == p3
|
49 |
+
|
50 |
+
assert pl4.normal_vector == (1, 1, 1)
|
51 |
+
assert pl5.normal_vector == (1, 2, 3)
|
52 |
+
|
53 |
+
assert p1 in pl3
|
54 |
+
assert p1 in pl4
|
55 |
+
assert p3 in pl5
|
56 |
+
|
57 |
+
assert pl3.projection(Point(0, 0)) == p1
|
58 |
+
p = pl3.projection(Point3D(1, 1, 0))
|
59 |
+
assert p == Point3D(Rational(7, 6), Rational(2, 3), Rational(1, 6))
|
60 |
+
assert p in pl3
|
61 |
+
|
62 |
+
l = pl3.projection_line(Line(Point(0, 0), Point(1, 1)))
|
63 |
+
assert l == Line3D(Point3D(0, 0, 0), Point3D(Rational(7, 6), Rational(2, 3), Rational(1, 6)))
|
64 |
+
assert l in pl3
|
65 |
+
# get a segment that does not intersect the plane which is also
|
66 |
+
# parallel to pl3's normal veector
|
67 |
+
t = Dummy()
|
68 |
+
r = pl3.random_point()
|
69 |
+
a = pl3.perpendicular_line(r).arbitrary_point(t)
|
70 |
+
s = Segment3D(a.subs(t, 1), a.subs(t, 2))
|
71 |
+
assert s.p1 not in pl3 and s.p2 not in pl3
|
72 |
+
assert pl3.projection_line(s).equals(r)
|
73 |
+
assert pl3.projection_line(Segment(Point(1, 0), Point(1, 1))) == \
|
74 |
+
Segment3D(Point3D(Rational(5, 6), Rational(1, 3), Rational(-1, 6)), Point3D(Rational(7, 6), Rational(2, 3), Rational(1, 6)))
|
75 |
+
assert pl6.projection_line(Ray(Point(1, 0), Point(1, 1))) == \
|
76 |
+
Ray3D(Point3D(Rational(14, 3), Rational(11, 3), Rational(11, 3)), Point3D(Rational(13, 3), Rational(13, 3), Rational(10, 3)))
|
77 |
+
assert pl3.perpendicular_line(r.args) == pl3.perpendicular_line(r)
|
78 |
+
|
79 |
+
assert pl3.is_parallel(pl6) is False
|
80 |
+
assert pl4.is_parallel(pl6)
|
81 |
+
assert pl3.is_parallel(Line(p1, p2))
|
82 |
+
assert pl6.is_parallel(l1) is False
|
83 |
+
|
84 |
+
assert pl3.is_perpendicular(pl6)
|
85 |
+
assert pl4.is_perpendicular(pl7)
|
86 |
+
assert pl6.is_perpendicular(pl7)
|
87 |
+
assert pl6.is_perpendicular(pl4) is False
|
88 |
+
assert pl6.is_perpendicular(l1) is False
|
89 |
+
assert pl6.is_perpendicular(Line((0, 0, 0), (1, 1, 1)))
|
90 |
+
assert pl6.is_perpendicular((1, 1)) is False
|
91 |
+
|
92 |
+
assert pl6.distance(pl6.arbitrary_point(u, v)) == 0
|
93 |
+
assert pl7.distance(pl7.arbitrary_point(u, v)) == 0
|
94 |
+
assert pl6.distance(pl6.arbitrary_point(t)) == 0
|
95 |
+
assert pl7.distance(pl7.arbitrary_point(t)) == 0
|
96 |
+
assert pl6.p1.distance(pl6.arbitrary_point(t)).simplify() == 1
|
97 |
+
assert pl7.p1.distance(pl7.arbitrary_point(t)).simplify() == 1
|
98 |
+
assert pl3.arbitrary_point(t) == Point3D(-sqrt(30)*sin(t)/30 + \
|
99 |
+
2*sqrt(5)*cos(t)/5, sqrt(30)*sin(t)/15 + sqrt(5)*cos(t)/5, sqrt(30)*sin(t)/6)
|
100 |
+
assert pl3.arbitrary_point(u, v) == Point3D(2*u - v, u + 2*v, 5*v)
|
101 |
+
|
102 |
+
assert pl7.distance(Point3D(1, 3, 5)) == 5*sqrt(6)/6
|
103 |
+
assert pl6.distance(Point3D(0, 0, 0)) == 4*sqrt(3)
|
104 |
+
assert pl6.distance(pl6.p1) == 0
|
105 |
+
assert pl7.distance(pl6) == 0
|
106 |
+
assert pl7.distance(l1) == 0
|
107 |
+
assert pl6.distance(Segment3D(Point3D(2, 3, 1), Point3D(1, 3, 4))) == \
|
108 |
+
pl6.distance(Point3D(1, 3, 4)) == 4*sqrt(3)/3
|
109 |
+
assert pl6.distance(Segment3D(Point3D(1, 3, 4), Point3D(0, 3, 7))) == \
|
110 |
+
pl6.distance(Point3D(0, 3, 7)) == 2*sqrt(3)/3
|
111 |
+
assert pl6.distance(Segment3D(Point3D(0, 3, 7), Point3D(-1, 3, 10))) == 0
|
112 |
+
assert pl6.distance(Segment3D(Point3D(-1, 3, 10), Point3D(-2, 3, 13))) == 0
|
113 |
+
assert pl6.distance(Segment3D(Point3D(-2, 3, 13), Point3D(-3, 3, 16))) == \
|
114 |
+
pl6.distance(Point3D(-2, 3, 13)) == 2*sqrt(3)/3
|
115 |
+
assert pl6.distance(Plane(Point3D(5, 5, 5), normal_vector=(8, 8, 8))) == sqrt(3)
|
116 |
+
assert pl6.distance(Ray3D(Point3D(1, 3, 4), direction_ratio=[1, 0, -3])) == 4*sqrt(3)/3
|
117 |
+
assert pl6.distance(Ray3D(Point3D(2, 3, 1), direction_ratio=[-1, 0, 3])) == 0
|
118 |
+
|
119 |
+
|
120 |
+
assert pl6.angle_between(pl3) == pi/2
|
121 |
+
assert pl6.angle_between(pl6) == 0
|
122 |
+
assert pl6.angle_between(pl4) == 0
|
123 |
+
assert pl7.angle_between(Line3D(Point3D(2, 3, 5), Point3D(2, 4, 6))) == \
|
124 |
+
-asin(sqrt(3)/6)
|
125 |
+
assert pl6.angle_between(Ray3D(Point3D(2, 4, 1), Point3D(6, 5, 3))) == \
|
126 |
+
asin(sqrt(7)/3)
|
127 |
+
assert pl7.angle_between(Segment3D(Point3D(5, 6, 1), Point3D(1, 2, 4))) == \
|
128 |
+
asin(7*sqrt(246)/246)
|
129 |
+
|
130 |
+
assert are_coplanar(l1, l2, l3) is False
|
131 |
+
assert are_coplanar(l1) is False
|
132 |
+
assert are_coplanar(Point3D(2, 7, 2), Point3D(0, 0, 2),
|
133 |
+
Point3D(1, 1, 2), Point3D(1, 2, 2))
|
134 |
+
assert are_coplanar(Plane(p1, p2, p3), Plane(p1, p3, p2))
|
135 |
+
assert Plane.are_concurrent(pl3, pl4, pl5) is False
|
136 |
+
assert Plane.are_concurrent(pl6) is False
|
137 |
+
raises(ValueError, lambda: Plane.are_concurrent(Point3D(0, 0, 0)))
|
138 |
+
raises(ValueError, lambda: Plane((1, 2, 3), normal_vector=(0, 0, 0)))
|
139 |
+
|
140 |
+
assert pl3.parallel_plane(Point3D(1, 2, 5)) == Plane(Point3D(1, 2, 5), \
|
141 |
+
normal_vector=(1, -2, 1))
|
142 |
+
|
143 |
+
# perpendicular_plane
|
144 |
+
p = Plane((0, 0, 0), (1, 0, 0))
|
145 |
+
# default
|
146 |
+
assert p.perpendicular_plane() == Plane(Point3D(0, 0, 0), (0, 1, 0))
|
147 |
+
# 1 pt
|
148 |
+
assert p.perpendicular_plane(Point3D(1, 0, 1)) == \
|
149 |
+
Plane(Point3D(1, 0, 1), (0, 1, 0))
|
150 |
+
# pts as tuples
|
151 |
+
assert p.perpendicular_plane((1, 0, 1), (1, 1, 1)) == \
|
152 |
+
Plane(Point3D(1, 0, 1), (0, 0, -1))
|
153 |
+
# more than two planes
|
154 |
+
raises(ValueError, lambda: p.perpendicular_plane((1, 0, 1), (1, 1, 1), (1, 1, 0)))
|
155 |
+
|
156 |
+
a, b = Point3D(0, 0, 0), Point3D(0, 1, 0)
|
157 |
+
Z = (0, 0, 1)
|
158 |
+
p = Plane(a, normal_vector=Z)
|
159 |
+
# case 4
|
160 |
+
assert p.perpendicular_plane(a, b) == Plane(a, (1, 0, 0))
|
161 |
+
n = Point3D(*Z)
|
162 |
+
# case 1
|
163 |
+
assert p.perpendicular_plane(a, n) == Plane(a, (-1, 0, 0))
|
164 |
+
# case 2
|
165 |
+
assert Plane(a, normal_vector=b.args).perpendicular_plane(a, a + b) == \
|
166 |
+
Plane(Point3D(0, 0, 0), (1, 0, 0))
|
167 |
+
# case 1&3
|
168 |
+
assert Plane(b, normal_vector=Z).perpendicular_plane(b, b + n) == \
|
169 |
+
Plane(Point3D(0, 1, 0), (-1, 0, 0))
|
170 |
+
# case 2&3
|
171 |
+
assert Plane(b, normal_vector=b.args).perpendicular_plane(n, n + b) == \
|
172 |
+
Plane(Point3D(0, 0, 1), (1, 0, 0))
|
173 |
+
|
174 |
+
p = Plane(a, normal_vector=(0, 0, 1))
|
175 |
+
assert p.perpendicular_plane() == Plane(a, normal_vector=(1, 0, 0))
|
176 |
+
|
177 |
+
assert pl6.intersection(pl6) == [pl6]
|
178 |
+
assert pl4.intersection(pl4.p1) == [pl4.p1]
|
179 |
+
assert pl3.intersection(pl6) == [
|
180 |
+
Line3D(Point3D(8, 4, 0), Point3D(2, 4, 6))]
|
181 |
+
assert pl3.intersection(Line3D(Point3D(1,2,4), Point3D(4,4,2))) == [
|
182 |
+
Point3D(2, Rational(8, 3), Rational(10, 3))]
|
183 |
+
assert pl3.intersection(Plane(Point3D(6, 0, 0), normal_vector=(2, -5, 3))
|
184 |
+
) == [Line3D(Point3D(-24, -12, 0), Point3D(-25, -13, -1))]
|
185 |
+
assert pl6.intersection(Ray3D(Point3D(2, 3, 1), Point3D(1, 3, 4))) == [
|
186 |
+
Point3D(-1, 3, 10)]
|
187 |
+
assert pl6.intersection(Segment3D(Point3D(2, 3, 1), Point3D(1, 3, 4))) == []
|
188 |
+
assert pl7.intersection(Line(Point(2, 3), Point(4, 2))) == [
|
189 |
+
Point3D(Rational(13, 2), Rational(3, 4), 0)]
|
190 |
+
r = Ray(Point(2, 3), Point(4, 2))
|
191 |
+
assert Plane((1,2,0), normal_vector=(0,0,1)).intersection(r) == [
|
192 |
+
Ray3D(Point(2, 3), Point(4, 2))]
|
193 |
+
assert pl9.intersection(pl8) == [Line3D(Point3D(0, 0, 0), Point3D(12, 0, 0))]
|
194 |
+
assert pl10.intersection(pl11) == [Line3D(Point3D(0, 0, 1), Point3D(0, 2, 1))]
|
195 |
+
assert pl4.intersection(pl8) == [Line3D(Point3D(0, 0, 0), Point3D(1, -1, 0))]
|
196 |
+
assert pl11.intersection(pl8) == []
|
197 |
+
assert pl9.intersection(pl11) == [Line3D(Point3D(0, 0, 1), Point3D(12, 0, 1))]
|
198 |
+
assert pl9.intersection(pl4) == [Line3D(Point3D(0, 0, 0), Point3D(12, 0, -12))]
|
199 |
+
assert pl3.random_point() in pl3
|
200 |
+
assert pl3.random_point(seed=1) in pl3
|
201 |
+
|
202 |
+
# test geometrical entity using equals
|
203 |
+
assert pl4.intersection(pl4.p1)[0].equals(pl4.p1)
|
204 |
+
assert pl3.intersection(pl6)[0].equals(Line3D(Point3D(8, 4, 0), Point3D(2, 4, 6)))
|
205 |
+
pl8 = Plane((1, 2, 0), normal_vector=(0, 0, 1))
|
206 |
+
assert pl8.intersection(Line3D(p1, (1, 12, 0)))[0].equals(Line((0, 0, 0), (0.1, 1.2, 0)))
|
207 |
+
assert pl8.intersection(Ray3D(p1, (1, 12, 0)))[0].equals(Ray((0, 0, 0), (1, 12, 0)))
|
208 |
+
assert pl8.intersection(Segment3D(p1, (21, 1, 0)))[0].equals(Segment3D(p1, (21, 1, 0)))
|
209 |
+
assert pl8.intersection(Plane(p1, normal_vector=(0, 0, 112)))[0].equals(pl8)
|
210 |
+
assert pl8.intersection(Plane(p1, normal_vector=(0, 12, 0)))[0].equals(
|
211 |
+
Line3D(p1, direction_ratio=(112 * pi, 0, 0)))
|
212 |
+
assert pl8.intersection(Plane(p1, normal_vector=(11, 0, 1)))[0].equals(
|
213 |
+
Line3D(p1, direction_ratio=(0, -11, 0)))
|
214 |
+
assert pl8.intersection(Plane(p1, normal_vector=(1, 0, 11)))[0].equals(
|
215 |
+
Line3D(p1, direction_ratio=(0, 11, 0)))
|
216 |
+
assert pl8.intersection(Plane(p1, normal_vector=(-1, -1, -11)))[0].equals(
|
217 |
+
Line3D(p1, direction_ratio=(1, -1, 0)))
|
218 |
+
assert pl3.random_point() in pl3
|
219 |
+
assert len(pl8.intersection(Ray3D(Point3D(0, 2, 3), Point3D(1, 0, 3)))) == 0
|
220 |
+
# check if two plane are equals
|
221 |
+
assert pl6.intersection(pl6)[0].equals(pl6)
|
222 |
+
assert pl8.equals(Plane(p1, normal_vector=(0, 12, 0))) is False
|
223 |
+
assert pl8.equals(pl8)
|
224 |
+
assert pl8.equals(Plane(p1, normal_vector=(0, 0, -12)))
|
225 |
+
assert pl8.equals(Plane(p1, normal_vector=(0, 0, -12*sqrt(3))))
|
226 |
+
assert pl8.equals(p1) is False
|
227 |
+
|
228 |
+
# issue 8570
|
229 |
+
l2 = Line3D(Point3D(Rational(50000004459633, 5000000000000),
|
230 |
+
Rational(-891926590718643, 1000000000000000),
|
231 |
+
Rational(231800966893633, 100000000000000)),
|
232 |
+
Point3D(Rational(50000004459633, 50000000000000),
|
233 |
+
Rational(-222981647679771, 250000000000000),
|
234 |
+
Rational(231800966893633, 100000000000000)))
|
235 |
+
|
236 |
+
p2 = Plane(Point3D(Rational(402775636372767, 100000000000000),
|
237 |
+
Rational(-97224357654973, 100000000000000),
|
238 |
+
Rational(216793600814789, 100000000000000)),
|
239 |
+
(-S('9.00000087501922'), -S('4.81170658872543e-13'),
|
240 |
+
S('0.0')))
|
241 |
+
|
242 |
+
assert str([i.n(2) for i in p2.intersection(l2)]) == \
|
243 |
+
'[Point3D(4.0, -0.89, 2.3)]'
|
244 |
+
|
245 |
+
|
246 |
+
def test_dimension_normalization():
|
247 |
+
A = Plane(Point3D(1, 1, 2), normal_vector=(1, 1, 1))
|
248 |
+
b = Point(1, 1)
|
249 |
+
assert A.projection(b) == Point(Rational(5, 3), Rational(5, 3), Rational(2, 3))
|
250 |
+
|
251 |
+
a, b = Point(0, 0), Point3D(0, 1)
|
252 |
+
Z = (0, 0, 1)
|
253 |
+
p = Plane(a, normal_vector=Z)
|
254 |
+
assert p.perpendicular_plane(a, b) == Plane(Point3D(0, 0, 0), (1, 0, 0))
|
255 |
+
assert Plane((1, 2, 1), (2, 1, 0), (3, 1, 2)
|
256 |
+
).intersection((2, 1)) == [Point(2, 1, 0)]
|
257 |
+
|
258 |
+
|
259 |
+
def test_parameter_value():
|
260 |
+
t, u, v = symbols("t, u v")
|
261 |
+
p1, p2, p3 = Point(0, 0, 0), Point(0, 0, 1), Point(0, 1, 0)
|
262 |
+
p = Plane(p1, p2, p3)
|
263 |
+
assert p.parameter_value((0, -3, 2), t) == {t: asin(2*sqrt(13)/13)}
|
264 |
+
assert p.parameter_value((0, -3, 2), u, v) == {u: 3, v: 2}
|
265 |
+
assert p.parameter_value(p1, t) == p1
|
266 |
+
raises(ValueError, lambda: p.parameter_value((1, 0, 0), t))
|
267 |
+
raises(ValueError, lambda: p.parameter_value(Line(Point(0, 0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
+
from sympy.core.basic import Basic
|
2 |
+
from sympy.core.numbers import (I, Rational, pi)
|
3 |
+
from sympy.core.parameters import evaluate
|
4 |
+
from sympy.core.singleton import S
|
5 |
+
from sympy.core.symbol import Symbol
|
6 |
+
from sympy.core.sympify import sympify
|
7 |
+
from sympy.functions.elementary.miscellaneous import sqrt
|
8 |
+
from sympy.geometry import Line, Point, Point2D, Point3D, Line3D, Plane
|
9 |
+
from sympy.geometry.entity import rotate, scale, translate, GeometryEntity
|
10 |
+
from sympy.matrices import Matrix
|
11 |
+
from sympy.utilities.iterables import subsets, permutations, cartes
|
12 |
+
from sympy.utilities.misc import Undecidable
|
13 |
+
from sympy.testing.pytest import raises, warns
|
14 |
+
|
15 |
+
|
16 |
+
def test_point():
|
17 |
+
x = Symbol('x', real=True)
|
18 |
+
y = Symbol('y', real=True)
|
19 |
+
x1 = Symbol('x1', real=True)
|
20 |
+
x2 = Symbol('x2', real=True)
|
21 |
+
y1 = Symbol('y1', real=True)
|
22 |
+
y2 = Symbol('y2', real=True)
|
23 |
+
half = S.Half
|
24 |
+
p1 = Point(x1, x2)
|
25 |
+
p2 = Point(y1, y2)
|
26 |
+
p3 = Point(0, 0)
|
27 |
+
p4 = Point(1, 1)
|
28 |
+
p5 = Point(0, 1)
|
29 |
+
line = Line(Point(1, 0), slope=1)
|
30 |
+
|
31 |
+
assert p1 in p1
|
32 |
+
assert p1 not in p2
|
33 |
+
assert p2.y == y2
|
34 |
+
assert (p3 + p4) == p4
|
35 |
+
assert (p2 - p1) == Point(y1 - x1, y2 - x2)
|
36 |
+
assert -p2 == Point(-y1, -y2)
|
37 |
+
raises(TypeError, lambda: Point(1))
|
38 |
+
raises(ValueError, lambda: Point([1]))
|
39 |
+
raises(ValueError, lambda: Point(3, I))
|
40 |
+
raises(ValueError, lambda: Point(2*I, I))
|
41 |
+
raises(ValueError, lambda: Point(3 + I, I))
|
42 |
+
|
43 |
+
assert Point(34.05, sqrt(3)) == Point(Rational(681, 20), sqrt(3))
|
44 |
+
assert Point.midpoint(p3, p4) == Point(half, half)
|
45 |
+
assert Point.midpoint(p1, p4) == Point(half + half*x1, half + half*x2)
|
46 |
+
assert Point.midpoint(p2, p2) == p2
|
47 |
+
assert p2.midpoint(p2) == p2
|
48 |
+
assert p1.origin == Point(0, 0)
|
49 |
+
|
50 |
+
assert Point.distance(p3, p4) == sqrt(2)
|
51 |
+
assert Point.distance(p1, p1) == 0
|
52 |
+
assert Point.distance(p3, p2) == sqrt(p2.x**2 + p2.y**2)
|
53 |
+
raises(TypeError, lambda: Point.distance(p1, 0))
|
54 |
+
raises(TypeError, lambda: Point.distance(p1, GeometryEntity()))
|
55 |
+
|
56 |
+
# distance should be symmetric
|
57 |
+
assert p1.distance(line) == line.distance(p1)
|
58 |
+
assert p4.distance(line) == line.distance(p4)
|
59 |
+
|
60 |
+
assert Point.taxicab_distance(p4, p3) == 2
|
61 |
+
|
62 |
+
assert Point.canberra_distance(p4, p5) == 1
|
63 |
+
raises(ValueError, lambda: Point.canberra_distance(p3, p3))
|
64 |
+
|
65 |
+
p1_1 = Point(x1, x1)
|
66 |
+
p1_2 = Point(y2, y2)
|
67 |
+
p1_3 = Point(x1 + 1, x1)
|
68 |
+
assert Point.is_collinear(p3)
|
69 |
+
|
70 |
+
with warns(UserWarning, test_stacklevel=False):
|
71 |
+
assert Point.is_collinear(p3, Point(p3, dim=4))
|
72 |
+
assert p3.is_collinear()
|
73 |
+
assert Point.is_collinear(p3, p4)
|
74 |
+
assert Point.is_collinear(p3, p4, p1_1, p1_2)
|
75 |
+
assert Point.is_collinear(p3, p4, p1_1, p1_3) is False
|
76 |
+
assert Point.is_collinear(p3, p3, p4, p5) is False
|
77 |
+
|
78 |
+
raises(TypeError, lambda: Point.is_collinear(line))
|
79 |
+
raises(TypeError, lambda: p1_1.is_collinear(line))
|
80 |
+
|
81 |
+
assert p3.intersection(Point(0, 0)) == [p3]
|
82 |
+
assert p3.intersection(p4) == []
|
83 |
+
assert p3.intersection(line) == []
|
84 |
+
with warns(UserWarning, test_stacklevel=False):
|
85 |
+
assert Point.intersection(Point(0, 0, 0), Point(0, 0)) == [Point(0, 0, 0)]
|
86 |
+
|
87 |
+
x_pos = Symbol('x', positive=True)
|
88 |
+
p2_1 = Point(x_pos, 0)
|
89 |
+
p2_2 = Point(0, x_pos)
|
90 |
+
p2_3 = Point(-x_pos, 0)
|
91 |
+
p2_4 = Point(0, -x_pos)
|
92 |
+
p2_5 = Point(x_pos, 5)
|
93 |
+
assert Point.is_concyclic(p2_1)
|
94 |
+
assert Point.is_concyclic(p2_1, p2_2)
|
95 |
+
assert Point.is_concyclic(p2_1, p2_2, p2_3, p2_4)
|
96 |
+
for pts in permutations((p2_1, p2_2, p2_3, p2_5)):
|
97 |
+
assert Point.is_concyclic(*pts) is False
|
98 |
+
assert Point.is_concyclic(p4, p4 * 2, p4 * 3) is False
|
99 |
+
assert Point(0, 0).is_concyclic((1, 1), (2, 2), (2, 1)) is False
|
100 |
+
assert Point.is_concyclic(Point(0, 0, 0, 0), Point(1, 0, 0, 0), Point(1, 1, 0, 0), Point(1, 1, 1, 0)) is False
|
101 |
+
|
102 |
+
assert p1.is_scalar_multiple(p1)
|
103 |
+
assert p1.is_scalar_multiple(2*p1)
|
104 |
+
assert not p1.is_scalar_multiple(p2)
|
105 |
+
assert Point.is_scalar_multiple(Point(1, 1), (-1, -1))
|
106 |
+
assert Point.is_scalar_multiple(Point(0, 0), (0, -1))
|
107 |
+
# test when is_scalar_multiple can't be determined
|
108 |
+
raises(Undecidable, lambda: Point.is_scalar_multiple(Point(sympify("x1%y1"), sympify("x2%y2")), Point(0, 1)))
|
109 |
+
|
110 |
+
assert Point(0, 1).orthogonal_direction == Point(1, 0)
|
111 |
+
assert Point(1, 0).orthogonal_direction == Point(0, 1)
|
112 |
+
|
113 |
+
assert p1.is_zero is None
|
114 |
+
assert p3.is_zero
|
115 |
+
assert p4.is_zero is False
|
116 |
+
assert p1.is_nonzero is None
|
117 |
+
assert p3.is_nonzero is False
|
118 |
+
assert p4.is_nonzero
|
119 |
+
|
120 |
+
assert p4.scale(2, 3) == Point(2, 3)
|
121 |
+
assert p3.scale(2, 3) == p3
|
122 |
+
|
123 |
+
assert p4.rotate(pi, Point(0.5, 0.5)) == p3
|
124 |
+
assert p1.__radd__(p2) == p1.midpoint(p2).scale(2, 2)
|
125 |
+
assert (-p3).__rsub__(p4) == p3.midpoint(p4).scale(2, 2)
|
126 |
+
|
127 |
+
assert p4 * 5 == Point(5, 5)
|
128 |
+
assert p4 / 5 == Point(0.2, 0.2)
|
129 |
+
assert 5 * p4 == Point(5, 5)
|
130 |
+
|
131 |
+
raises(ValueError, lambda: Point(0, 0) + 10)
|
132 |
+
|
133 |
+
# Point differences should be simplified
|
134 |
+
assert Point(x*(x - 1), y) - Point(x**2 - x, y + 1) == Point(0, -1)
|
135 |
+
|
136 |
+
a, b = S.Half, Rational(1, 3)
|
137 |
+
assert Point(a, b).evalf(2) == \
|
138 |
+
Point(a.n(2), b.n(2), evaluate=False)
|
139 |
+
raises(ValueError, lambda: Point(1, 2) + 1)
|
140 |
+
|
141 |
+
# test project
|
142 |
+
assert Point.project((0, 1), (1, 0)) == Point(0, 0)
|
143 |
+
assert Point.project((1, 1), (1, 0)) == Point(1, 0)
|
144 |
+
raises(ValueError, lambda: Point.project(p1, Point(0, 0)))
|
145 |
+
|
146 |
+
# test transformations
|
147 |
+
p = Point(1, 0)
|
148 |
+
assert p.rotate(pi/2) == Point(0, 1)
|
149 |
+
assert p.rotate(pi/2, p) == p
|
150 |
+
p = Point(1, 1)
|
151 |
+
assert p.scale(2, 3) == Point(2, 3)
|
152 |
+
assert p.translate(1, 2) == Point(2, 3)
|
153 |
+
assert p.translate(1) == Point(2, 1)
|
154 |
+
assert p.translate(y=1) == Point(1, 2)
|
155 |
+
assert p.translate(*p.args) == Point(2, 2)
|
156 |
+
|
157 |
+
# Check invalid input for transform
|
158 |
+
raises(ValueError, lambda: p3.transform(p3))
|
159 |
+
raises(ValueError, lambda: p.transform(Matrix([[1, 0], [0, 1]])))
|
160 |
+
|
161 |
+
# test __contains__
|
162 |
+
assert 0 in Point(0, 0, 0, 0)
|
163 |
+
assert 1 not in Point(0, 0, 0, 0)
|
164 |
+
|
165 |
+
# test affine_rank
|
166 |
+
assert Point.affine_rank() == -1
|
167 |
+
|
168 |
+
|
169 |
+
def test_point3D():
|
170 |
+
x = Symbol('x', real=True)
|
171 |
+
y = Symbol('y', real=True)
|
172 |
+
x1 = Symbol('x1', real=True)
|
173 |
+
x2 = Symbol('x2', real=True)
|
174 |
+
x3 = Symbol('x3', real=True)
|
175 |
+
y1 = Symbol('y1', real=True)
|
176 |
+
y2 = Symbol('y2', real=True)
|
177 |
+
y3 = Symbol('y3', real=True)
|
178 |
+
half = S.Half
|
179 |
+
p1 = Point3D(x1, x2, x3)
|
180 |
+
p2 = Point3D(y1, y2, y3)
|
181 |
+
p3 = Point3D(0, 0, 0)
|
182 |
+
p4 = Point3D(1, 1, 1)
|
183 |
+
p5 = Point3D(0, 1, 2)
|
184 |
+
|
185 |
+
assert p1 in p1
|
186 |
+
assert p1 not in p2
|
187 |
+
assert p2.y == y2
|
188 |
+
assert (p3 + p4) == p4
|
189 |
+
assert (p2 - p1) == Point3D(y1 - x1, y2 - x2, y3 - x3)
|
190 |
+
assert -p2 == Point3D(-y1, -y2, -y3)
|
191 |
+
|
192 |
+
assert Point(34.05, sqrt(3)) == Point(Rational(681, 20), sqrt(3))
|
193 |
+
assert Point3D.midpoint(p3, p4) == Point3D(half, half, half)
|
194 |
+
assert Point3D.midpoint(p1, p4) == Point3D(half + half*x1, half + half*x2,
|
195 |
+
half + half*x3)
|
196 |
+
assert Point3D.midpoint(p2, p2) == p2
|
197 |
+
assert p2.midpoint(p2) == p2
|
198 |
+
|
199 |
+
assert Point3D.distance(p3, p4) == sqrt(3)
|
200 |
+
assert Point3D.distance(p1, p1) == 0
|
201 |
+
assert Point3D.distance(p3, p2) == sqrt(p2.x**2 + p2.y**2 + p2.z**2)
|
202 |
+
|
203 |
+
p1_1 = Point3D(x1, x1, x1)
|
204 |
+
p1_2 = Point3D(y2, y2, y2)
|
205 |
+
p1_3 = Point3D(x1 + 1, x1, x1)
|
206 |
+
Point3D.are_collinear(p3)
|
207 |
+
assert Point3D.are_collinear(p3, p4)
|
208 |
+
assert Point3D.are_collinear(p3, p4, p1_1, p1_2)
|
209 |
+
assert Point3D.are_collinear(p3, p4, p1_1, p1_3) is False
|
210 |
+
assert Point3D.are_collinear(p3, p3, p4, p5) is False
|
211 |
+
|
212 |
+
assert p3.intersection(Point3D(0, 0, 0)) == [p3]
|
213 |
+
assert p3.intersection(p4) == []
|
214 |
+
|
215 |
+
|
216 |
+
assert p4 * 5 == Point3D(5, 5, 5)
|
217 |
+
assert p4 / 5 == Point3D(0.2, 0.2, 0.2)
|
218 |
+
assert 5 * p4 == Point3D(5, 5, 5)
|
219 |
+
|
220 |
+
raises(ValueError, lambda: Point3D(0, 0, 0) + 10)
|
221 |
+
|
222 |
+
# Test coordinate properties
|
223 |
+
assert p1.coordinates == (x1, x2, x3)
|
224 |
+
assert p2.coordinates == (y1, y2, y3)
|
225 |
+
assert p3.coordinates == (0, 0, 0)
|
226 |
+
assert p4.coordinates == (1, 1, 1)
|
227 |
+
assert p5.coordinates == (0, 1, 2)
|
228 |
+
assert p5.x == 0
|
229 |
+
assert p5.y == 1
|
230 |
+
assert p5.z == 2
|
231 |
+
|
232 |
+
# Point differences should be simplified
|
233 |
+
assert Point3D(x*(x - 1), y, 2) - Point3D(x**2 - x, y + 1, 1) == \
|
234 |
+
Point3D(0, -1, 1)
|
235 |
+
|
236 |
+
a, b, c = S.Half, Rational(1, 3), Rational(1, 4)
|
237 |
+
assert Point3D(a, b, c).evalf(2) == \
|
238 |
+
Point(a.n(2), b.n(2), c.n(2), evaluate=False)
|
239 |
+
raises(ValueError, lambda: Point3D(1, 2, 3) + 1)
|
240 |
+
|
241 |
+
# test transformations
|
242 |
+
p = Point3D(1, 1, 1)
|
243 |
+
assert p.scale(2, 3) == Point3D(2, 3, 1)
|
244 |
+
assert p.translate(1, 2) == Point3D(2, 3, 1)
|
245 |
+
assert p.translate(1) == Point3D(2, 1, 1)
|
246 |
+
assert p.translate(z=1) == Point3D(1, 1, 2)
|
247 |
+
assert p.translate(*p.args) == Point3D(2, 2, 2)
|
248 |
+
|
249 |
+
# Test __new__
|
250 |
+
assert Point3D(0.1, 0.2, evaluate=False, on_morph='ignore').args[0].is_Float
|
251 |
+
|
252 |
+
# Test length property returns correctly
|
253 |
+
assert p.length == 0
|
254 |
+
assert p1_1.length == 0
|
255 |
+
assert p1_2.length == 0
|
256 |
+
|
257 |
+
# Test are_colinear type error
|
258 |
+
raises(TypeError, lambda: Point3D.are_collinear(p, x))
|
259 |
+
|
260 |
+
# Test are_coplanar
|
261 |
+
assert Point.are_coplanar()
|
262 |
+
assert Point.are_coplanar((1, 2, 0), (1, 2, 0), (1, 3, 0))
|
263 |
+
assert Point.are_coplanar((1, 2, 0), (1, 2, 3))
|
264 |
+
with warns(UserWarning, test_stacklevel=False):
|
265 |
+
raises(ValueError, lambda: Point2D.are_coplanar((1, 2), (1, 2, 3)))
|
266 |
+
assert Point3D.are_coplanar((1, 2, 0), (1, 2, 3))
|
267 |
+
assert Point.are_coplanar((0, 0, 0), (1, 1, 0), (1, 1, 1), (1, 2, 1)) is False
|
268 |
+
planar2 = Point3D(1, -1, 1)
|
269 |
+
planar3 = Point3D(-1, 1, 1)
|
270 |
+
assert Point3D.are_coplanar(p, planar2, planar3) == True
|
271 |
+
assert Point3D.are_coplanar(p, planar2, planar3, p3) == False
|
272 |
+
assert Point.are_coplanar(p, planar2)
|
273 |
+
planar2 = Point3D(1, 1, 2)
|
274 |
+
planar3 = Point3D(1, 1, 3)
|
275 |
+
assert Point3D.are_coplanar(p, planar2, planar3) # line, not plane
|
276 |
+
plane = Plane((1, 2, 1), (2, 1, 0), (3, 1, 2))
|
277 |
+
assert Point.are_coplanar(*[plane.projection(((-1)**i, i)) for i in range(4)])
|
278 |
+
|
279 |
+
# all 2D points are coplanar
|
280 |
+
assert Point.are_coplanar(Point(x, y), Point(x, x + y), Point(y, x + 2)) is True
|
281 |
+
|
282 |
+
# Test Intersection
|
283 |
+
assert planar2.intersection(Line3D(p, planar3)) == [Point3D(1, 1, 2)]
|
284 |
+
|
285 |
+
# Test Scale
|
286 |
+
assert planar2.scale(1, 1, 1) == planar2
|
287 |
+
assert planar2.scale(2, 2, 2, planar3) == Point3D(1, 1, 1)
|
288 |
+
assert planar2.scale(1, 1, 1, p3) == planar2
|
289 |
+
|
290 |
+
# Test Transform
|
291 |
+
identity = Matrix([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]])
|
292 |
+
assert p.transform(identity) == p
|
293 |
+
trans = Matrix([[1, 0, 0, 1], [0, 1, 0, 1], [0, 0, 1, 1], [0, 0, 0, 1]])
|
294 |
+
assert p.transform(trans) == Point3D(2, 2, 2)
|
295 |
+
raises(ValueError, lambda: p.transform(p))
|
296 |
+
raises(ValueError, lambda: p.transform(Matrix([[1, 0], [0, 1]])))
|
297 |
+
|
298 |
+
# Test Equals
|
299 |
+
assert p.equals(x1) == False
|
300 |
+
|
301 |
+
# Test __sub__
|
302 |
+
p_4d = Point(0, 0, 0, 1)
|
303 |
+
with warns(UserWarning, test_stacklevel=False):
|
304 |
+
assert p - p_4d == Point(1, 1, 1, -1)
|
305 |
+
p_4d3d = Point(0, 0, 1, 0)
|
306 |
+
with warns(UserWarning, test_stacklevel=False):
|
307 |
+
assert p - p_4d3d == Point(1, 1, 0, 0)
|
308 |
+
|
309 |
+
|
310 |
+
def test_Point2D():
|
311 |
+
|
312 |
+
# Test Distance
|
313 |
+
p1 = Point2D(1, 5)
|
314 |
+
p2 = Point2D(4, 2.5)
|
315 |
+
p3 = (6, 3)
|
316 |
+
assert p1.distance(p2) == sqrt(61)/2
|
317 |
+
assert p2.distance(p3) == sqrt(17)/2
|
318 |
+
|
319 |
+
# Test coordinates
|
320 |
+
assert p1.x == 1
|
321 |
+
assert p1.y == 5
|
322 |
+
assert p2.x == 4
|
323 |
+
assert p2.y == S(5)/2
|
324 |
+
assert p1.coordinates == (1, 5)
|
325 |
+
assert p2.coordinates == (4, S(5)/2)
|
326 |
+
|
327 |
+
# test bounds
|
328 |
+
assert p1.bounds == (1, 5, 1, 5)
|
329 |
+
|
330 |
+
def test_issue_9214():
|
331 |
+
p1 = Point3D(4, -2, 6)
|
332 |
+
p2 = Point3D(1, 2, 3)
|
333 |
+
p3 = Point3D(7, 2, 3)
|
334 |
+
|
335 |
+
assert Point3D.are_collinear(p1, p2, p3) is False
|
336 |
+
|
337 |
+
|
338 |
+
def test_issue_11617():
|
339 |
+
p1 = Point3D(1,0,2)
|
340 |
+
p2 = Point2D(2,0)
|
341 |
+
|
342 |
+
with warns(UserWarning, test_stacklevel=False):
|
343 |
+
assert p1.distance(p2) == sqrt(5)
|
344 |
+
|
345 |
+
|
346 |
+
def test_transform():
|
347 |
+
p = Point(1, 1)
|
348 |
+
assert p.transform(rotate(pi/2)) == Point(-1, 1)
|
349 |
+
assert p.transform(scale(3, 2)) == Point(3, 2)
|
350 |
+
assert p.transform(translate(1, 2)) == Point(2, 3)
|
351 |
+
assert Point(1, 1).scale(2, 3, (4, 5)) == \
|
352 |
+
Point(-2, -7)
|
353 |
+
assert Point(1, 1).translate(4, 5) == \
|
354 |
+
Point(5, 6)
|
355 |
+
|
356 |
+
|
357 |
+
def test_concyclic_doctest_bug():
|
358 |
+
p1, p2 = Point(-1, 0), Point(1, 0)
|
359 |
+
p3, p4 = Point(0, 1), Point(-1, 2)
|
360 |
+
assert Point.is_concyclic(p1, p2, p3)
|
361 |
+
assert not Point.is_concyclic(p1, p2, p3, p4)
|
362 |
+
|
363 |
+
|
364 |
+
def test_arguments():
|
365 |
+
"""Functions accepting `Point` objects in `geometry`
|
366 |
+
should also accept tuples and lists and
|
367 |
+
automatically convert them to points."""
|
368 |
+
|
369 |
+
singles2d = ((1,2), [1,2], Point(1,2))
|
370 |
+
singles2d2 = ((1,3), [1,3], Point(1,3))
|
371 |
+
doubles2d = cartes(singles2d, singles2d2)
|
372 |
+
p2d = Point2D(1,2)
|
373 |
+
singles3d = ((1,2,3), [1,2,3], Point(1,2,3))
|
374 |
+
doubles3d = subsets(singles3d, 2)
|
375 |
+
p3d = Point3D(1,2,3)
|
376 |
+
singles4d = ((1,2,3,4), [1,2,3,4], Point(1,2,3,4))
|
377 |
+
doubles4d = subsets(singles4d, 2)
|
378 |
+
p4d = Point(1,2,3,4)
|
379 |
+
|
380 |
+
# test 2D
|
381 |
+
test_single = ['distance', 'is_scalar_multiple', 'taxicab_distance', 'midpoint', 'intersection', 'dot', 'equals', '__add__', '__sub__']
|
382 |
+
test_double = ['is_concyclic', 'is_collinear']
|
383 |
+
for p in singles2d:
|
384 |
+
Point2D(p)
|
385 |
+
for func in test_single:
|
386 |
+
for p in singles2d:
|
387 |
+
getattr(p2d, func)(p)
|
388 |
+
for func in test_double:
|
389 |
+
for p in doubles2d:
|
390 |
+
getattr(p2d, func)(*p)
|
391 |
+
|
392 |
+
# test 3D
|
393 |
+
test_double = ['is_collinear']
|
394 |
+
for p in singles3d:
|
395 |
+
Point3D(p)
|
396 |
+
for func in test_single:
|
397 |
+
for p in singles3d:
|
398 |
+
getattr(p3d, func)(p)
|
399 |
+
for func in test_double:
|
400 |
+
for p in doubles3d:
|
401 |
+
getattr(p3d, func)(*p)
|
402 |
+
|
403 |
+
# test 4D
|
404 |
+
test_double = ['is_collinear']
|
405 |
+
for p in singles4d:
|
406 |
+
Point(p)
|
407 |
+
for func in test_single:
|
408 |
+
for p in singles4d:
|
409 |
+
getattr(p4d, func)(p)
|
410 |
+
for func in test_double:
|
411 |
+
for p in doubles4d:
|
412 |
+
getattr(p4d, func)(*p)
|
413 |
+
|
414 |
+
# test evaluate=False for ops
|
415 |
+
x = Symbol('x')
|
416 |
+
a = Point(0, 1)
|
417 |
+
assert a + (0.1, x) == Point(0.1, 1 + x, evaluate=False)
|
418 |
+
a = Point(0, 1)
|
419 |
+
assert a/10.0 == Point(0, 0.1, evaluate=False)
|
420 |
+
a = Point(0, 1)
|
421 |
+
assert a*10.0 == Point(0.0, 10.0, evaluate=False)
|
422 |
+
|
423 |
+
# test evaluate=False when changing dimensions
|
424 |
+
u = Point(.1, .2, evaluate=False)
|
425 |
+
u4 = Point(u, dim=4, on_morph='ignore')
|
426 |
+
assert u4.args == (.1, .2, 0, 0)
|
427 |
+
assert all(i.is_Float for i in u4.args[:2])
|
428 |
+
# and even when *not* changing dimensions
|
429 |
+
assert all(i.is_Float for i in Point(u).args)
|
430 |
+
|
431 |
+
# never raise error if creating an origin
|
432 |
+
assert Point(dim=3, on_morph='error')
|
433 |
+
|
434 |
+
# raise error with unmatched dimension
|
435 |
+
raises(ValueError, lambda: Point(1, 1, dim=3, on_morph='error'))
|
436 |
+
# test unknown on_morph
|
437 |
+
raises(ValueError, lambda: Point(1, 1, dim=3, on_morph='unknown'))
|
438 |
+
# test invalid expressions
|
439 |
+
raises(TypeError, lambda: Point(Basic(), Basic()))
|
440 |
+
|
441 |
+
def test_unit():
|
442 |
+
assert Point(1, 1).unit == Point(sqrt(2)/2, sqrt(2)/2)
|
443 |
+
|
444 |
+
|
445 |
+
def test_dot():
|
446 |
+
raises(TypeError, lambda: Point(1, 2).dot(Line((0, 0), (1, 1))))
|
447 |
+
|
448 |
+
|
449 |
+
def test__normalize_dimension():
|
450 |
+
assert Point._normalize_dimension(Point(1, 2), Point(3, 4)) == [
|
451 |
+
Point(1, 2), Point(3, 4)]
|
452 |
+
assert Point._normalize_dimension(
|
453 |
+
Point(1, 2), Point(3, 4, 0), on_morph='ignore') == [
|
454 |
+
Point(1, 2, 0), Point(3, 4, 0)]
|
455 |
+
|
456 |
+
|
457 |
+
def test_issue_22684():
|
458 |
+
# Used to give an error
|
459 |
+
with evaluate(False):
|
460 |
+
Point(1, 2)
|
461 |
+
|
462 |
+
|
463 |
+
def test_direction_cosine():
|
464 |
+
p1 = Point3D(0, 0, 0)
|
465 |
+
p2 = Point3D(1, 1, 1)
|
466 |
+
|
467 |
+
assert p1.direction_cosine(Point3D(1, 0, 0)) == [1, 0, 0]
|
468 |
+
assert p1.direction_cosine(Point3D(0, 1, 0)) == [0, 1, 0]
|
469 |
+
assert p1.direction_cosine(Point3D(0, 0, pi)) == [0, 0, 1]
|
470 |
+
|
471 |
+
assert p1.direction_cosine(Point3D(5, 0, 0)) == [1, 0, 0]
|
472 |
+
assert p1.direction_cosine(Point3D(0, sqrt(3), 0)) == [0, 1, 0]
|
473 |
+
assert p1.direction_cosine(Point3D(0, 0, 5)) == [0, 0, 1]
|
474 |
+
|
475 |
+
assert p1.direction_cosine(Point3D(2.4, 2.4, 0)) == [sqrt(2)/2, sqrt(2)/2, 0]
|
476 |
+
assert p1.direction_cosine(Point3D(1, 1, 1)) == [sqrt(3) / 3, sqrt(3) / 3, sqrt(3) / 3]
|
477 |
+
assert p1.direction_cosine(Point3D(-12, 0 -15)) == [-4*sqrt(41)/41, -5*sqrt(41)/41, 0]
|
478 |
+
|
479 |
+
assert p2.direction_cosine(Point3D(0, 0, 0)) == [-sqrt(3) / 3, -sqrt(3) / 3, -sqrt(3) / 3]
|
480 |
+
assert p2.direction_cosine(Point3D(1, 1, 12)) == [0, 0, 1]
|
481 |
+
assert p2.direction_cosine(Point3D(12, 1, 12)) == [sqrt(2) / 2, 0, sqrt(2) / 2]
|
env-llmeval/lib/python3.10/site-packages/sympy/geometry/tests/test_polygon.py
ADDED
@@ -0,0 +1,664 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from sympy.core.numbers import (Float, Rational, oo, pi)
|
2 |
+
from sympy.core.singleton import S
|
3 |
+
from sympy.core.symbol import (Symbol, symbols)
|
4 |
+
from sympy.functions.elementary.complexes import Abs
|
5 |
+
from sympy.functions.elementary.miscellaneous import sqrt
|
6 |
+
from sympy.functions.elementary.trigonometric import (acos, cos, sin)
|
7 |
+
from sympy.functions.elementary.trigonometric import tan
|
8 |
+
from sympy.geometry import (Circle, Ellipse, GeometryError, Point, Point2D,
|
9 |
+
Polygon, Ray, RegularPolygon, Segment, Triangle,
|
10 |
+
are_similar, convex_hull, intersection, Line, Ray2D)
|
11 |
+
from sympy.testing.pytest import raises, slow, warns
|
12 |
+
from sympy.core.random import verify_numerically
|
13 |
+
from sympy.geometry.polygon import rad, deg
|
14 |
+
from sympy.integrals.integrals import integrate
|
15 |
+
|
16 |
+
|
17 |
+
def feq(a, b):
|
18 |
+
"""Test if two floating point values are 'equal'."""
|
19 |
+
t_float = Float("1.0E-10")
|
20 |
+
return -t_float < a - b < t_float
|
21 |
+
|
22 |
+
@slow
|
23 |
+
def test_polygon():
|
24 |
+
x = Symbol('x', real=True)
|
25 |
+
y = Symbol('y', real=True)
|
26 |
+
q = Symbol('q', real=True)
|
27 |
+
u = Symbol('u', real=True)
|
28 |
+
v = Symbol('v', real=True)
|
29 |
+
w = Symbol('w', real=True)
|
30 |
+
x1 = Symbol('x1', real=True)
|
31 |
+
half = S.Half
|
32 |
+
a, b, c = Point(0, 0), Point(2, 0), Point(3, 3)
|
33 |
+
t = Triangle(a, b, c)
|
34 |
+
assert Polygon(Point(0, 0)) == Point(0, 0)
|
35 |
+
assert Polygon(a, Point(1, 0), b, c) == t
|
36 |
+
assert Polygon(Point(1, 0), b, c, a) == t
|
37 |
+
assert Polygon(b, c, a, Point(1, 0)) == t
|
38 |
+
# 2 "remove folded" tests
|
39 |
+
assert Polygon(a, Point(3, 0), b, c) == t
|
40 |
+
assert Polygon(a, b, Point(3, -1), b, c) == t
|
41 |
+
# remove multiple collinear points
|
42 |
+
assert Polygon(Point(-4, 15), Point(-11, 15), Point(-15, 15),
|
43 |
+
Point(-15, 33/5), Point(-15, -87/10), Point(-15, -15),
|
44 |
+
Point(-42/5, -15), Point(-2, -15), Point(7, -15), Point(15, -15),
|
45 |
+
Point(15, -3), Point(15, 10), Point(15, 15)) == \
|
46 |
+
Polygon(Point(-15, -15), Point(15, -15), Point(15, 15), Point(-15, 15))
|
47 |
+
|
48 |
+
p1 = Polygon(
|
49 |
+
Point(0, 0), Point(3, -1),
|
50 |
+
Point(6, 0), Point(4, 5),
|
51 |
+
Point(2, 3), Point(0, 3))
|
52 |
+
p2 = Polygon(
|
53 |
+
Point(6, 0), Point(3, -1),
|
54 |
+
Point(0, 0), Point(0, 3),
|
55 |
+
Point(2, 3), Point(4, 5))
|
56 |
+
p3 = Polygon(
|
57 |
+
Point(0, 0), Point(3, 0),
|
58 |
+
Point(5, 2), Point(4, 4))
|
59 |
+
p4 = Polygon(
|
60 |
+
Point(0, 0), Point(4, 4),
|
61 |
+
Point(5, 2), Point(3, 0))
|
62 |
+
p5 = Polygon(
|
63 |
+
Point(0, 0), Point(4, 4),
|
64 |
+
Point(0, 4))
|
65 |
+
p6 = Polygon(
|
66 |
+
Point(-11, 1), Point(-9, 6.6),
|
67 |
+
Point(-4, -3), Point(-8.4, -8.7))
|
68 |
+
p7 = Polygon(
|
69 |
+
Point(x, y), Point(q, u),
|
70 |
+
Point(v, w))
|
71 |
+
p8 = Polygon(
|
72 |
+
Point(x, y), Point(v, w),
|
73 |
+
Point(q, u))
|
74 |
+
p9 = Polygon(
|
75 |
+
Point(0, 0), Point(4, 4),
|
76 |
+
Point(3, 0), Point(5, 2))
|
77 |
+
p10 = Polygon(
|
78 |
+
Point(0, 2), Point(2, 2),
|
79 |
+
Point(0, 0), Point(2, 0))
|
80 |
+
p11 = Polygon(Point(0, 0), 1, n=3)
|
81 |
+
p12 = Polygon(Point(0, 0), 1, 0, n=3)
|
82 |
+
|
83 |
+
r = Ray(Point(-9, 6.6), Point(-9, 5.5))
|
84 |
+
#
|
85 |
+
# General polygon
|
86 |
+
#
|
87 |
+
assert p1 == p2
|
88 |
+
assert len(p1.args) == 6
|
89 |
+
assert len(p1.sides) == 6
|
90 |
+
assert p1.perimeter == 5 + 2*sqrt(10) + sqrt(29) + sqrt(8)
|
91 |
+
assert p1.area == 22
|
92 |
+
assert not p1.is_convex()
|
93 |
+
assert Polygon((-1, 1), (2, -1), (2, 1), (-1, -1), (3, 0)
|
94 |
+
).is_convex() is False
|
95 |
+
# ensure convex for both CW and CCW point specification
|
96 |
+
assert p3.is_convex()
|
97 |
+
assert p4.is_convex()
|
98 |
+
dict5 = p5.angles
|
99 |
+
assert dict5[Point(0, 0)] == pi / 4
|
100 |
+
assert dict5[Point(0, 4)] == pi / 2
|
101 |
+
assert p5.encloses_point(Point(x, y)) is None
|
102 |
+
assert p5.encloses_point(Point(1, 3))
|
103 |
+
assert p5.encloses_point(Point(0, 0)) is False
|
104 |
+
assert p5.encloses_point(Point(4, 0)) is False
|
105 |
+
assert p1.encloses(Circle(Point(2.5, 2.5), 5)) is False
|
106 |
+
assert p1.encloses(Ellipse(Point(2.5, 2), 5, 6)) is False
|
107 |
+
assert p5.plot_interval('x') == [x, 0, 1]
|
108 |
+
assert p5.distance(
|
109 |
+
Polygon(Point(10, 10), Point(14, 14), Point(10, 14))) == 6 * sqrt(2)
|
110 |
+
assert p5.distance(
|
111 |
+
Polygon(Point(1, 8), Point(5, 8), Point(8, 12), Point(1, 12))) == 4
|
112 |
+
with warns(UserWarning, \
|
113 |
+
match="Polygons may intersect producing erroneous output"):
|
114 |
+
Polygon(Point(0, 0), Point(1, 0), Point(1, 1)).distance(
|
115 |
+
Polygon(Point(0, 0), Point(0, 1), Point(1, 1)))
|
116 |
+
assert hash(p5) == hash(Polygon(Point(0, 0), Point(4, 4), Point(0, 4)))
|
117 |
+
assert hash(p1) == hash(p2)
|
118 |
+
assert hash(p7) == hash(p8)
|
119 |
+
assert hash(p3) != hash(p9)
|
120 |
+
assert p5 == Polygon(Point(4, 4), Point(0, 4), Point(0, 0))
|
121 |
+
assert Polygon(Point(4, 4), Point(0, 4), Point(0, 0)) in p5
|
122 |
+
assert p5 != Point(0, 4)
|
123 |
+
assert Point(0, 1) in p5
|
124 |
+
assert p5.arbitrary_point('t').subs(Symbol('t', real=True), 0) == \
|
125 |
+
Point(0, 0)
|
126 |
+
raises(ValueError, lambda: Polygon(
|
127 |
+
Point(x, 0), Point(0, y), Point(x, y)).arbitrary_point('x'))
|
128 |
+
assert p6.intersection(r) == [Point(-9, Rational(-84, 13)), Point(-9, Rational(33, 5))]
|
129 |
+
assert p10.area == 0
|
130 |
+
assert p11 == RegularPolygon(Point(0, 0), 1, 3, 0)
|
131 |
+
assert p11 == p12
|
132 |
+
assert p11.vertices[0] == Point(1, 0)
|
133 |
+
assert p11.args[0] == Point(0, 0)
|
134 |
+
p11.spin(pi/2)
|
135 |
+
assert p11.vertices[0] == Point(0, 1)
|
136 |
+
#
|
137 |
+
# Regular polygon
|
138 |
+
#
|
139 |
+
p1 = RegularPolygon(Point(0, 0), 10, 5)
|
140 |
+
p2 = RegularPolygon(Point(0, 0), 5, 5)
|
141 |
+
raises(GeometryError, lambda: RegularPolygon(Point(0, 0), Point(0,
|
142 |
+
1), Point(1, 1)))
|
143 |
+
raises(GeometryError, lambda: RegularPolygon(Point(0, 0), 1, 2))
|
144 |
+
raises(ValueError, lambda: RegularPolygon(Point(0, 0), 1, 2.5))
|
145 |
+
|
146 |
+
assert p1 != p2
|
147 |
+
assert p1.interior_angle == pi*Rational(3, 5)
|
148 |
+
assert p1.exterior_angle == pi*Rational(2, 5)
|
149 |
+
assert p2.apothem == 5*cos(pi/5)
|
150 |
+
assert p2.circumcenter == p1.circumcenter == Point(0, 0)
|
151 |
+
assert p1.circumradius == p1.radius == 10
|
152 |
+
assert p2.circumcircle == Circle(Point(0, 0), 5)
|
153 |
+
assert p2.incircle == Circle(Point(0, 0), p2.apothem)
|
154 |
+
assert p2.inradius == p2.apothem == (5 * (1 + sqrt(5)) / 4)
|
155 |
+
p2.spin(pi / 10)
|
156 |
+
dict1 = p2.angles
|
157 |
+
assert dict1[Point(0, 5)] == 3 * pi / 5
|
158 |
+
assert p1.is_convex()
|
159 |
+
assert p1.rotation == 0
|
160 |
+
assert p1.encloses_point(Point(0, 0))
|
161 |
+
assert p1.encloses_point(Point(11, 0)) is False
|
162 |
+
assert p2.encloses_point(Point(0, 4.9))
|
163 |
+
p1.spin(pi/3)
|
164 |
+
assert p1.rotation == pi/3
|
165 |
+
assert p1.vertices[0] == Point(5, 5*sqrt(3))
|
166 |
+
for var in p1.args:
|
167 |
+
if isinstance(var, Point):
|
168 |
+
assert var == Point(0, 0)
|
169 |
+
else:
|
170 |
+
assert var in (5, 10, pi / 3)
|
171 |
+
assert p1 != Point(0, 0)
|
172 |
+
assert p1 != p5
|
173 |
+
|
174 |
+
# while spin works in place (notice that rotation is 2pi/3 below)
|
175 |
+
# rotate returns a new object
|
176 |
+
p1_old = p1
|
177 |
+
assert p1.rotate(pi/3) == RegularPolygon(Point(0, 0), 10, 5, pi*Rational(2, 3))
|
178 |
+
assert p1 == p1_old
|
179 |
+
|
180 |
+
assert p1.area == (-250*sqrt(5) + 1250)/(4*tan(pi/5))
|
181 |
+
assert p1.length == 20*sqrt(-sqrt(5)/8 + Rational(5, 8))
|
182 |
+
assert p1.scale(2, 2) == \
|
183 |
+
RegularPolygon(p1.center, p1.radius*2, p1._n, p1.rotation)
|
184 |
+
assert RegularPolygon((0, 0), 1, 4).scale(2, 3) == \
|
185 |
+
Polygon(Point(2, 0), Point(0, 3), Point(-2, 0), Point(0, -3))
|
186 |
+
|
187 |
+
assert repr(p1) == str(p1)
|
188 |
+
|
189 |
+
#
|
190 |
+
# Angles
|
191 |
+
#
|
192 |
+
angles = p4.angles
|
193 |
+
assert feq(angles[Point(0, 0)].evalf(), Float("0.7853981633974483"))
|
194 |
+
assert feq(angles[Point(4, 4)].evalf(), Float("1.2490457723982544"))
|
195 |
+
assert feq(angles[Point(5, 2)].evalf(), Float("1.8925468811915388"))
|
196 |
+
assert feq(angles[Point(3, 0)].evalf(), Float("2.3561944901923449"))
|
197 |
+
|
198 |
+
angles = p3.angles
|
199 |
+
assert feq(angles[Point(0, 0)].evalf(), Float("0.7853981633974483"))
|
200 |
+
assert feq(angles[Point(4, 4)].evalf(), Float("1.2490457723982544"))
|
201 |
+
assert feq(angles[Point(5, 2)].evalf(), Float("1.8925468811915388"))
|
202 |
+
assert feq(angles[Point(3, 0)].evalf(), Float("2.3561944901923449"))
|
203 |
+
|
204 |
+
#
|
205 |
+
# Triangle
|
206 |
+
#
|
207 |
+
p1 = Point(0, 0)
|
208 |
+
p2 = Point(5, 0)
|
209 |
+
p3 = Point(0, 5)
|
210 |
+
t1 = Triangle(p1, p2, p3)
|
211 |
+
t2 = Triangle(p1, p2, Point(Rational(5, 2), sqrt(Rational(75, 4))))
|
212 |
+
t3 = Triangle(p1, Point(x1, 0), Point(0, x1))
|
213 |
+
s1 = t1.sides
|
214 |
+
assert Triangle(p1, p2, p1) == Polygon(p1, p2, p1) == Segment(p1, p2)
|
215 |
+
raises(GeometryError, lambda: Triangle(Point(0, 0)))
|
216 |
+
|
217 |
+
# Basic stuff
|
218 |
+
assert Triangle(p1, p1, p1) == p1
|
219 |
+
assert Triangle(p2, p2*2, p2*3) == Segment(p2, p2*3)
|
220 |
+
assert t1.area == Rational(25, 2)
|
221 |
+
assert t1.is_right()
|
222 |
+
assert t2.is_right() is False
|
223 |
+
assert t3.is_right()
|
224 |
+
assert p1 in t1
|
225 |
+
assert t1.sides[0] in t1
|
226 |
+
assert Segment((0, 0), (1, 0)) in t1
|
227 |
+
assert Point(5, 5) not in t2
|
228 |
+
assert t1.is_convex()
|
229 |
+
assert feq(t1.angles[p1].evalf(), pi.evalf()/2)
|
230 |
+
|
231 |
+
assert t1.is_equilateral() is False
|
232 |
+
assert t2.is_equilateral()
|
233 |
+
assert t3.is_equilateral() is False
|
234 |
+
assert are_similar(t1, t2) is False
|
235 |
+
assert are_similar(t1, t3)
|
236 |
+
assert are_similar(t2, t3) is False
|
237 |
+
assert t1.is_similar(Point(0, 0)) is False
|
238 |
+
assert t1.is_similar(t2) is False
|
239 |
+
|
240 |
+
# Bisectors
|
241 |
+
bisectors = t1.bisectors()
|
242 |
+
assert bisectors[p1] == Segment(
|
243 |
+
p1, Point(Rational(5, 2), Rational(5, 2)))
|
244 |
+
assert t2.bisectors()[p2] == Segment(
|
245 |
+
Point(5, 0), Point(Rational(5, 4), 5*sqrt(3)/4))
|
246 |
+
p4 = Point(0, x1)
|
247 |
+
assert t3.bisectors()[p4] == Segment(p4, Point(x1*(sqrt(2) - 1), 0))
|
248 |
+
ic = (250 - 125*sqrt(2))/50
|
249 |
+
assert t1.incenter == Point(ic, ic)
|
250 |
+
|
251 |
+
# Inradius
|
252 |
+
assert t1.inradius == t1.incircle.radius == 5 - 5*sqrt(2)/2
|
253 |
+
assert t2.inradius == t2.incircle.radius == 5*sqrt(3)/6
|
254 |
+
assert t3.inradius == t3.incircle.radius == x1**2/((2 + sqrt(2))*Abs(x1))
|
255 |
+
|
256 |
+
# Exradius
|
257 |
+
assert t1.exradii[t1.sides[2]] == 5*sqrt(2)/2
|
258 |
+
|
259 |
+
# Excenters
|
260 |
+
assert t1.excenters[t1.sides[2]] == Point2D(25*sqrt(2), -5*sqrt(2)/2)
|
261 |
+
|
262 |
+
# Circumcircle
|
263 |
+
assert t1.circumcircle.center == Point(2.5, 2.5)
|
264 |
+
|
265 |
+
# Medians + Centroid
|
266 |
+
m = t1.medians
|
267 |
+
assert t1.centroid == Point(Rational(5, 3), Rational(5, 3))
|
268 |
+
assert m[p1] == Segment(p1, Point(Rational(5, 2), Rational(5, 2)))
|
269 |
+
assert t3.medians[p1] == Segment(p1, Point(x1/2, x1/2))
|
270 |
+
assert intersection(m[p1], m[p2], m[p3]) == [t1.centroid]
|
271 |
+
assert t1.medial == Triangle(Point(2.5, 0), Point(0, 2.5), Point(2.5, 2.5))
|
272 |
+
|
273 |
+
# Nine-point circle
|
274 |
+
assert t1.nine_point_circle == Circle(Point(2.5, 0),
|
275 |
+
Point(0, 2.5), Point(2.5, 2.5))
|
276 |
+
assert t1.nine_point_circle == Circle(Point(0, 0),
|
277 |
+
Point(0, 2.5), Point(2.5, 2.5))
|
278 |
+
|
279 |
+
# Perpendicular
|
280 |
+
altitudes = t1.altitudes
|
281 |
+
assert altitudes[p1] == Segment(p1, Point(Rational(5, 2), Rational(5, 2)))
|
282 |
+
assert altitudes[p2].equals(s1[0])
|
283 |
+
assert altitudes[p3] == s1[2]
|
284 |
+
assert t1.orthocenter == p1
|
285 |
+
t = S('''Triangle(
|
286 |
+
Point(100080156402737/5000000000000, 79782624633431/500000000000),
|
287 |
+
Point(39223884078253/2000000000000, 156345163124289/1000000000000),
|
288 |
+
Point(31241359188437/1250000000000, 338338270939941/1000000000000000))''')
|
289 |
+
assert t.orthocenter == S('''Point(-780660869050599840216997'''
|
290 |
+
'''79471538701955848721853/80368430960602242240789074233100000000000000,'''
|
291 |
+
'''20151573611150265741278060334545897615974257/16073686192120448448157'''
|
292 |
+
'''8148466200000000000)''')
|
293 |
+
|
294 |
+
# Ensure
|
295 |
+
assert len(intersection(*bisectors.values())) == 1
|
296 |
+
assert len(intersection(*altitudes.values())) == 1
|
297 |
+
assert len(intersection(*m.values())) == 1
|
298 |
+
|
299 |
+
# Distance
|
300 |
+
p1 = Polygon(
|
301 |
+
Point(0, 0), Point(1, 0),
|
302 |
+
Point(1, 1), Point(0, 1))
|
303 |
+
p2 = Polygon(
|
304 |
+
Point(0, Rational(5)/4), Point(1, Rational(5)/4),
|
305 |
+
Point(1, Rational(9)/4), Point(0, Rational(9)/4))
|
306 |
+
p3 = Polygon(
|
307 |
+
Point(1, 2), Point(2, 2),
|
308 |
+
Point(2, 1))
|
309 |
+
p4 = Polygon(
|
310 |
+
Point(1, 1), Point(Rational(6)/5, 1),
|
311 |
+
Point(1, Rational(6)/5))
|
312 |
+
pt1 = Point(half, half)
|
313 |
+
pt2 = Point(1, 1)
|
314 |
+
|
315 |
+
'''Polygon to Point'''
|
316 |
+
assert p1.distance(pt1) == half
|
317 |
+
assert p1.distance(pt2) == 0
|
318 |
+
assert p2.distance(pt1) == Rational(3)/4
|
319 |
+
assert p3.distance(pt2) == sqrt(2)/2
|
320 |
+
|
321 |
+
'''Polygon to Polygon'''
|
322 |
+
# p1.distance(p2) emits a warning
|
323 |
+
with warns(UserWarning, \
|
324 |
+
match="Polygons may intersect producing erroneous output"):
|
325 |
+
assert p1.distance(p2) == half/2
|
326 |
+
|
327 |
+
assert p1.distance(p3) == sqrt(2)/2
|
328 |
+
|
329 |
+
# p3.distance(p4) emits a warning
|
330 |
+
with warns(UserWarning, \
|
331 |
+
match="Polygons may intersect producing erroneous output"):
|
332 |
+
assert p3.distance(p4) == (sqrt(2)/2 - sqrt(Rational(2)/25)/2)
|
333 |
+
|
334 |
+
|
335 |
+
def test_convex_hull():
|
336 |
+
p = [Point(-5, -1), Point(-2, 1), Point(-2, -1), Point(-1, -3), \
|
337 |
+
Point(0, 0), Point(1, 1), Point(2, 2), Point(2, -1), Point(3, 1), \
|
338 |
+
Point(4, -1), Point(6, 2)]
|
339 |
+
ch = Polygon(p[0], p[3], p[9], p[10], p[6], p[1])
|
340 |
+
#test handling of duplicate points
|
341 |
+
p.append(p[3])
|
342 |
+
|
343 |
+
#more than 3 collinear points
|
344 |
+
another_p = [Point(-45, -85), Point(-45, 85), Point(-45, 26), \
|
345 |
+
Point(-45, -24)]
|
346 |
+
ch2 = Segment(another_p[0], another_p[1])
|
347 |
+
|
348 |
+
assert convex_hull(*another_p) == ch2
|
349 |
+
assert convex_hull(*p) == ch
|
350 |
+
assert convex_hull(p[0]) == p[0]
|
351 |
+
assert convex_hull(p[0], p[1]) == Segment(p[0], p[1])
|
352 |
+
|
353 |
+
# no unique points
|
354 |
+
assert convex_hull(*[p[-1]]*3) == p[-1]
|
355 |
+
|
356 |
+
# collection of items
|
357 |
+
assert convex_hull(*[Point(0, 0), \
|
358 |
+
Segment(Point(1, 0), Point(1, 1)), \
|
359 |
+
RegularPolygon(Point(2, 0), 2, 4)]) == \
|
360 |
+
Polygon(Point(0, 0), Point(2, -2), Point(4, 0), Point(2, 2))
|
361 |
+
|
362 |
+
|
363 |
+
def test_encloses():
|
364 |
+
# square with a dimpled left side
|
365 |
+
s = Polygon(Point(0, 0), Point(1, 0), Point(1, 1), Point(0, 1), \
|
366 |
+
Point(S.Half, S.Half))
|
367 |
+
# the following is True if the polygon isn't treated as closing on itself
|
368 |
+
assert s.encloses(Point(0, S.Half)) is False
|
369 |
+
assert s.encloses(Point(S.Half, S.Half)) is False # it's a vertex
|
370 |
+
assert s.encloses(Point(Rational(3, 4), S.Half)) is True
|
371 |
+
|
372 |
+
|
373 |
+
def test_triangle_kwargs():
|
374 |
+
assert Triangle(sss=(3, 4, 5)) == \
|
375 |
+
Triangle(Point(0, 0), Point(3, 0), Point(3, 4))
|
376 |
+
assert Triangle(asa=(30, 2, 30)) == \
|
377 |
+
Triangle(Point(0, 0), Point(2, 0), Point(1, sqrt(3)/3))
|
378 |
+
assert Triangle(sas=(1, 45, 2)) == \
|
379 |
+
Triangle(Point(0, 0), Point(2, 0), Point(sqrt(2)/2, sqrt(2)/2))
|
380 |
+
assert Triangle(sss=(1, 2, 5)) is None
|
381 |
+
assert deg(rad(180)) == 180
|
382 |
+
|
383 |
+
|
384 |
+
def test_transform():
|
385 |
+
pts = [Point(0, 0), Point(S.Half, Rational(1, 4)), Point(1, 1)]
|
386 |
+
pts_out = [Point(-4, -10), Point(-3, Rational(-37, 4)), Point(-2, -7)]
|
387 |
+
assert Triangle(*pts).scale(2, 3, (4, 5)) == Triangle(*pts_out)
|
388 |
+
assert RegularPolygon((0, 0), 1, 4).scale(2, 3, (4, 5)) == \
|
389 |
+
Polygon(Point(-2, -10), Point(-4, -7), Point(-6, -10), Point(-4, -13))
|
390 |
+
# Checks for symmetric scaling
|
391 |
+
assert RegularPolygon((0, 0), 1, 4).scale(2, 2) == \
|
392 |
+
RegularPolygon(Point2D(0, 0), 2, 4, 0)
|
393 |
+
|
394 |
+
def test_reflect():
|
395 |
+
x = Symbol('x', real=True)
|
396 |
+
y = Symbol('y', real=True)
|
397 |
+
b = Symbol('b')
|
398 |
+
m = Symbol('m')
|
399 |
+
l = Line((0, b), slope=m)
|
400 |
+
p = Point(x, y)
|
401 |
+
r = p.reflect(l)
|
402 |
+
dp = l.perpendicular_segment(p).length
|
403 |
+
dr = l.perpendicular_segment(r).length
|
404 |
+
|
405 |
+
assert verify_numerically(dp, dr)
|
406 |
+
|
407 |
+
assert Polygon((1, 0), (2, 0), (2, 2)).reflect(Line((3, 0), slope=oo)) \
|
408 |
+
== Triangle(Point(5, 0), Point(4, 0), Point(4, 2))
|
409 |
+
assert Polygon((1, 0), (2, 0), (2, 2)).reflect(Line((0, 3), slope=oo)) \
|
410 |
+
== Triangle(Point(-1, 0), Point(-2, 0), Point(-2, 2))
|
411 |
+
assert Polygon((1, 0), (2, 0), (2, 2)).reflect(Line((0, 3), slope=0)) \
|
412 |
+
== Triangle(Point(1, 6), Point(2, 6), Point(2, 4))
|
413 |
+
assert Polygon((1, 0), (2, 0), (2, 2)).reflect(Line((3, 0), slope=0)) \
|
414 |
+
== Triangle(Point(1, 0), Point(2, 0), Point(2, -2))
|
415 |
+
|
416 |
+
def test_bisectors():
|
417 |
+
p1, p2, p3 = Point(0, 0), Point(1, 0), Point(0, 1)
|
418 |
+
p = Polygon(Point(0, 0), Point(2, 0), Point(1, 1), Point(0, 3))
|
419 |
+
q = Polygon(Point(1, 0), Point(2, 0), Point(3, 3), Point(-1, 5))
|
420 |
+
poly = Polygon(Point(3, 4), Point(0, 0), Point(8, 7), Point(-1, 1), Point(19, -19))
|
421 |
+
t = Triangle(p1, p2, p3)
|
422 |
+
assert t.bisectors()[p2] == Segment(Point(1, 0), Point(0, sqrt(2) - 1))
|
423 |
+
assert p.bisectors()[Point2D(0, 3)] == Ray2D(Point2D(0, 3), \
|
424 |
+
Point2D(sin(acos(2*sqrt(5)/5)/2), 3 - cos(acos(2*sqrt(5)/5)/2)))
|
425 |
+
assert q.bisectors()[Point2D(-1, 5)] == \
|
426 |
+
Ray2D(Point2D(-1, 5), Point2D(-1 + sqrt(29)*(5*sin(acos(9*sqrt(145)/145)/2) + \
|
427 |
+
2*cos(acos(9*sqrt(145)/145)/2))/29, sqrt(29)*(-5*cos(acos(9*sqrt(145)/145)/2) + \
|
428 |
+
2*sin(acos(9*sqrt(145)/145)/2))/29 + 5))
|
429 |
+
assert poly.bisectors()[Point2D(-1, 1)] == Ray2D(Point2D(-1, 1), \
|
430 |
+
Point2D(-1 + sin(acos(sqrt(26)/26)/2 + pi/4), 1 - sin(-acos(sqrt(26)/26)/2 + pi/4)))
|
431 |
+
|
432 |
+
def test_incenter():
|
433 |
+
assert Triangle(Point(0, 0), Point(1, 0), Point(0, 1)).incenter \
|
434 |
+
== Point(1 - sqrt(2)/2, 1 - sqrt(2)/2)
|
435 |
+
|
436 |
+
def test_inradius():
|
437 |
+
assert Triangle(Point(0, 0), Point(4, 0), Point(0, 3)).inradius == 1
|
438 |
+
|
439 |
+
def test_incircle():
|
440 |
+
assert Triangle(Point(0, 0), Point(2, 0), Point(0, 2)).incircle \
|
441 |
+
== Circle(Point(2 - sqrt(2), 2 - sqrt(2)), 2 - sqrt(2))
|
442 |
+
|
443 |
+
def test_exradii():
|
444 |
+
t = Triangle(Point(0, 0), Point(6, 0), Point(0, 2))
|
445 |
+
assert t.exradii[t.sides[2]] == (-2 + sqrt(10))
|
446 |
+
|
447 |
+
def test_medians():
|
448 |
+
t = Triangle(Point(0, 0), Point(1, 0), Point(0, 1))
|
449 |
+
assert t.medians[Point(0, 0)] == Segment(Point(0, 0), Point(S.Half, S.Half))
|
450 |
+
|
451 |
+
def test_medial():
|
452 |
+
assert Triangle(Point(0, 0), Point(1, 0), Point(0, 1)).medial \
|
453 |
+
== Triangle(Point(S.Half, 0), Point(S.Half, S.Half), Point(0, S.Half))
|
454 |
+
|
455 |
+
def test_nine_point_circle():
|
456 |
+
assert Triangle(Point(0, 0), Point(1, 0), Point(0, 1)).nine_point_circle \
|
457 |
+
== Circle(Point2D(Rational(1, 4), Rational(1, 4)), sqrt(2)/4)
|
458 |
+
|
459 |
+
def test_eulerline():
|
460 |
+
assert Triangle(Point(0, 0), Point(1, 0), Point(0, 1)).eulerline \
|
461 |
+
== Line(Point2D(0, 0), Point2D(S.Half, S.Half))
|
462 |
+
assert Triangle(Point(0, 0), Point(10, 0), Point(5, 5*sqrt(3))).eulerline \
|
463 |
+
== Point2D(5, 5*sqrt(3)/3)
|
464 |
+
assert Triangle(Point(4, -6), Point(4, -1), Point(-3, 3)).eulerline \
|
465 |
+
== Line(Point2D(Rational(64, 7), 3), Point2D(Rational(-29, 14), Rational(-7, 2)))
|
466 |
+
|
467 |
+
def test_intersection():
|
468 |
+
poly1 = Triangle(Point(0, 0), Point(1, 0), Point(0, 1))
|
469 |
+
poly2 = Polygon(Point(0, 1), Point(-5, 0),
|
470 |
+
Point(0, -4), Point(0, Rational(1, 5)),
|
471 |
+
Point(S.Half, -0.1), Point(1, 0), Point(0, 1))
|
472 |
+
|
473 |
+
assert poly1.intersection(poly2) == [Point2D(Rational(1, 3), 0),
|
474 |
+
Segment(Point(0, Rational(1, 5)), Point(0, 0)),
|
475 |
+
Segment(Point(1, 0), Point(0, 1))]
|
476 |
+
assert poly2.intersection(poly1) == [Point(Rational(1, 3), 0),
|
477 |
+
Segment(Point(0, 0), Point(0, Rational(1, 5))),
|
478 |
+
Segment(Point(1, 0), Point(0, 1))]
|
479 |
+
assert poly1.intersection(Point(0, 0)) == [Point(0, 0)]
|
480 |
+
assert poly1.intersection(Point(-12, -43)) == []
|
481 |
+
assert poly2.intersection(Line((-12, 0), (12, 0))) == [Point(-5, 0),
|
482 |
+
Point(0, 0), Point(Rational(1, 3), 0), Point(1, 0)]
|
483 |
+
assert poly2.intersection(Line((-12, 12), (12, 12))) == []
|
484 |
+
assert poly2.intersection(Ray((-3, 4), (1, 0))) == [Segment(Point(1, 0),
|
485 |
+
Point(0, 1))]
|
486 |
+
assert poly2.intersection(Circle((0, -1), 1)) == [Point(0, -2),
|
487 |
+
Point(0, 0)]
|
488 |
+
assert poly1.intersection(poly1) == [Segment(Point(0, 0), Point(1, 0)),
|
489 |
+
Segment(Point(0, 1), Point(0, 0)), Segment(Point(1, 0), Point(0, 1))]
|
490 |
+
assert poly2.intersection(poly2) == [Segment(Point(-5, 0), Point(0, -4)),
|
491 |
+
Segment(Point(0, -4), Point(0, Rational(1, 5))),
|
492 |
+
Segment(Point(0, Rational(1, 5)), Point(S.Half, Rational(-1, 10))),
|
493 |
+
Segment(Point(0, 1), Point(-5, 0)),
|
494 |
+
Segment(Point(S.Half, Rational(-1, 10)), Point(1, 0)),
|
495 |
+
Segment(Point(1, 0), Point(0, 1))]
|
496 |
+
assert poly2.intersection(Triangle(Point(0, 1), Point(1, 0), Point(-1, 1))) \
|
497 |
+
== [Point(Rational(-5, 7), Rational(6, 7)), Segment(Point2D(0, 1), Point(1, 0))]
|
498 |
+
assert poly1.intersection(RegularPolygon((-12, -15), 3, 3)) == []
|
499 |
+
|
500 |
+
|
501 |
+
def test_parameter_value():
|
502 |
+
t = Symbol('t')
|
503 |
+
sq = Polygon((0, 0), (0, 1), (1, 1), (1, 0))
|
504 |
+
assert sq.parameter_value((0.5, 1), t) == {t: Rational(3, 8)}
|
505 |
+
q = Polygon((0, 0), (2, 1), (2, 4), (4, 0))
|
506 |
+
assert q.parameter_value((4, 0), t) == {t: -6 + 3*sqrt(5)} # ~= 0.708
|
507 |
+
|
508 |
+
raises(ValueError, lambda: sq.parameter_value((5, 6), t))
|
509 |
+
raises(ValueError, lambda: sq.parameter_value(Circle(Point(0, 0), 1), t))
|
510 |
+
|
511 |
+
|
512 |
+
def test_issue_12966():
|
513 |
+
poly = Polygon(Point(0, 0), Point(0, 10), Point(5, 10), Point(5, 5),
|
514 |
+
Point(10, 5), Point(10, 0))
|
515 |
+
t = Symbol('t')
|
516 |
+
pt = poly.arbitrary_point(t)
|
517 |
+
DELTA = 5/poly.perimeter
|
518 |
+
assert [pt.subs(t, DELTA*i) for i in range(int(1/DELTA))] == [
|
519 |
+
Point(0, 0), Point(0, 5), Point(0, 10), Point(5, 10),
|
520 |
+
Point(5, 5), Point(10, 5), Point(10, 0), Point(5, 0)]
|
521 |
+
|
522 |
+
|
523 |
+
def test_second_moment_of_area():
|
524 |
+
x, y = symbols('x, y')
|
525 |
+
# triangle
|
526 |
+
p1, p2, p3 = [(0, 0), (4, 0), (0, 2)]
|
527 |
+
p = (0, 0)
|
528 |
+
# equation of hypotenuse
|
529 |
+
eq_y = (1-x/4)*2
|
530 |
+
I_yy = integrate((x**2) * (integrate(1, (y, 0, eq_y))), (x, 0, 4))
|
531 |
+
I_xx = integrate(1 * (integrate(y**2, (y, 0, eq_y))), (x, 0, 4))
|
532 |
+
I_xy = integrate(x * (integrate(y, (y, 0, eq_y))), (x, 0, 4))
|
533 |
+
|
534 |
+
triangle = Polygon(p1, p2, p3)
|
535 |
+
|
536 |
+
assert (I_xx - triangle.second_moment_of_area(p)[0]) == 0
|
537 |
+
assert (I_yy - triangle.second_moment_of_area(p)[1]) == 0
|
538 |
+
assert (I_xy - triangle.second_moment_of_area(p)[2]) == 0
|
539 |
+
|
540 |
+
# rectangle
|
541 |
+
p1, p2, p3, p4=[(0, 0), (4, 0), (4, 2), (0, 2)]
|
542 |
+
I_yy = integrate((x**2) * integrate(1, (y, 0, 2)), (x, 0, 4))
|
543 |
+
I_xx = integrate(1 * integrate(y**2, (y, 0, 2)), (x, 0, 4))
|
544 |
+
I_xy = integrate(x * integrate(y, (y, 0, 2)), (x, 0, 4))
|
545 |
+
|
546 |
+
rectangle = Polygon(p1, p2, p3, p4)
|
547 |
+
|
548 |
+
assert (I_xx - rectangle.second_moment_of_area(p)[0]) == 0
|
549 |
+
assert (I_yy - rectangle.second_moment_of_area(p)[1]) == 0
|
550 |
+
assert (I_xy - rectangle.second_moment_of_area(p)[2]) == 0
|
551 |
+
|
552 |
+
|
553 |
+
r = RegularPolygon(Point(0, 0), 5, 3)
|
554 |
+
assert r.second_moment_of_area() == (1875*sqrt(3)/S(32), 1875*sqrt(3)/S(32), 0)
|
555 |
+
|
556 |
+
|
557 |
+
def test_first_moment():
|
558 |
+
a, b = symbols('a, b', positive=True)
|
559 |
+
# rectangle
|
560 |
+
p1 = Polygon((0, 0), (a, 0), (a, b), (0, b))
|
561 |
+
assert p1.first_moment_of_area() == (a*b**2/8, a**2*b/8)
|
562 |
+
assert p1.first_moment_of_area((a/3, b/4)) == (-3*a*b**2/32, -a**2*b/9)
|
563 |
+
|
564 |
+
p1 = Polygon((0, 0), (40, 0), (40, 30), (0, 30))
|
565 |
+
assert p1.first_moment_of_area() == (4500, 6000)
|
566 |
+
|
567 |
+
# triangle
|
568 |
+
p2 = Polygon((0, 0), (a, 0), (a/2, b))
|
569 |
+
assert p2.first_moment_of_area() == (4*a*b**2/81, a**2*b/24)
|
570 |
+
assert p2.first_moment_of_area((a/8, b/6)) == (-25*a*b**2/648, -5*a**2*b/768)
|
571 |
+
|
572 |
+
p2 = Polygon((0, 0), (12, 0), (12, 30))
|
573 |
+
assert p2.first_moment_of_area() == (S(1600)/3, -S(640)/3)
|
574 |
+
|
575 |
+
|
576 |
+
def test_section_modulus_and_polar_second_moment_of_area():
|
577 |
+
a, b = symbols('a, b', positive=True)
|
578 |
+
x, y = symbols('x, y')
|
579 |
+
rectangle = Polygon((0, b), (0, 0), (a, 0), (a, b))
|
580 |
+
assert rectangle.section_modulus(Point(x, y)) == (a*b**3/12/(-b/2 + y), a**3*b/12/(-a/2 + x))
|
581 |
+
assert rectangle.polar_second_moment_of_area() == a**3*b/12 + a*b**3/12
|
582 |
+
|
583 |
+
convex = RegularPolygon((0, 0), 1, 6)
|
584 |
+
assert convex.section_modulus() == (Rational(5, 8), sqrt(3)*Rational(5, 16))
|
585 |
+
assert convex.polar_second_moment_of_area() == 5*sqrt(3)/S(8)
|
586 |
+
|
587 |
+
concave = Polygon((0, 0), (1, 8), (3, 4), (4, 6), (7, 1))
|
588 |
+
assert concave.section_modulus() == (Rational(-6371, 429), Rational(-9778, 519))
|
589 |
+
assert concave.polar_second_moment_of_area() == Rational(-38669, 252)
|
590 |
+
|
591 |
+
|
592 |
+
def test_cut_section():
|
593 |
+
# concave polygon
|
594 |
+
p = Polygon((-1, -1), (1, Rational(5, 2)), (2, 1), (3, Rational(5, 2)), (4, 2), (5, 3), (-1, 3))
|
595 |
+
l = Line((0, 0), (Rational(9, 2), 3))
|
596 |
+
p1 = p.cut_section(l)[0]
|
597 |
+
p2 = p.cut_section(l)[1]
|
598 |
+
assert p1 == Polygon(
|
599 |
+
Point2D(Rational(-9, 13), Rational(-6, 13)), Point2D(1, Rational(5, 2)), Point2D(Rational(24, 13), Rational(16, 13)),
|
600 |
+
Point2D(Rational(12, 5), Rational(8, 5)), Point2D(3, Rational(5, 2)), Point2D(Rational(24, 7), Rational(16, 7)),
|
601 |
+
Point2D(Rational(9, 2), 3), Point2D(-1, 3), Point2D(-1, Rational(-2, 3)))
|
602 |
+
assert p2 == Polygon(Point2D(-1, -1), Point2D(Rational(-9, 13), Rational(-6, 13)), Point2D(Rational(24, 13), Rational(16, 13)),
|
603 |
+
Point2D(2, 1), Point2D(Rational(12, 5), Rational(8, 5)), Point2D(Rational(24, 7), Rational(16, 7)), Point2D(4, 2), Point2D(5, 3),
|
604 |
+
Point2D(Rational(9, 2), 3), Point2D(-1, Rational(-2, 3)))
|
605 |
+
|
606 |
+
# convex polygon
|
607 |
+
p = RegularPolygon(Point2D(0, 0), 6, 6)
|
608 |
+
s = p.cut_section(Line((0, 0), slope=1))
|
609 |
+
assert s[0] == Polygon(Point2D(-3*sqrt(3) + 9, -3*sqrt(3) + 9), Point2D(3, 3*sqrt(3)),
|
610 |
+
Point2D(-3, 3*sqrt(3)), Point2D(-6, 0), Point2D(-9 + 3*sqrt(3), -9 + 3*sqrt(3)))
|
611 |
+
assert s[1] == Polygon(Point2D(6, 0), Point2D(-3*sqrt(3) + 9, -3*sqrt(3) + 9),
|
612 |
+
Point2D(-9 + 3*sqrt(3), -9 + 3*sqrt(3)), Point2D(-3, -3*sqrt(3)), Point2D(3, -3*sqrt(3)))
|
613 |
+
|
614 |
+
# case where line does not intersects but coincides with the edge of polygon
|
615 |
+
a, b = 20, 10
|
616 |
+
t1, t2, t3, t4 = [(0, b), (0, 0), (a, 0), (a, b)]
|
617 |
+
p = Polygon(t1, t2, t3, t4)
|
618 |
+
p1, p2 = p.cut_section(Line((0, b), slope=0))
|
619 |
+
assert p1 == None
|
620 |
+
assert p2 == Polygon(Point2D(0, 10), Point2D(0, 0), Point2D(20, 0), Point2D(20, 10))
|
621 |
+
|
622 |
+
p3, p4 = p.cut_section(Line((0, 0), slope=0))
|
623 |
+
assert p3 == Polygon(Point2D(0, 10), Point2D(0, 0), Point2D(20, 0), Point2D(20, 10))
|
624 |
+
assert p4 == None
|
625 |
+
|
626 |
+
# case where the line does not intersect with a polygon at all
|
627 |
+
raises(ValueError, lambda: p.cut_section(Line((0, a), slope=0)))
|
628 |
+
|
629 |
+
def test_type_of_triangle():
|
630 |
+
# Isoceles triangle
|
631 |
+
p1 = Polygon(Point(0, 0), Point(5, 0), Point(2, 4))
|
632 |
+
assert p1.is_isosceles() == True
|
633 |
+
assert p1.is_scalene() == False
|
634 |
+
assert p1.is_equilateral() == False
|
635 |
+
|
636 |
+
# Scalene triangle
|
637 |
+
p2 = Polygon (Point(0, 0), Point(0, 2), Point(4, 0))
|
638 |
+
assert p2.is_isosceles() == False
|
639 |
+
assert p2.is_scalene() == True
|
640 |
+
assert p2.is_equilateral() == False
|
641 |
+
|
642 |
+
# Equilateral triagle
|
643 |
+
p3 = Polygon(Point(0, 0), Point(6, 0), Point(3, sqrt(27)))
|
644 |
+
assert p3.is_isosceles() == True
|
645 |
+
assert p3.is_scalene() == False
|
646 |
+
assert p3.is_equilateral() == True
|
647 |
+
|
648 |
+
def test_do_poly_distance():
|
649 |
+
# Non-intersecting polygons
|
650 |
+
square1 = Polygon (Point(0, 0), Point(0, 1), Point(1, 1), Point(1, 0))
|
651 |
+
triangle1 = Polygon(Point(1, 2), Point(2, 2), Point(2, 1))
|
652 |
+
assert square1._do_poly_distance(triangle1) == sqrt(2)/2
|
653 |
+
|
654 |
+
# Polygons which sides intersect
|
655 |
+
square2 = Polygon(Point(1, 0), Point(2, 0), Point(2, 1), Point(1, 1))
|
656 |
+
with warns(UserWarning, \
|
657 |
+
match="Polygons may intersect producing erroneous output", test_stacklevel=False):
|
658 |
+
assert square1._do_poly_distance(square2) == 0
|
659 |
+
|
660 |
+
# Polygons which bodies intersect
|
661 |
+
triangle2 = Polygon(Point(0, -1), Point(2, -1), Point(S.Half, S.Half))
|
662 |
+
with warns(UserWarning, \
|
663 |
+
match="Polygons may intersect producing erroneous output", test_stacklevel=False):
|
664 |
+
assert triangle2._do_poly_distance(square1) == 0
|
env-llmeval/lib/python3.10/site-packages/sympy/geometry/tests/test_util.py
ADDED
@@ -0,0 +1,151 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from sympy.core.function import (Derivative, Function)
|
2 |
+
from sympy.core.singleton import S
|
3 |
+
from sympy.core.symbol import Symbol
|
4 |
+
from sympy.functions import exp, cos, sin, tan, cosh, sinh
|
5 |
+
from sympy.functions.elementary.miscellaneous import sqrt
|
6 |
+
from sympy.geometry import Point, Point2D, Line, Polygon, Segment, convex_hull,\
|
7 |
+
intersection, centroid, Point3D, Line3D
|
8 |
+
from sympy.geometry.util import idiff, closest_points, farthest_points, _ordered_points, are_coplanar
|
9 |
+
from sympy.solvers.solvers import solve
|
10 |
+
from sympy.testing.pytest import raises
|
11 |
+
|
12 |
+
|
13 |
+
def test_idiff():
|
14 |
+
x = Symbol('x', real=True)
|
15 |
+
y = Symbol('y', real=True)
|
16 |
+
t = Symbol('t', real=True)
|
17 |
+
f = Function('f')
|
18 |
+
g = Function('g')
|
19 |
+
# the use of idiff in ellipse also provides coverage
|
20 |
+
circ = x**2 + y**2 - 4
|
21 |
+
ans = -3*x*(x**2/y**2 + 1)/y**3
|
22 |
+
assert ans == idiff(circ, y, x, 3), idiff(circ, y, x, 3)
|
23 |
+
assert ans == idiff(circ, [y], x, 3)
|
24 |
+
assert idiff(circ, y, x, 3) == ans
|
25 |
+
explicit = 12*x/sqrt(-x**2 + 4)**5
|
26 |
+
assert ans.subs(y, solve(circ, y)[0]).equals(explicit)
|
27 |
+
assert True in [sol.diff(x, 3).equals(explicit) for sol in solve(circ, y)]
|
28 |
+
assert idiff(x + t + y, [y, t], x) == -Derivative(t, x) - 1
|
29 |
+
assert idiff(f(x) * exp(f(x)) - x * exp(x), f(x), x) == (x + 1)*exp(x)*exp(-f(x))/(f(x) + 1)
|
30 |
+
assert idiff(f(x) - y * exp(x), [f(x), y], x) == (y + Derivative(y, x))*exp(x)
|
31 |
+
assert idiff(f(x) - y * exp(x), [y, f(x)], x) == -y + Derivative(f(x), x)*exp(-x)
|
32 |
+
assert idiff(f(x) - g(x), [f(x), g(x)], x) == Derivative(g(x), x)
|
33 |
+
# this should be fast
|
34 |
+
fxy = y - (-10*(-sin(x) + 1/x)**2 + tan(x)**2 + 2*cosh(x/10))
|
35 |
+
assert idiff(fxy, y, x) == -20*sin(x)*cos(x) + 2*tan(x)**3 + \
|
36 |
+
2*tan(x) + sinh(x/10)/5 + 20*cos(x)/x - 20*sin(x)/x**2 + 20/x**3
|
37 |
+
|
38 |
+
|
39 |
+
def test_intersection():
|
40 |
+
assert intersection(Point(0, 0)) == []
|
41 |
+
raises(TypeError, lambda: intersection(Point(0, 0), 3))
|
42 |
+
assert intersection(
|
43 |
+
Segment((0, 0), (2, 0)),
|
44 |
+
Segment((-1, 0), (1, 0)),
|
45 |
+
Line((0, 0), (0, 1)), pairwise=True) == [
|
46 |
+
Point(0, 0), Segment((0, 0), (1, 0))]
|
47 |
+
assert intersection(
|
48 |
+
Line((0, 0), (0, 1)),
|
49 |
+
Segment((0, 0), (2, 0)),
|
50 |
+
Segment((-1, 0), (1, 0)), pairwise=True) == [
|
51 |
+
Point(0, 0), Segment((0, 0), (1, 0))]
|
52 |
+
assert intersection(
|
53 |
+
Line((0, 0), (0, 1)),
|
54 |
+
Segment((0, 0), (2, 0)),
|
55 |
+
Segment((-1, 0), (1, 0)),
|
56 |
+
Line((0, 0), slope=1), pairwise=True) == [
|
57 |
+
Point(0, 0), Segment((0, 0), (1, 0))]
|
58 |
+
|
59 |
+
|
60 |
+
def test_convex_hull():
|
61 |
+
raises(TypeError, lambda: convex_hull(Point(0, 0), 3))
|
62 |
+
points = [(1, -1), (1, -2), (3, -1), (-5, -2), (15, -4)]
|
63 |
+
assert convex_hull(*points, **{"polygon": False}) == (
|
64 |
+
[Point2D(-5, -2), Point2D(1, -1), Point2D(3, -1), Point2D(15, -4)],
|
65 |
+
[Point2D(-5, -2), Point2D(15, -4)])
|
66 |
+
|
67 |
+
|
68 |
+
def test_centroid():
|
69 |
+
p = Polygon((0, 0), (10, 0), (10, 10))
|
70 |
+
q = p.translate(0, 20)
|
71 |
+
assert centroid(p, q) == Point(20, 40)/3
|
72 |
+
p = Segment((0, 0), (2, 0))
|
73 |
+
q = Segment((0, 0), (2, 2))
|
74 |
+
assert centroid(p, q) == Point(1, -sqrt(2) + 2)
|
75 |
+
assert centroid(Point(0, 0), Point(2, 0)) == Point(2, 0)/2
|
76 |
+
assert centroid(Point(0, 0), Point(0, 0), Point(2, 0)) == Point(2, 0)/3
|
77 |
+
|
78 |
+
|
79 |
+
def test_farthest_points_closest_points():
|
80 |
+
from sympy.core.random import randint
|
81 |
+
from sympy.utilities.iterables import subsets
|
82 |
+
|
83 |
+
for how in (min, max):
|
84 |
+
if how == min:
|
85 |
+
func = closest_points
|
86 |
+
else:
|
87 |
+
func = farthest_points
|
88 |
+
|
89 |
+
raises(ValueError, lambda: func(Point2D(0, 0), Point2D(0, 0)))
|
90 |
+
|
91 |
+
# 3rd pt dx is close and pt is closer to 1st pt
|
92 |
+
p1 = [Point2D(0, 0), Point2D(3, 0), Point2D(1, 1)]
|
93 |
+
# 3rd pt dx is close and pt is closer to 2nd pt
|
94 |
+
p2 = [Point2D(0, 0), Point2D(3, 0), Point2D(2, 1)]
|
95 |
+
# 3rd pt dx is close and but pt is not closer
|
96 |
+
p3 = [Point2D(0, 0), Point2D(3, 0), Point2D(1, 10)]
|
97 |
+
# 3rd pt dx is not closer and it's closer to 2nd pt
|
98 |
+
p4 = [Point2D(0, 0), Point2D(3, 0), Point2D(4, 0)]
|
99 |
+
# 3rd pt dx is not closer and it's closer to 1st pt
|
100 |
+
p5 = [Point2D(0, 0), Point2D(3, 0), Point2D(-1, 0)]
|
101 |
+
# duplicate point doesn't affect outcome
|
102 |
+
dup = [Point2D(0, 0), Point2D(3, 0), Point2D(3, 0), Point2D(-1, 0)]
|
103 |
+
# symbolic
|
104 |
+
x = Symbol('x', positive=True)
|
105 |
+
s = [Point2D(a) for a in ((x, 1), (x + 3, 2), (x + 2, 2))]
|
106 |
+
|
107 |
+
for points in (p1, p2, p3, p4, p5, dup, s):
|
108 |
+
d = how(i.distance(j) for i, j in subsets(set(points), 2))
|
109 |
+
ans = a, b = list(func(*points))[0]
|
110 |
+
assert a.distance(b) == d
|
111 |
+
assert ans == _ordered_points(ans)
|
112 |
+
|
113 |
+
# if the following ever fails, the above tests were not sufficient
|
114 |
+
# and the logical error in the routine should be fixed
|
115 |
+
points = set()
|
116 |
+
while len(points) != 7:
|
117 |
+
points.add(Point2D(randint(1, 100), randint(1, 100)))
|
118 |
+
points = list(points)
|
119 |
+
d = how(i.distance(j) for i, j in subsets(points, 2))
|
120 |
+
ans = a, b = list(func(*points))[0]
|
121 |
+
assert a.distance(b) == d
|
122 |
+
assert ans == _ordered_points(ans)
|
123 |
+
|
124 |
+
# equidistant points
|
125 |
+
a, b, c = (
|
126 |
+
Point2D(0, 0), Point2D(1, 0), Point2D(S.Half, sqrt(3)/2))
|
127 |
+
ans = {_ordered_points((i, j))
|
128 |
+
for i, j in subsets((a, b, c), 2)}
|
129 |
+
assert closest_points(b, c, a) == ans
|
130 |
+
assert farthest_points(b, c, a) == ans
|
131 |
+
|
132 |
+
# unique to farthest
|
133 |
+
points = [(1, 1), (1, 2), (3, 1), (-5, 2), (15, 4)]
|
134 |
+
assert farthest_points(*points) == {
|
135 |
+
(Point2D(-5, 2), Point2D(15, 4))}
|
136 |
+
points = [(1, -1), (1, -2), (3, -1), (-5, -2), (15, -4)]
|
137 |
+
assert farthest_points(*points) == {
|
138 |
+
(Point2D(-5, -2), Point2D(15, -4))}
|
139 |
+
assert farthest_points((1, 1), (0, 0)) == {
|
140 |
+
(Point2D(0, 0), Point2D(1, 1))}
|
141 |
+
raises(ValueError, lambda: farthest_points((1, 1)))
|
142 |
+
|
143 |
+
|
144 |
+
def test_are_coplanar():
|
145 |
+
a = Line3D(Point3D(5, 0, 0), Point3D(1, -1, 1))
|
146 |
+
b = Line3D(Point3D(0, -2, 0), Point3D(3, 1, 1))
|
147 |
+
c = Line3D(Point3D(0, -1, 0), Point3D(5, -1, 9))
|
148 |
+
d = Line(Point2D(0, 3), Point2D(1, 5))
|
149 |
+
|
150 |
+
assert are_coplanar(a, b, c) == False
|
151 |
+
assert are_coplanar(a, d) == False
|
env-llmeval/lib/python3.10/site-packages/sympy/geometry/util.py
ADDED
@@ -0,0 +1,718 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""Utility functions for geometrical entities.
|
2 |
+
|
3 |
+
Contains
|
4 |
+
========
|
5 |
+
intersection
|
6 |
+
convex_hull
|
7 |
+
closest_points
|
8 |
+
farthest_points
|
9 |
+
are_coplanar
|
10 |
+
are_similar
|
11 |
+
|
12 |
+
"""
|
13 |
+
|
14 |
+
from collections import deque
|
15 |
+
from math import sqrt as _sqrt
|
16 |
+
|
17 |
+
|
18 |
+
from .entity import GeometryEntity
|
19 |
+
from .exceptions import GeometryError
|
20 |
+
from .point import Point, Point2D, Point3D
|
21 |
+
from sympy.core.containers import OrderedSet
|
22 |
+
from sympy.core.exprtools import factor_terms
|
23 |
+
from sympy.core.function import Function, expand_mul
|
24 |
+
from sympy.core.sorting import ordered
|
25 |
+
from sympy.core.symbol import Symbol
|
26 |
+
from sympy.core.singleton import S
|
27 |
+
from sympy.polys.polytools import cancel
|
28 |
+
from sympy.functions.elementary.miscellaneous import sqrt
|
29 |
+
from sympy.utilities.iterables import is_sequence
|
30 |
+
|
31 |
+
|
32 |
+
def find(x, equation):
|
33 |
+
"""
|
34 |
+
Checks whether a Symbol matching ``x`` is present in ``equation``
|
35 |
+
or not. If present, the matching symbol is returned, else a
|
36 |
+
ValueError is raised. If ``x`` is a string the matching symbol
|
37 |
+
will have the same name; if ``x`` is a Symbol then it will be
|
38 |
+
returned if found.
|
39 |
+
|
40 |
+
Examples
|
41 |
+
========
|
42 |
+
|
43 |
+
>>> from sympy.geometry.util import find
|
44 |
+
>>> from sympy import Dummy
|
45 |
+
>>> from sympy.abc import x
|
46 |
+
>>> find('x', x)
|
47 |
+
x
|
48 |
+
>>> find('x', Dummy('x'))
|
49 |
+
_x
|
50 |
+
|
51 |
+
The dummy symbol is returned since it has a matching name:
|
52 |
+
|
53 |
+
>>> _.name == 'x'
|
54 |
+
True
|
55 |
+
>>> find(x, Dummy('x'))
|
56 |
+
Traceback (most recent call last):
|
57 |
+
...
|
58 |
+
ValueError: could not find x
|
59 |
+
"""
|
60 |
+
|
61 |
+
free = equation.free_symbols
|
62 |
+
xs = [i for i in free if (i.name if isinstance(x, str) else i) == x]
|
63 |
+
if not xs:
|
64 |
+
raise ValueError('could not find %s' % x)
|
65 |
+
if len(xs) != 1:
|
66 |
+
raise ValueError('ambiguous %s' % x)
|
67 |
+
return xs[0]
|
68 |
+
|
69 |
+
|
70 |
+
def _ordered_points(p):
|
71 |
+
"""Return the tuple of points sorted numerically according to args"""
|
72 |
+
return tuple(sorted(p, key=lambda x: x.args))
|
73 |
+
|
74 |
+
|
75 |
+
def are_coplanar(*e):
|
76 |
+
""" Returns True if the given entities are coplanar otherwise False
|
77 |
+
|
78 |
+
Parameters
|
79 |
+
==========
|
80 |
+
|
81 |
+
e: entities to be checked for being coplanar
|
82 |
+
|
83 |
+
Returns
|
84 |
+
=======
|
85 |
+
|
86 |
+
Boolean
|
87 |
+
|
88 |
+
Examples
|
89 |
+
========
|
90 |
+
|
91 |
+
>>> from sympy import Point3D, Line3D
|
92 |
+
>>> from sympy.geometry.util import are_coplanar
|
93 |
+
>>> a = Line3D(Point3D(5, 0, 0), Point3D(1, -1, 1))
|
94 |
+
>>> b = Line3D(Point3D(0, -2, 0), Point3D(3, 1, 1))
|
95 |
+
>>> c = Line3D(Point3D(0, -1, 0), Point3D(5, -1, 9))
|
96 |
+
>>> are_coplanar(a, b, c)
|
97 |
+
False
|
98 |
+
|
99 |
+
"""
|
100 |
+
from .line import LinearEntity3D
|
101 |
+
from .plane import Plane
|
102 |
+
# XXX update tests for coverage
|
103 |
+
|
104 |
+
e = set(e)
|
105 |
+
# first work with a Plane if present
|
106 |
+
for i in list(e):
|
107 |
+
if isinstance(i, Plane):
|
108 |
+
e.remove(i)
|
109 |
+
return all(p.is_coplanar(i) for p in e)
|
110 |
+
|
111 |
+
if all(isinstance(i, Point3D) for i in e):
|
112 |
+
if len(e) < 3:
|
113 |
+
return False
|
114 |
+
|
115 |
+
# remove pts that are collinear with 2 pts
|
116 |
+
a, b = e.pop(), e.pop()
|
117 |
+
for i in list(e):
|
118 |
+
if Point3D.are_collinear(a, b, i):
|
119 |
+
e.remove(i)
|
120 |
+
|
121 |
+
if not e:
|
122 |
+
return False
|
123 |
+
else:
|
124 |
+
# define a plane
|
125 |
+
p = Plane(a, b, e.pop())
|
126 |
+
for i in e:
|
127 |
+
if i not in p:
|
128 |
+
return False
|
129 |
+
return True
|
130 |
+
else:
|
131 |
+
pt3d = []
|
132 |
+
for i in e:
|
133 |
+
if isinstance(i, Point3D):
|
134 |
+
pt3d.append(i)
|
135 |
+
elif isinstance(i, LinearEntity3D):
|
136 |
+
pt3d.extend(i.args)
|
137 |
+
elif isinstance(i, GeometryEntity): # XXX we should have a GeometryEntity3D class so we can tell the difference between 2D and 3D -- here we just want to deal with 2D objects; if new 3D objects are encountered that we didn't handle above, an error should be raised
|
138 |
+
# all 2D objects have some Point that defines them; so convert those points to 3D pts by making z=0
|
139 |
+
for p in i.args:
|
140 |
+
if isinstance(p, Point):
|
141 |
+
pt3d.append(Point3D(*(p.args + (0,))))
|
142 |
+
return are_coplanar(*pt3d)
|
143 |
+
|
144 |
+
|
145 |
+
def are_similar(e1, e2):
|
146 |
+
"""Are two geometrical entities similar.
|
147 |
+
|
148 |
+
Can one geometrical entity be uniformly scaled to the other?
|
149 |
+
|
150 |
+
Parameters
|
151 |
+
==========
|
152 |
+
|
153 |
+
e1 : GeometryEntity
|
154 |
+
e2 : GeometryEntity
|
155 |
+
|
156 |
+
Returns
|
157 |
+
=======
|
158 |
+
|
159 |
+
are_similar : boolean
|
160 |
+
|
161 |
+
Raises
|
162 |
+
======
|
163 |
+
|
164 |
+
GeometryError
|
165 |
+
When `e1` and `e2` cannot be compared.
|
166 |
+
|
167 |
+
Notes
|
168 |
+
=====
|
169 |
+
|
170 |
+
If the two objects are equal then they are similar.
|
171 |
+
|
172 |
+
See Also
|
173 |
+
========
|
174 |
+
|
175 |
+
sympy.geometry.entity.GeometryEntity.is_similar
|
176 |
+
|
177 |
+
Examples
|
178 |
+
========
|
179 |
+
|
180 |
+
>>> from sympy import Point, Circle, Triangle, are_similar
|
181 |
+
>>> c1, c2 = Circle(Point(0, 0), 4), Circle(Point(1, 4), 3)
|
182 |
+
>>> t1 = Triangle(Point(0, 0), Point(1, 0), Point(0, 1))
|
183 |
+
>>> t2 = Triangle(Point(0, 0), Point(2, 0), Point(0, 2))
|
184 |
+
>>> t3 = Triangle(Point(0, 0), Point(3, 0), Point(0, 1))
|
185 |
+
>>> are_similar(t1, t2)
|
186 |
+
True
|
187 |
+
>>> are_similar(t1, t3)
|
188 |
+
False
|
189 |
+
|
190 |
+
"""
|
191 |
+
if e1 == e2:
|
192 |
+
return True
|
193 |
+
is_similar1 = getattr(e1, 'is_similar', None)
|
194 |
+
if is_similar1:
|
195 |
+
return is_similar1(e2)
|
196 |
+
is_similar2 = getattr(e2, 'is_similar', None)
|
197 |
+
if is_similar2:
|
198 |
+
return is_similar2(e1)
|
199 |
+
n1 = e1.__class__.__name__
|
200 |
+
n2 = e2.__class__.__name__
|
201 |
+
raise GeometryError(
|
202 |
+
"Cannot test similarity between %s and %s" % (n1, n2))
|
203 |
+
|
204 |
+
|
205 |
+
def centroid(*args):
|
206 |
+
"""Find the centroid (center of mass) of the collection containing only Points,
|
207 |
+
Segments or Polygons. The centroid is the weighted average of the individual centroid
|
208 |
+
where the weights are the lengths (of segments) or areas (of polygons).
|
209 |
+
Overlapping regions will add to the weight of that region.
|
210 |
+
|
211 |
+
If there are no objects (or a mixture of objects) then None is returned.
|
212 |
+
|
213 |
+
See Also
|
214 |
+
========
|
215 |
+
|
216 |
+
sympy.geometry.point.Point, sympy.geometry.line.Segment,
|
217 |
+
sympy.geometry.polygon.Polygon
|
218 |
+
|
219 |
+
Examples
|
220 |
+
========
|
221 |
+
|
222 |
+
>>> from sympy import Point, Segment, Polygon
|
223 |
+
>>> from sympy.geometry.util import centroid
|
224 |
+
>>> p = Polygon((0, 0), (10, 0), (10, 10))
|
225 |
+
>>> q = p.translate(0, 20)
|
226 |
+
>>> p.centroid, q.centroid
|
227 |
+
(Point2D(20/3, 10/3), Point2D(20/3, 70/3))
|
228 |
+
>>> centroid(p, q)
|
229 |
+
Point2D(20/3, 40/3)
|
230 |
+
>>> p, q = Segment((0, 0), (2, 0)), Segment((0, 0), (2, 2))
|
231 |
+
>>> centroid(p, q)
|
232 |
+
Point2D(1, 2 - sqrt(2))
|
233 |
+
>>> centroid(Point(0, 0), Point(2, 0))
|
234 |
+
Point2D(1, 0)
|
235 |
+
|
236 |
+
Stacking 3 polygons on top of each other effectively triples the
|
237 |
+
weight of that polygon:
|
238 |
+
|
239 |
+
>>> p = Polygon((0, 0), (1, 0), (1, 1), (0, 1))
|
240 |
+
>>> q = Polygon((1, 0), (3, 0), (3, 1), (1, 1))
|
241 |
+
>>> centroid(p, q)
|
242 |
+
Point2D(3/2, 1/2)
|
243 |
+
>>> centroid(p, p, p, q) # centroid x-coord shifts left
|
244 |
+
Point2D(11/10, 1/2)
|
245 |
+
|
246 |
+
Stacking the squares vertically above and below p has the same
|
247 |
+
effect:
|
248 |
+
|
249 |
+
>>> centroid(p, p.translate(0, 1), p.translate(0, -1), q)
|
250 |
+
Point2D(11/10, 1/2)
|
251 |
+
|
252 |
+
"""
|
253 |
+
from .line import Segment
|
254 |
+
from .polygon import Polygon
|
255 |
+
if args:
|
256 |
+
if all(isinstance(g, Point) for g in args):
|
257 |
+
c = Point(0, 0)
|
258 |
+
for g in args:
|
259 |
+
c += g
|
260 |
+
den = len(args)
|
261 |
+
elif all(isinstance(g, Segment) for g in args):
|
262 |
+
c = Point(0, 0)
|
263 |
+
L = 0
|
264 |
+
for g in args:
|
265 |
+
l = g.length
|
266 |
+
c += g.midpoint*l
|
267 |
+
L += l
|
268 |
+
den = L
|
269 |
+
elif all(isinstance(g, Polygon) for g in args):
|
270 |
+
c = Point(0, 0)
|
271 |
+
A = 0
|
272 |
+
for g in args:
|
273 |
+
a = g.area
|
274 |
+
c += g.centroid*a
|
275 |
+
A += a
|
276 |
+
den = A
|
277 |
+
c /= den
|
278 |
+
return c.func(*[i.simplify() for i in c.args])
|
279 |
+
|
280 |
+
|
281 |
+
def closest_points(*args):
|
282 |
+
"""Return the subset of points from a set of points that were
|
283 |
+
the closest to each other in the 2D plane.
|
284 |
+
|
285 |
+
Parameters
|
286 |
+
==========
|
287 |
+
|
288 |
+
args
|
289 |
+
A collection of Points on 2D plane.
|
290 |
+
|
291 |
+
Notes
|
292 |
+
=====
|
293 |
+
|
294 |
+
This can only be performed on a set of points whose coordinates can
|
295 |
+
be ordered on the number line. If there are no ties then a single
|
296 |
+
pair of Points will be in the set.
|
297 |
+
|
298 |
+
Examples
|
299 |
+
========
|
300 |
+
|
301 |
+
>>> from sympy import closest_points, Triangle
|
302 |
+
>>> Triangle(sss=(3, 4, 5)).args
|
303 |
+
(Point2D(0, 0), Point2D(3, 0), Point2D(3, 4))
|
304 |
+
>>> closest_points(*_)
|
305 |
+
{(Point2D(0, 0), Point2D(3, 0))}
|
306 |
+
|
307 |
+
References
|
308 |
+
==========
|
309 |
+
|
310 |
+
.. [1] https://www.cs.mcgill.ca/~cs251/ClosestPair/ClosestPairPS.html
|
311 |
+
|
312 |
+
.. [2] Sweep line algorithm
|
313 |
+
https://en.wikipedia.org/wiki/Sweep_line_algorithm
|
314 |
+
|
315 |
+
"""
|
316 |
+
p = [Point2D(i) for i in set(args)]
|
317 |
+
if len(p) < 2:
|
318 |
+
raise ValueError('At least 2 distinct points must be given.')
|
319 |
+
|
320 |
+
try:
|
321 |
+
p.sort(key=lambda x: x.args)
|
322 |
+
except TypeError:
|
323 |
+
raise ValueError("The points could not be sorted.")
|
324 |
+
|
325 |
+
if not all(i.is_Rational for j in p for i in j.args):
|
326 |
+
def hypot(x, y):
|
327 |
+
arg = x*x + y*y
|
328 |
+
if arg.is_Rational:
|
329 |
+
return _sqrt(arg)
|
330 |
+
return sqrt(arg)
|
331 |
+
else:
|
332 |
+
from math import hypot
|
333 |
+
|
334 |
+
rv = [(0, 1)]
|
335 |
+
best_dist = hypot(p[1].x - p[0].x, p[1].y - p[0].y)
|
336 |
+
i = 2
|
337 |
+
left = 0
|
338 |
+
box = deque([0, 1])
|
339 |
+
while i < len(p):
|
340 |
+
while left < i and p[i][0] - p[left][0] > best_dist:
|
341 |
+
box.popleft()
|
342 |
+
left += 1
|
343 |
+
|
344 |
+
for j in box:
|
345 |
+
d = hypot(p[i].x - p[j].x, p[i].y - p[j].y)
|
346 |
+
if d < best_dist:
|
347 |
+
rv = [(j, i)]
|
348 |
+
elif d == best_dist:
|
349 |
+
rv.append((j, i))
|
350 |
+
else:
|
351 |
+
continue
|
352 |
+
best_dist = d
|
353 |
+
box.append(i)
|
354 |
+
i += 1
|
355 |
+
|
356 |
+
return {tuple([p[i] for i in pair]) for pair in rv}
|
357 |
+
|
358 |
+
|
359 |
+
def convex_hull(*args, polygon=True):
|
360 |
+
"""The convex hull surrounding the Points contained in the list of entities.
|
361 |
+
|
362 |
+
Parameters
|
363 |
+
==========
|
364 |
+
|
365 |
+
args : a collection of Points, Segments and/or Polygons
|
366 |
+
|
367 |
+
Optional parameters
|
368 |
+
===================
|
369 |
+
|
370 |
+
polygon : Boolean. If True, returns a Polygon, if false a tuple, see below.
|
371 |
+
Default is True.
|
372 |
+
|
373 |
+
Returns
|
374 |
+
=======
|
375 |
+
|
376 |
+
convex_hull : Polygon if ``polygon`` is True else as a tuple `(U, L)` where
|
377 |
+
``L`` and ``U`` are the lower and upper hulls, respectively.
|
378 |
+
|
379 |
+
Notes
|
380 |
+
=====
|
381 |
+
|
382 |
+
This can only be performed on a set of points whose coordinates can
|
383 |
+
be ordered on the number line.
|
384 |
+
|
385 |
+
See Also
|
386 |
+
========
|
387 |
+
|
388 |
+
sympy.geometry.point.Point, sympy.geometry.polygon.Polygon
|
389 |
+
|
390 |
+
Examples
|
391 |
+
========
|
392 |
+
|
393 |
+
>>> from sympy import convex_hull
|
394 |
+
>>> points = [(1, 1), (1, 2), (3, 1), (-5, 2), (15, 4)]
|
395 |
+
>>> convex_hull(*points)
|
396 |
+
Polygon(Point2D(-5, 2), Point2D(1, 1), Point2D(3, 1), Point2D(15, 4))
|
397 |
+
>>> convex_hull(*points, **dict(polygon=False))
|
398 |
+
([Point2D(-5, 2), Point2D(15, 4)],
|
399 |
+
[Point2D(-5, 2), Point2D(1, 1), Point2D(3, 1), Point2D(15, 4)])
|
400 |
+
|
401 |
+
References
|
402 |
+
==========
|
403 |
+
|
404 |
+
.. [1] https://en.wikipedia.org/wiki/Graham_scan
|
405 |
+
|
406 |
+
.. [2] Andrew's Monotone Chain Algorithm
|
407 |
+
(A.M. Andrew,
|
408 |
+
"Another Efficient Algorithm for Convex Hulls in Two Dimensions", 1979)
|
409 |
+
https://web.archive.org/web/20210511015444/http://geomalgorithms.com/a10-_hull-1.html
|
410 |
+
|
411 |
+
"""
|
412 |
+
from .line import Segment
|
413 |
+
from .polygon import Polygon
|
414 |
+
p = OrderedSet()
|
415 |
+
for e in args:
|
416 |
+
if not isinstance(e, GeometryEntity):
|
417 |
+
try:
|
418 |
+
e = Point(e)
|
419 |
+
except NotImplementedError:
|
420 |
+
raise ValueError('%s is not a GeometryEntity and cannot be made into Point' % str(e))
|
421 |
+
if isinstance(e, Point):
|
422 |
+
p.add(e)
|
423 |
+
elif isinstance(e, Segment):
|
424 |
+
p.update(e.points)
|
425 |
+
elif isinstance(e, Polygon):
|
426 |
+
p.update(e.vertices)
|
427 |
+
else:
|
428 |
+
raise NotImplementedError(
|
429 |
+
'Convex hull for %s not implemented.' % type(e))
|
430 |
+
|
431 |
+
# make sure all our points are of the same dimension
|
432 |
+
if any(len(x) != 2 for x in p):
|
433 |
+
raise ValueError('Can only compute the convex hull in two dimensions')
|
434 |
+
|
435 |
+
p = list(p)
|
436 |
+
if len(p) == 1:
|
437 |
+
return p[0] if polygon else (p[0], None)
|
438 |
+
elif len(p) == 2:
|
439 |
+
s = Segment(p[0], p[1])
|
440 |
+
return s if polygon else (s, None)
|
441 |
+
|
442 |
+
def _orientation(p, q, r):
|
443 |
+
'''Return positive if p-q-r are clockwise, neg if ccw, zero if
|
444 |
+
collinear.'''
|
445 |
+
return (q.y - p.y)*(r.x - p.x) - (q.x - p.x)*(r.y - p.y)
|
446 |
+
|
447 |
+
# scan to find upper and lower convex hulls of a set of 2d points.
|
448 |
+
U = []
|
449 |
+
L = []
|
450 |
+
try:
|
451 |
+
p.sort(key=lambda x: x.args)
|
452 |
+
except TypeError:
|
453 |
+
raise ValueError("The points could not be sorted.")
|
454 |
+
for p_i in p:
|
455 |
+
while len(U) > 1 and _orientation(U[-2], U[-1], p_i) <= 0:
|
456 |
+
U.pop()
|
457 |
+
while len(L) > 1 and _orientation(L[-2], L[-1], p_i) >= 0:
|
458 |
+
L.pop()
|
459 |
+
U.append(p_i)
|
460 |
+
L.append(p_i)
|
461 |
+
U.reverse()
|
462 |
+
convexHull = tuple(L + U[1:-1])
|
463 |
+
|
464 |
+
if len(convexHull) == 2:
|
465 |
+
s = Segment(convexHull[0], convexHull[1])
|
466 |
+
return s if polygon else (s, None)
|
467 |
+
if polygon:
|
468 |
+
return Polygon(*convexHull)
|
469 |
+
else:
|
470 |
+
U.reverse()
|
471 |
+
return (U, L)
|
472 |
+
|
473 |
+
def farthest_points(*args):
|
474 |
+
"""Return the subset of points from a set of points that were
|
475 |
+
the furthest apart from each other in the 2D plane.
|
476 |
+
|
477 |
+
Parameters
|
478 |
+
==========
|
479 |
+
|
480 |
+
args
|
481 |
+
A collection of Points on 2D plane.
|
482 |
+
|
483 |
+
Notes
|
484 |
+
=====
|
485 |
+
|
486 |
+
This can only be performed on a set of points whose coordinates can
|
487 |
+
be ordered on the number line. If there are no ties then a single
|
488 |
+
pair of Points will be in the set.
|
489 |
+
|
490 |
+
Examples
|
491 |
+
========
|
492 |
+
|
493 |
+
>>> from sympy.geometry import farthest_points, Triangle
|
494 |
+
>>> Triangle(sss=(3, 4, 5)).args
|
495 |
+
(Point2D(0, 0), Point2D(3, 0), Point2D(3, 4))
|
496 |
+
>>> farthest_points(*_)
|
497 |
+
{(Point2D(0, 0), Point2D(3, 4))}
|
498 |
+
|
499 |
+
References
|
500 |
+
==========
|
501 |
+
|
502 |
+
.. [1] https://code.activestate.com/recipes/117225-convex-hull-and-diameter-of-2d-point-sets/
|
503 |
+
|
504 |
+
.. [2] Rotating Callipers Technique
|
505 |
+
https://en.wikipedia.org/wiki/Rotating_calipers
|
506 |
+
|
507 |
+
"""
|
508 |
+
|
509 |
+
def rotatingCalipers(Points):
|
510 |
+
U, L = convex_hull(*Points, **{"polygon": False})
|
511 |
+
|
512 |
+
if L is None:
|
513 |
+
if isinstance(U, Point):
|
514 |
+
raise ValueError('At least two distinct points must be given.')
|
515 |
+
yield U.args
|
516 |
+
else:
|
517 |
+
i = 0
|
518 |
+
j = len(L) - 1
|
519 |
+
while i < len(U) - 1 or j > 0:
|
520 |
+
yield U[i], L[j]
|
521 |
+
# if all the way through one side of hull, advance the other side
|
522 |
+
if i == len(U) - 1:
|
523 |
+
j -= 1
|
524 |
+
elif j == 0:
|
525 |
+
i += 1
|
526 |
+
# still points left on both lists, compare slopes of next hull edges
|
527 |
+
# being careful to avoid divide-by-zero in slope calculation
|
528 |
+
elif (U[i+1].y - U[i].y) * (L[j].x - L[j-1].x) > \
|
529 |
+
(L[j].y - L[j-1].y) * (U[i+1].x - U[i].x):
|
530 |
+
i += 1
|
531 |
+
else:
|
532 |
+
j -= 1
|
533 |
+
|
534 |
+
p = [Point2D(i) for i in set(args)]
|
535 |
+
|
536 |
+
if not all(i.is_Rational for j in p for i in j.args):
|
537 |
+
def hypot(x, y):
|
538 |
+
arg = x*x + y*y
|
539 |
+
if arg.is_Rational:
|
540 |
+
return _sqrt(arg)
|
541 |
+
return sqrt(arg)
|
542 |
+
else:
|
543 |
+
from math import hypot
|
544 |
+
|
545 |
+
rv = []
|
546 |
+
diam = 0
|
547 |
+
for pair in rotatingCalipers(args):
|
548 |
+
h, q = _ordered_points(pair)
|
549 |
+
d = hypot(h.x - q.x, h.y - q.y)
|
550 |
+
if d > diam:
|
551 |
+
rv = [(h, q)]
|
552 |
+
elif d == diam:
|
553 |
+
rv.append((h, q))
|
554 |
+
else:
|
555 |
+
continue
|
556 |
+
diam = d
|
557 |
+
|
558 |
+
return set(rv)
|
559 |
+
|
560 |
+
|
561 |
+
def idiff(eq, y, x, n=1):
|
562 |
+
"""Return ``dy/dx`` assuming that ``eq == 0``.
|
563 |
+
|
564 |
+
Parameters
|
565 |
+
==========
|
566 |
+
|
567 |
+
y : the dependent variable or a list of dependent variables (with y first)
|
568 |
+
x : the variable that the derivative is being taken with respect to
|
569 |
+
n : the order of the derivative (default is 1)
|
570 |
+
|
571 |
+
Examples
|
572 |
+
========
|
573 |
+
|
574 |
+
>>> from sympy.abc import x, y, a
|
575 |
+
>>> from sympy.geometry.util import idiff
|
576 |
+
|
577 |
+
>>> circ = x**2 + y**2 - 4
|
578 |
+
>>> idiff(circ, y, x)
|
579 |
+
-x/y
|
580 |
+
>>> idiff(circ, y, x, 2).simplify()
|
581 |
+
(-x**2 - y**2)/y**3
|
582 |
+
|
583 |
+
Here, ``a`` is assumed to be independent of ``x``:
|
584 |
+
|
585 |
+
>>> idiff(x + a + y, y, x)
|
586 |
+
-1
|
587 |
+
|
588 |
+
Now the x-dependence of ``a`` is made explicit by listing ``a`` after
|
589 |
+
``y`` in a list.
|
590 |
+
|
591 |
+
>>> idiff(x + a + y, [y, a], x)
|
592 |
+
-Derivative(a, x) - 1
|
593 |
+
|
594 |
+
See Also
|
595 |
+
========
|
596 |
+
|
597 |
+
sympy.core.function.Derivative: represents unevaluated derivatives
|
598 |
+
sympy.core.function.diff: explicitly differentiates wrt symbols
|
599 |
+
|
600 |
+
"""
|
601 |
+
if is_sequence(y):
|
602 |
+
dep = set(y)
|
603 |
+
y = y[0]
|
604 |
+
elif isinstance(y, Symbol):
|
605 |
+
dep = {y}
|
606 |
+
elif isinstance(y, Function):
|
607 |
+
pass
|
608 |
+
else:
|
609 |
+
raise ValueError("expecting x-dependent symbol(s) or function(s) but got: %s" % y)
|
610 |
+
|
611 |
+
f = {s: Function(s.name)(x) for s in eq.free_symbols
|
612 |
+
if s != x and s in dep}
|
613 |
+
|
614 |
+
if isinstance(y, Symbol):
|
615 |
+
dydx = Function(y.name)(x).diff(x)
|
616 |
+
else:
|
617 |
+
dydx = y.diff(x)
|
618 |
+
|
619 |
+
eq = eq.subs(f)
|
620 |
+
derivs = {}
|
621 |
+
for i in range(n):
|
622 |
+
# equation will be linear in dydx, a*dydx + b, so dydx = -b/a
|
623 |
+
deq = eq.diff(x)
|
624 |
+
b = deq.xreplace({dydx: S.Zero})
|
625 |
+
a = (deq - b).xreplace({dydx: S.One})
|
626 |
+
yp = factor_terms(expand_mul(cancel((-b/a).subs(derivs)), deep=False))
|
627 |
+
if i == n - 1:
|
628 |
+
return yp.subs([(v, k) for k, v in f.items()])
|
629 |
+
derivs[dydx] = yp
|
630 |
+
eq = dydx - yp
|
631 |
+
dydx = dydx.diff(x)
|
632 |
+
|
633 |
+
|
634 |
+
def intersection(*entities, pairwise=False, **kwargs):
|
635 |
+
"""The intersection of a collection of GeometryEntity instances.
|
636 |
+
|
637 |
+
Parameters
|
638 |
+
==========
|
639 |
+
entities : sequence of GeometryEntity
|
640 |
+
pairwise (keyword argument) : Can be either True or False
|
641 |
+
|
642 |
+
Returns
|
643 |
+
=======
|
644 |
+
intersection : list of GeometryEntity
|
645 |
+
|
646 |
+
Raises
|
647 |
+
======
|
648 |
+
NotImplementedError
|
649 |
+
When unable to calculate intersection.
|
650 |
+
|
651 |
+
Notes
|
652 |
+
=====
|
653 |
+
The intersection of any geometrical entity with itself should return
|
654 |
+
a list with one item: the entity in question.
|
655 |
+
An intersection requires two or more entities. If only a single
|
656 |
+
entity is given then the function will return an empty list.
|
657 |
+
It is possible for `intersection` to miss intersections that one
|
658 |
+
knows exists because the required quantities were not fully
|
659 |
+
simplified internally.
|
660 |
+
Reals should be converted to Rationals, e.g. Rational(str(real_num))
|
661 |
+
or else failures due to floating point issues may result.
|
662 |
+
|
663 |
+
Case 1: When the keyword argument 'pairwise' is False (default value):
|
664 |
+
In this case, the function returns a list of intersections common to
|
665 |
+
all entities.
|
666 |
+
|
667 |
+
Case 2: When the keyword argument 'pairwise' is True:
|
668 |
+
In this case, the functions returns a list intersections that occur
|
669 |
+
between any pair of entities.
|
670 |
+
|
671 |
+
See Also
|
672 |
+
========
|
673 |
+
|
674 |
+
sympy.geometry.entity.GeometryEntity.intersection
|
675 |
+
|
676 |
+
Examples
|
677 |
+
========
|
678 |
+
|
679 |
+
>>> from sympy import Ray, Circle, intersection
|
680 |
+
>>> c = Circle((0, 1), 1)
|
681 |
+
>>> intersection(c, c.center)
|
682 |
+
[]
|
683 |
+
>>> right = Ray((0, 0), (1, 0))
|
684 |
+
>>> up = Ray((0, 0), (0, 1))
|
685 |
+
>>> intersection(c, right, up)
|
686 |
+
[Point2D(0, 0)]
|
687 |
+
>>> intersection(c, right, up, pairwise=True)
|
688 |
+
[Point2D(0, 0), Point2D(0, 2)]
|
689 |
+
>>> left = Ray((1, 0), (0, 0))
|
690 |
+
>>> intersection(right, left)
|
691 |
+
[Segment2D(Point2D(0, 0), Point2D(1, 0))]
|
692 |
+
|
693 |
+
"""
|
694 |
+
if len(entities) <= 1:
|
695 |
+
return []
|
696 |
+
|
697 |
+
# entities may be an immutable tuple
|
698 |
+
entities = list(entities)
|
699 |
+
for i, e in enumerate(entities):
|
700 |
+
if not isinstance(e, GeometryEntity):
|
701 |
+
entities[i] = Point(e)
|
702 |
+
|
703 |
+
if not pairwise:
|
704 |
+
# find the intersection common to all objects
|
705 |
+
res = entities[0].intersection(entities[1])
|
706 |
+
for entity in entities[2:]:
|
707 |
+
newres = []
|
708 |
+
for x in res:
|
709 |
+
newres.extend(x.intersection(entity))
|
710 |
+
res = newres
|
711 |
+
return res
|
712 |
+
|
713 |
+
# find all pairwise intersections
|
714 |
+
ans = []
|
715 |
+
for j in range(len(entities)):
|
716 |
+
for k in range(j + 1, len(entities)):
|
717 |
+
ans.extend(intersection(entities[j], entities[k]))
|
718 |
+
return list(ordered(set(ans)))
|
env-llmeval/lib/python3.10/site-packages/sympy/liealgebras/cartan_matrix.py
ADDED
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from .cartan_type import CartanType
|
2 |
+
|
3 |
+
def CartanMatrix(ct):
|
4 |
+
"""Access the Cartan matrix of a specific Lie algebra
|
5 |
+
|
6 |
+
Examples
|
7 |
+
========
|
8 |
+
|
9 |
+
>>> from sympy.liealgebras.cartan_matrix import CartanMatrix
|
10 |
+
>>> CartanMatrix("A2")
|
11 |
+
Matrix([
|
12 |
+
[ 2, -1],
|
13 |
+
[-1, 2]])
|
14 |
+
|
15 |
+
>>> CartanMatrix(['C', 3])
|
16 |
+
Matrix([
|
17 |
+
[ 2, -1, 0],
|
18 |
+
[-1, 2, -1],
|
19 |
+
[ 0, -2, 2]])
|
20 |
+
|
21 |
+
This method works by returning the Cartan matrix
|
22 |
+
which corresponds to Cartan type t.
|
23 |
+
"""
|
24 |
+
|
25 |
+
return CartanType(ct).cartan_matrix()
|
env-llmeval/lib/python3.10/site-packages/sympy/liealgebras/root_system.py
ADDED
@@ -0,0 +1,199 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from .cartan_type import CartanType
|
2 |
+
from sympy.core.basic import Atom
|
3 |
+
|
4 |
+
class RootSystem(Atom):
|
5 |
+
"""Represent the root system of a simple Lie algebra
|
6 |
+
|
7 |
+
Every simple Lie algebra has a unique root system. To find the root
|
8 |
+
system, we first consider the Cartan subalgebra of g, which is the maximal
|
9 |
+
abelian subalgebra, and consider the adjoint action of g on this
|
10 |
+
subalgebra. There is a root system associated with this action. Now, a
|
11 |
+
root system over a vector space V is a set of finite vectors Phi (called
|
12 |
+
roots), which satisfy:
|
13 |
+
|
14 |
+
1. The roots span V
|
15 |
+
2. The only scalar multiples of x in Phi are x and -x
|
16 |
+
3. For every x in Phi, the set Phi is closed under reflection
|
17 |
+
through the hyperplane perpendicular to x.
|
18 |
+
4. If x and y are roots in Phi, then the projection of y onto
|
19 |
+
the line through x is a half-integral multiple of x.
|
20 |
+
|
21 |
+
Now, there is a subset of Phi, which we will call Delta, such that:
|
22 |
+
1. Delta is a basis of V
|
23 |
+
2. Each root x in Phi can be written x = sum k_y y for y in Delta
|
24 |
+
|
25 |
+
The elements of Delta are called the simple roots.
|
26 |
+
Therefore, we see that the simple roots span the root space of a given
|
27 |
+
simple Lie algebra.
|
28 |
+
|
29 |
+
References
|
30 |
+
==========
|
31 |
+
|
32 |
+
.. [1] https://en.wikipedia.org/wiki/Root_system
|
33 |
+
.. [2] Lie Algebras and Representation Theory - Humphreys
|
34 |
+
|
35 |
+
"""
|
36 |
+
|
37 |
+
def __new__(cls, cartantype):
|
38 |
+
"""Create a new RootSystem object
|
39 |
+
|
40 |
+
This method assigns an attribute called cartan_type to each instance of
|
41 |
+
a RootSystem object. When an instance of RootSystem is called, it
|
42 |
+
needs an argument, which should be an instance of a simple Lie algebra.
|
43 |
+
We then take the CartanType of this argument and set it as the
|
44 |
+
cartan_type attribute of the RootSystem instance.
|
45 |
+
|
46 |
+
"""
|
47 |
+
obj = Atom.__new__(cls)
|
48 |
+
obj.cartan_type = CartanType(cartantype)
|
49 |
+
return obj
|
50 |
+
|
51 |
+
def simple_roots(self):
|
52 |
+
"""Generate the simple roots of the Lie algebra
|
53 |
+
|
54 |
+
The rank of the Lie algebra determines the number of simple roots that
|
55 |
+
it has. This method obtains the rank of the Lie algebra, and then uses
|
56 |
+
the simple_root method from the Lie algebra classes to generate all the
|
57 |
+
simple roots.
|
58 |
+
|
59 |
+
Examples
|
60 |
+
========
|
61 |
+
|
62 |
+
>>> from sympy.liealgebras.root_system import RootSystem
|
63 |
+
>>> c = RootSystem("A3")
|
64 |
+
>>> roots = c.simple_roots()
|
65 |
+
>>> roots
|
66 |
+
{1: [1, -1, 0, 0], 2: [0, 1, -1, 0], 3: [0, 0, 1, -1]}
|
67 |
+
|
68 |
+
"""
|
69 |
+
n = self.cartan_type.rank()
|
70 |
+
roots = {}
|
71 |
+
for i in range(1, n+1):
|
72 |
+
root = self.cartan_type.simple_root(i)
|
73 |
+
roots[i] = root
|
74 |
+
return roots
|
75 |
+
|
76 |
+
|
77 |
+
def all_roots(self):
|
78 |
+
"""Generate all the roots of a given root system
|
79 |
+
|
80 |
+
The result is a dictionary where the keys are integer numbers. It
|
81 |
+
generates the roots by getting the dictionary of all positive roots
|
82 |
+
from the bases classes, and then taking each root, and multiplying it
|
83 |
+
by -1 and adding it to the dictionary. In this way all the negative
|
84 |
+
roots are generated.
|
85 |
+
|
86 |
+
"""
|
87 |
+
alpha = self.cartan_type.positive_roots()
|
88 |
+
keys = list(alpha.keys())
|
89 |
+
k = max(keys)
|
90 |
+
for val in keys:
|
91 |
+
k += 1
|
92 |
+
root = alpha[val]
|
93 |
+
newroot = [-x for x in root]
|
94 |
+
alpha[k] = newroot
|
95 |
+
return alpha
|
96 |
+
|
97 |
+
def root_space(self):
|
98 |
+
"""Return the span of the simple roots
|
99 |
+
|
100 |
+
The root space is the vector space spanned by the simple roots, i.e. it
|
101 |
+
is a vector space with a distinguished basis, the simple roots. This
|
102 |
+
method returns a string that represents the root space as the span of
|
103 |
+
the simple roots, alpha[1],...., alpha[n].
|
104 |
+
|
105 |
+
Examples
|
106 |
+
========
|
107 |
+
|
108 |
+
>>> from sympy.liealgebras.root_system import RootSystem
|
109 |
+
>>> c = RootSystem("A3")
|
110 |
+
>>> c.root_space()
|
111 |
+
'alpha[1] + alpha[2] + alpha[3]'
|
112 |
+
|
113 |
+
"""
|
114 |
+
n = self.cartan_type.rank()
|
115 |
+
rs = " + ".join("alpha["+str(i) +"]" for i in range(1, n+1))
|
116 |
+
return rs
|
117 |
+
|
118 |
+
def add_simple_roots(self, root1, root2):
|
119 |
+
"""Add two simple roots together
|
120 |
+
|
121 |
+
The function takes as input two integers, root1 and root2. It then
|
122 |
+
uses these integers as keys in the dictionary of simple roots, and gets
|
123 |
+
the corresponding simple roots, and then adds them together.
|
124 |
+
|
125 |
+
Examples
|
126 |
+
========
|
127 |
+
|
128 |
+
>>> from sympy.liealgebras.root_system import RootSystem
|
129 |
+
>>> c = RootSystem("A3")
|
130 |
+
>>> newroot = c.add_simple_roots(1, 2)
|
131 |
+
>>> newroot
|
132 |
+
[1, 0, -1, 0]
|
133 |
+
|
134 |
+
"""
|
135 |
+
|
136 |
+
alpha = self.simple_roots()
|
137 |
+
if root1 > len(alpha) or root2 > len(alpha):
|
138 |
+
raise ValueError("You've used a root that doesn't exist!")
|
139 |
+
a1 = alpha[root1]
|
140 |
+
a2 = alpha[root2]
|
141 |
+
newroot = [_a1 + _a2 for _a1, _a2 in zip(a1, a2)]
|
142 |
+
return newroot
|
143 |
+
|
144 |
+
def add_as_roots(self, root1, root2):
|
145 |
+
"""Add two roots together if and only if their sum is also a root
|
146 |
+
|
147 |
+
It takes as input two vectors which should be roots. It then computes
|
148 |
+
their sum and checks if it is in the list of all possible roots. If it
|
149 |
+
is, it returns the sum. Otherwise it returns a string saying that the
|
150 |
+
sum is not a root.
|
151 |
+
|
152 |
+
Examples
|
153 |
+
========
|
154 |
+
|
155 |
+
>>> from sympy.liealgebras.root_system import RootSystem
|
156 |
+
>>> c = RootSystem("A3")
|
157 |
+
>>> c.add_as_roots([1, 0, -1, 0], [0, 0, 1, -1])
|
158 |
+
[1, 0, 0, -1]
|
159 |
+
>>> c.add_as_roots([1, -1, 0, 0], [0, 0, -1, 1])
|
160 |
+
'The sum of these two roots is not a root'
|
161 |
+
|
162 |
+
"""
|
163 |
+
alpha = self.all_roots()
|
164 |
+
newroot = [r1 + r2 for r1, r2 in zip(root1, root2)]
|
165 |
+
if newroot in alpha.values():
|
166 |
+
return newroot
|
167 |
+
else:
|
168 |
+
return "The sum of these two roots is not a root"
|
169 |
+
|
170 |
+
|
171 |
+
def cartan_matrix(self):
|
172 |
+
"""Cartan matrix of Lie algebra associated with this root system
|
173 |
+
|
174 |
+
Examples
|
175 |
+
========
|
176 |
+
|
177 |
+
>>> from sympy.liealgebras.root_system import RootSystem
|
178 |
+
>>> c = RootSystem("A3")
|
179 |
+
>>> c.cartan_matrix()
|
180 |
+
Matrix([
|
181 |
+
[ 2, -1, 0],
|
182 |
+
[-1, 2, -1],
|
183 |
+
[ 0, -1, 2]])
|
184 |
+
"""
|
185 |
+
return self.cartan_type.cartan_matrix()
|
186 |
+
|
187 |
+
def dynkin_diagram(self):
|
188 |
+
"""Dynkin diagram of the Lie algebra associated with this root system
|
189 |
+
|
190 |
+
Examples
|
191 |
+
========
|
192 |
+
|
193 |
+
>>> from sympy.liealgebras.root_system import RootSystem
|
194 |
+
>>> c = RootSystem("A3")
|
195 |
+
>>> print(c.dynkin_diagram())
|
196 |
+
0---0---0
|
197 |
+
1 2 3
|
198 |
+
"""
|
199 |
+
return self.cartan_type.dynkin_diagram()
|
env-llmeval/lib/python3.10/site-packages/sympy/multipledispatch/__init__.py
ADDED
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from .core import dispatch
|
2 |
+
from .dispatcher import (Dispatcher, halt_ordering, restart_ordering,
|
3 |
+
MDNotImplementedError)
|
4 |
+
|
5 |
+
__version__ = '0.4.9'
|
6 |
+
|
7 |
+
__all__ = [
|
8 |
+
'dispatch',
|
9 |
+
|
10 |
+
'Dispatcher', 'halt_ordering', 'restart_ordering', 'MDNotImplementedError',
|
11 |
+
]
|
env-llmeval/lib/python3.10/site-packages/sympy/multipledispatch/conflict.py
ADDED
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from .utils import _toposort, groupby
|
2 |
+
|
3 |
+
class AmbiguityWarning(Warning):
|
4 |
+
pass
|
5 |
+
|
6 |
+
|
7 |
+
def supercedes(a, b):
|
8 |
+
""" A is consistent and strictly more specific than B """
|
9 |
+
return len(a) == len(b) and all(map(issubclass, a, b))
|
10 |
+
|
11 |
+
|
12 |
+
def consistent(a, b):
|
13 |
+
""" It is possible for an argument list to satisfy both A and B """
|
14 |
+
return (len(a) == len(b) and
|
15 |
+
all(issubclass(aa, bb) or issubclass(bb, aa)
|
16 |
+
for aa, bb in zip(a, b)))
|
17 |
+
|
18 |
+
|
19 |
+
def ambiguous(a, b):
|
20 |
+
""" A is consistent with B but neither is strictly more specific """
|
21 |
+
return consistent(a, b) and not (supercedes(a, b) or supercedes(b, a))
|
22 |
+
|
23 |
+
|
24 |
+
def ambiguities(signatures):
|
25 |
+
""" All signature pairs such that A is ambiguous with B """
|
26 |
+
signatures = list(map(tuple, signatures))
|
27 |
+
return {(a, b) for a in signatures for b in signatures
|
28 |
+
if hash(a) < hash(b)
|
29 |
+
and ambiguous(a, b)
|
30 |
+
and not any(supercedes(c, a) and supercedes(c, b)
|
31 |
+
for c in signatures)}
|
32 |
+
|
33 |
+
|
34 |
+
def super_signature(signatures):
|
35 |
+
""" A signature that would break ambiguities """
|
36 |
+
n = len(signatures[0])
|
37 |
+
assert all(len(s) == n for s in signatures)
|
38 |
+
|
39 |
+
return [max([type.mro(sig[i]) for sig in signatures], key=len)[0]
|
40 |
+
for i in range(n)]
|
41 |
+
|
42 |
+
|
43 |
+
def edge(a, b, tie_breaker=hash):
|
44 |
+
""" A should be checked before B
|
45 |
+
|
46 |
+
Tie broken by tie_breaker, defaults to ``hash``
|
47 |
+
"""
|
48 |
+
if supercedes(a, b):
|
49 |
+
if supercedes(b, a):
|
50 |
+
return tie_breaker(a) > tie_breaker(b)
|
51 |
+
else:
|
52 |
+
return True
|
53 |
+
return False
|
54 |
+
|
55 |
+
|
56 |
+
def ordering(signatures):
|
57 |
+
""" A sane ordering of signatures to check, first to last
|
58 |
+
|
59 |
+
Topoological sort of edges as given by ``edge`` and ``supercedes``
|
60 |
+
"""
|
61 |
+
signatures = list(map(tuple, signatures))
|
62 |
+
edges = [(a, b) for a in signatures for b in signatures if edge(a, b)]
|
63 |
+
edges = groupby(lambda x: x[0], edges)
|
64 |
+
for s in signatures:
|
65 |
+
if s not in edges:
|
66 |
+
edges[s] = []
|
67 |
+
edges = {k: [b for a, b in v] for k, v in edges.items()}
|
68 |
+
return _toposort(edges)
|
env-llmeval/lib/python3.10/site-packages/sympy/multipledispatch/core.py
ADDED
@@ -0,0 +1,83 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from __future__ import annotations
|
2 |
+
from typing import Any
|
3 |
+
|
4 |
+
import inspect
|
5 |
+
|
6 |
+
from .dispatcher import Dispatcher, MethodDispatcher, ambiguity_warn
|
7 |
+
|
8 |
+
# XXX: This parameter to dispatch isn't documented and isn't used anywhere in
|
9 |
+
# sympy. Maybe it should just be removed.
|
10 |
+
global_namespace: dict[str, Any] = {}
|
11 |
+
|
12 |
+
|
13 |
+
def dispatch(*types, namespace=global_namespace, on_ambiguity=ambiguity_warn):
|
14 |
+
""" Dispatch function on the types of the inputs
|
15 |
+
|
16 |
+
Supports dispatch on all non-keyword arguments.
|
17 |
+
|
18 |
+
Collects implementations based on the function name. Ignores namespaces.
|
19 |
+
|
20 |
+
If ambiguous type signatures occur a warning is raised when the function is
|
21 |
+
defined suggesting the additional method to break the ambiguity.
|
22 |
+
|
23 |
+
Examples
|
24 |
+
--------
|
25 |
+
|
26 |
+
>>> from sympy.multipledispatch import dispatch
|
27 |
+
>>> @dispatch(int)
|
28 |
+
... def f(x):
|
29 |
+
... return x + 1
|
30 |
+
|
31 |
+
>>> @dispatch(float)
|
32 |
+
... def f(x): # noqa: F811
|
33 |
+
... return x - 1
|
34 |
+
|
35 |
+
>>> f(3)
|
36 |
+
4
|
37 |
+
>>> f(3.0)
|
38 |
+
2.0
|
39 |
+
|
40 |
+
Specify an isolated namespace with the namespace keyword argument
|
41 |
+
|
42 |
+
>>> my_namespace = dict()
|
43 |
+
>>> @dispatch(int, namespace=my_namespace)
|
44 |
+
... def foo(x):
|
45 |
+
... return x + 1
|
46 |
+
|
47 |
+
Dispatch on instance methods within classes
|
48 |
+
|
49 |
+
>>> class MyClass(object):
|
50 |
+
... @dispatch(list)
|
51 |
+
... def __init__(self, data):
|
52 |
+
... self.data = data
|
53 |
+
... @dispatch(int)
|
54 |
+
... def __init__(self, datum): # noqa: F811
|
55 |
+
... self.data = [datum]
|
56 |
+
"""
|
57 |
+
types = tuple(types)
|
58 |
+
|
59 |
+
def _(func):
|
60 |
+
name = func.__name__
|
61 |
+
|
62 |
+
if ismethod(func):
|
63 |
+
dispatcher = inspect.currentframe().f_back.f_locals.get(
|
64 |
+
name,
|
65 |
+
MethodDispatcher(name))
|
66 |
+
else:
|
67 |
+
if name not in namespace:
|
68 |
+
namespace[name] = Dispatcher(name)
|
69 |
+
dispatcher = namespace[name]
|
70 |
+
|
71 |
+
dispatcher.add(types, func, on_ambiguity=on_ambiguity)
|
72 |
+
return dispatcher
|
73 |
+
return _
|
74 |
+
|
75 |
+
|
76 |
+
def ismethod(func):
|
77 |
+
""" Is func a method?
|
78 |
+
|
79 |
+
Note that this has to work as the method is defined but before the class is
|
80 |
+
defined. At this stage methods look like functions.
|
81 |
+
"""
|
82 |
+
signature = inspect.signature(func)
|
83 |
+
return signature.parameters.get('self', None) is not None
|
env-llmeval/lib/python3.10/site-packages/sympy/multipledispatch/dispatcher.py
ADDED
@@ -0,0 +1,413 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from __future__ import annotations
|
2 |
+
|
3 |
+
from warnings import warn
|
4 |
+
import inspect
|
5 |
+
from .conflict import ordering, ambiguities, super_signature, AmbiguityWarning
|
6 |
+
from .utils import expand_tuples
|
7 |
+
import itertools as itl
|
8 |
+
|
9 |
+
|
10 |
+
class MDNotImplementedError(NotImplementedError):
|
11 |
+
""" A NotImplementedError for multiple dispatch """
|
12 |
+
|
13 |
+
|
14 |
+
### Functions for on_ambiguity
|
15 |
+
|
16 |
+
def ambiguity_warn(dispatcher, ambiguities):
|
17 |
+
""" Raise warning when ambiguity is detected
|
18 |
+
|
19 |
+
Parameters
|
20 |
+
----------
|
21 |
+
dispatcher : Dispatcher
|
22 |
+
The dispatcher on which the ambiguity was detected
|
23 |
+
ambiguities : set
|
24 |
+
Set of type signature pairs that are ambiguous within this dispatcher
|
25 |
+
|
26 |
+
See Also:
|
27 |
+
Dispatcher.add
|
28 |
+
warning_text
|
29 |
+
"""
|
30 |
+
warn(warning_text(dispatcher.name, ambiguities), AmbiguityWarning)
|
31 |
+
|
32 |
+
|
33 |
+
class RaiseNotImplementedError:
|
34 |
+
"""Raise ``NotImplementedError`` when called."""
|
35 |
+
|
36 |
+
def __init__(self, dispatcher):
|
37 |
+
self.dispatcher = dispatcher
|
38 |
+
|
39 |
+
def __call__(self, *args, **kwargs):
|
40 |
+
types = tuple(type(a) for a in args)
|
41 |
+
raise NotImplementedError(
|
42 |
+
"Ambiguous signature for %s: <%s>" % (
|
43 |
+
self.dispatcher.name, str_signature(types)
|
44 |
+
))
|
45 |
+
|
46 |
+
def ambiguity_register_error_ignore_dup(dispatcher, ambiguities):
|
47 |
+
"""
|
48 |
+
If super signature for ambiguous types is duplicate types, ignore it.
|
49 |
+
Else, register instance of ``RaiseNotImplementedError`` for ambiguous types.
|
50 |
+
|
51 |
+
Parameters
|
52 |
+
----------
|
53 |
+
dispatcher : Dispatcher
|
54 |
+
The dispatcher on which the ambiguity was detected
|
55 |
+
ambiguities : set
|
56 |
+
Set of type signature pairs that are ambiguous within this dispatcher
|
57 |
+
|
58 |
+
See Also:
|
59 |
+
Dispatcher.add
|
60 |
+
ambiguity_warn
|
61 |
+
"""
|
62 |
+
for amb in ambiguities:
|
63 |
+
signature = tuple(super_signature(amb))
|
64 |
+
if len(set(signature)) == 1:
|
65 |
+
continue
|
66 |
+
dispatcher.add(
|
67 |
+
signature, RaiseNotImplementedError(dispatcher),
|
68 |
+
on_ambiguity=ambiguity_register_error_ignore_dup
|
69 |
+
)
|
70 |
+
|
71 |
+
###
|
72 |
+
|
73 |
+
|
74 |
+
_unresolved_dispatchers: set[Dispatcher] = set()
|
75 |
+
_resolve = [True]
|
76 |
+
|
77 |
+
|
78 |
+
def halt_ordering():
|
79 |
+
_resolve[0] = False
|
80 |
+
|
81 |
+
|
82 |
+
def restart_ordering(on_ambiguity=ambiguity_warn):
|
83 |
+
_resolve[0] = True
|
84 |
+
while _unresolved_dispatchers:
|
85 |
+
dispatcher = _unresolved_dispatchers.pop()
|
86 |
+
dispatcher.reorder(on_ambiguity=on_ambiguity)
|
87 |
+
|
88 |
+
|
89 |
+
class Dispatcher:
|
90 |
+
""" Dispatch methods based on type signature
|
91 |
+
|
92 |
+
Use ``dispatch`` to add implementations
|
93 |
+
|
94 |
+
Examples
|
95 |
+
--------
|
96 |
+
|
97 |
+
>>> from sympy.multipledispatch import dispatch
|
98 |
+
>>> @dispatch(int)
|
99 |
+
... def f(x):
|
100 |
+
... return x + 1
|
101 |
+
|
102 |
+
>>> @dispatch(float)
|
103 |
+
... def f(x): # noqa: F811
|
104 |
+
... return x - 1
|
105 |
+
|
106 |
+
>>> f(3)
|
107 |
+
4
|
108 |
+
>>> f(3.0)
|
109 |
+
2.0
|
110 |
+
"""
|
111 |
+
__slots__ = '__name__', 'name', 'funcs', 'ordering', '_cache', 'doc'
|
112 |
+
|
113 |
+
def __init__(self, name, doc=None):
|
114 |
+
self.name = self.__name__ = name
|
115 |
+
self.funcs = {}
|
116 |
+
self._cache = {}
|
117 |
+
self.ordering = []
|
118 |
+
self.doc = doc
|
119 |
+
|
120 |
+
def register(self, *types, **kwargs):
|
121 |
+
""" Register dispatcher with new implementation
|
122 |
+
|
123 |
+
>>> from sympy.multipledispatch.dispatcher import Dispatcher
|
124 |
+
>>> f = Dispatcher('f')
|
125 |
+
>>> @f.register(int)
|
126 |
+
... def inc(x):
|
127 |
+
... return x + 1
|
128 |
+
|
129 |
+
>>> @f.register(float)
|
130 |
+
... def dec(x):
|
131 |
+
... return x - 1
|
132 |
+
|
133 |
+
>>> @f.register(list)
|
134 |
+
... @f.register(tuple)
|
135 |
+
... def reverse(x):
|
136 |
+
... return x[::-1]
|
137 |
+
|
138 |
+
>>> f(1)
|
139 |
+
2
|
140 |
+
|
141 |
+
>>> f(1.0)
|
142 |
+
0.0
|
143 |
+
|
144 |
+
>>> f([1, 2, 3])
|
145 |
+
[3, 2, 1]
|
146 |
+
"""
|
147 |
+
def _(func):
|
148 |
+
self.add(types, func, **kwargs)
|
149 |
+
return func
|
150 |
+
return _
|
151 |
+
|
152 |
+
@classmethod
|
153 |
+
def get_func_params(cls, func):
|
154 |
+
if hasattr(inspect, "signature"):
|
155 |
+
sig = inspect.signature(func)
|
156 |
+
return sig.parameters.values()
|
157 |
+
|
158 |
+
@classmethod
|
159 |
+
def get_func_annotations(cls, func):
|
160 |
+
""" Get annotations of function positional parameters
|
161 |
+
"""
|
162 |
+
params = cls.get_func_params(func)
|
163 |
+
if params:
|
164 |
+
Parameter = inspect.Parameter
|
165 |
+
|
166 |
+
params = (param for param in params
|
167 |
+
if param.kind in
|
168 |
+
(Parameter.POSITIONAL_ONLY,
|
169 |
+
Parameter.POSITIONAL_OR_KEYWORD))
|
170 |
+
|
171 |
+
annotations = tuple(
|
172 |
+
param.annotation
|
173 |
+
for param in params)
|
174 |
+
|
175 |
+
if not any(ann is Parameter.empty for ann in annotations):
|
176 |
+
return annotations
|
177 |
+
|
178 |
+
def add(self, signature, func, on_ambiguity=ambiguity_warn):
|
179 |
+
""" Add new types/method pair to dispatcher
|
180 |
+
|
181 |
+
>>> from sympy.multipledispatch import Dispatcher
|
182 |
+
>>> D = Dispatcher('add')
|
183 |
+
>>> D.add((int, int), lambda x, y: x + y)
|
184 |
+
>>> D.add((float, float), lambda x, y: x + y)
|
185 |
+
|
186 |
+
>>> D(1, 2)
|
187 |
+
3
|
188 |
+
>>> D(1, 2.0)
|
189 |
+
Traceback (most recent call last):
|
190 |
+
...
|
191 |
+
NotImplementedError: Could not find signature for add: <int, float>
|
192 |
+
|
193 |
+
When ``add`` detects a warning it calls the ``on_ambiguity`` callback
|
194 |
+
with a dispatcher/itself, and a set of ambiguous type signature pairs
|
195 |
+
as inputs. See ``ambiguity_warn`` for an example.
|
196 |
+
"""
|
197 |
+
# Handle annotations
|
198 |
+
if not signature:
|
199 |
+
annotations = self.get_func_annotations(func)
|
200 |
+
if annotations:
|
201 |
+
signature = annotations
|
202 |
+
|
203 |
+
# Handle union types
|
204 |
+
if any(isinstance(typ, tuple) for typ in signature):
|
205 |
+
for typs in expand_tuples(signature):
|
206 |
+
self.add(typs, func, on_ambiguity)
|
207 |
+
return
|
208 |
+
|
209 |
+
for typ in signature:
|
210 |
+
if not isinstance(typ, type):
|
211 |
+
str_sig = ', '.join(c.__name__ if isinstance(c, type)
|
212 |
+
else str(c) for c in signature)
|
213 |
+
raise TypeError("Tried to dispatch on non-type: %s\n"
|
214 |
+
"In signature: <%s>\n"
|
215 |
+
"In function: %s" %
|
216 |
+
(typ, str_sig, self.name))
|
217 |
+
|
218 |
+
self.funcs[signature] = func
|
219 |
+
self.reorder(on_ambiguity=on_ambiguity)
|
220 |
+
self._cache.clear()
|
221 |
+
|
222 |
+
def reorder(self, on_ambiguity=ambiguity_warn):
|
223 |
+
if _resolve[0]:
|
224 |
+
self.ordering = ordering(self.funcs)
|
225 |
+
amb = ambiguities(self.funcs)
|
226 |
+
if amb:
|
227 |
+
on_ambiguity(self, amb)
|
228 |
+
else:
|
229 |
+
_unresolved_dispatchers.add(self)
|
230 |
+
|
231 |
+
def __call__(self, *args, **kwargs):
|
232 |
+
types = tuple([type(arg) for arg in args])
|
233 |
+
try:
|
234 |
+
func = self._cache[types]
|
235 |
+
except KeyError:
|
236 |
+
func = self.dispatch(*types)
|
237 |
+
if not func:
|
238 |
+
raise NotImplementedError(
|
239 |
+
'Could not find signature for %s: <%s>' %
|
240 |
+
(self.name, str_signature(types)))
|
241 |
+
self._cache[types] = func
|
242 |
+
try:
|
243 |
+
return func(*args, **kwargs)
|
244 |
+
|
245 |
+
except MDNotImplementedError:
|
246 |
+
funcs = self.dispatch_iter(*types)
|
247 |
+
next(funcs) # burn first
|
248 |
+
for func in funcs:
|
249 |
+
try:
|
250 |
+
return func(*args, **kwargs)
|
251 |
+
except MDNotImplementedError:
|
252 |
+
pass
|
253 |
+
raise NotImplementedError("Matching functions for "
|
254 |
+
"%s: <%s> found, but none completed successfully"
|
255 |
+
% (self.name, str_signature(types)))
|
256 |
+
|
257 |
+
def __str__(self):
|
258 |
+
return "<dispatched %s>" % self.name
|
259 |
+
__repr__ = __str__
|
260 |
+
|
261 |
+
def dispatch(self, *types):
|
262 |
+
""" Deterimine appropriate implementation for this type signature
|
263 |
+
|
264 |
+
This method is internal. Users should call this object as a function.
|
265 |
+
Implementation resolution occurs within the ``__call__`` method.
|
266 |
+
|
267 |
+
>>> from sympy.multipledispatch import dispatch
|
268 |
+
>>> @dispatch(int)
|
269 |
+
... def inc(x):
|
270 |
+
... return x + 1
|
271 |
+
|
272 |
+
>>> implementation = inc.dispatch(int)
|
273 |
+
>>> implementation(3)
|
274 |
+
4
|
275 |
+
|
276 |
+
>>> print(inc.dispatch(float))
|
277 |
+
None
|
278 |
+
|
279 |
+
See Also:
|
280 |
+
``sympy.multipledispatch.conflict`` - module to determine resolution order
|
281 |
+
"""
|
282 |
+
|
283 |
+
if types in self.funcs:
|
284 |
+
return self.funcs[types]
|
285 |
+
|
286 |
+
try:
|
287 |
+
return next(self.dispatch_iter(*types))
|
288 |
+
except StopIteration:
|
289 |
+
return None
|
290 |
+
|
291 |
+
def dispatch_iter(self, *types):
|
292 |
+
n = len(types)
|
293 |
+
for signature in self.ordering:
|
294 |
+
if len(signature) == n and all(map(issubclass, types, signature)):
|
295 |
+
result = self.funcs[signature]
|
296 |
+
yield result
|
297 |
+
|
298 |
+
def resolve(self, types):
|
299 |
+
""" Deterimine appropriate implementation for this type signature
|
300 |
+
|
301 |
+
.. deprecated:: 0.4.4
|
302 |
+
Use ``dispatch(*types)`` instead
|
303 |
+
"""
|
304 |
+
warn("resolve() is deprecated, use dispatch(*types)",
|
305 |
+
DeprecationWarning)
|
306 |
+
|
307 |
+
return self.dispatch(*types)
|
308 |
+
|
309 |
+
def __getstate__(self):
|
310 |
+
return {'name': self.name,
|
311 |
+
'funcs': self.funcs}
|
312 |
+
|
313 |
+
def __setstate__(self, d):
|
314 |
+
self.name = d['name']
|
315 |
+
self.funcs = d['funcs']
|
316 |
+
self.ordering = ordering(self.funcs)
|
317 |
+
self._cache = {}
|
318 |
+
|
319 |
+
@property
|
320 |
+
def __doc__(self):
|
321 |
+
docs = ["Multiply dispatched method: %s" % self.name]
|
322 |
+
|
323 |
+
if self.doc:
|
324 |
+
docs.append(self.doc)
|
325 |
+
|
326 |
+
other = []
|
327 |
+
for sig in self.ordering[::-1]:
|
328 |
+
func = self.funcs[sig]
|
329 |
+
if func.__doc__:
|
330 |
+
s = 'Inputs: <%s>\n' % str_signature(sig)
|
331 |
+
s += '-' * len(s) + '\n'
|
332 |
+
s += func.__doc__.strip()
|
333 |
+
docs.append(s)
|
334 |
+
else:
|
335 |
+
other.append(str_signature(sig))
|
336 |
+
|
337 |
+
if other:
|
338 |
+
docs.append('Other signatures:\n ' + '\n '.join(other))
|
339 |
+
|
340 |
+
return '\n\n'.join(docs)
|
341 |
+
|
342 |
+
def _help(self, *args):
|
343 |
+
return self.dispatch(*map(type, args)).__doc__
|
344 |
+
|
345 |
+
def help(self, *args, **kwargs):
|
346 |
+
""" Print docstring for the function corresponding to inputs """
|
347 |
+
print(self._help(*args))
|
348 |
+
|
349 |
+
def _source(self, *args):
|
350 |
+
func = self.dispatch(*map(type, args))
|
351 |
+
if not func:
|
352 |
+
raise TypeError("No function found")
|
353 |
+
return source(func)
|
354 |
+
|
355 |
+
def source(self, *args, **kwargs):
|
356 |
+
""" Print source code for the function corresponding to inputs """
|
357 |
+
print(self._source(*args))
|
358 |
+
|
359 |
+
|
360 |
+
def source(func):
|
361 |
+
s = 'File: %s\n\n' % inspect.getsourcefile(func)
|
362 |
+
s = s + inspect.getsource(func)
|
363 |
+
return s
|
364 |
+
|
365 |
+
|
366 |
+
class MethodDispatcher(Dispatcher):
|
367 |
+
""" Dispatch methods based on type signature
|
368 |
+
|
369 |
+
See Also:
|
370 |
+
Dispatcher
|
371 |
+
"""
|
372 |
+
|
373 |
+
@classmethod
|
374 |
+
def get_func_params(cls, func):
|
375 |
+
if hasattr(inspect, "signature"):
|
376 |
+
sig = inspect.signature(func)
|
377 |
+
return itl.islice(sig.parameters.values(), 1, None)
|
378 |
+
|
379 |
+
def __get__(self, instance, owner):
|
380 |
+
self.obj = instance
|
381 |
+
self.cls = owner
|
382 |
+
return self
|
383 |
+
|
384 |
+
def __call__(self, *args, **kwargs):
|
385 |
+
types = tuple([type(arg) for arg in args])
|
386 |
+
func = self.dispatch(*types)
|
387 |
+
if not func:
|
388 |
+
raise NotImplementedError('Could not find signature for %s: <%s>' %
|
389 |
+
(self.name, str_signature(types)))
|
390 |
+
return func(self.obj, *args, **kwargs)
|
391 |
+
|
392 |
+
|
393 |
+
def str_signature(sig):
|
394 |
+
""" String representation of type signature
|
395 |
+
|
396 |
+
>>> from sympy.multipledispatch.dispatcher import str_signature
|
397 |
+
>>> str_signature((int, float))
|
398 |
+
'int, float'
|
399 |
+
"""
|
400 |
+
return ', '.join(cls.__name__ for cls in sig)
|
401 |
+
|
402 |
+
|
403 |
+
def warning_text(name, amb):
|
404 |
+
""" The text for ambiguity warnings """
|
405 |
+
text = "\nAmbiguities exist in dispatched function %s\n\n" % (name)
|
406 |
+
text += "The following signatures may result in ambiguous behavior:\n"
|
407 |
+
for pair in amb:
|
408 |
+
text += "\t" + \
|
409 |
+
', '.join('[' + str_signature(s) + ']' for s in pair) + "\n"
|
410 |
+
text += "\n\nConsider making the following additions:\n\n"
|
411 |
+
text += '\n\n'.join(['@dispatch(' + str_signature(super_signature(s))
|
412 |
+
+ ')\ndef %s(...)' % name for s in amb])
|
413 |
+
return text
|
env-llmeval/lib/python3.10/site-packages/sympy/multipledispatch/tests/__init__.py
ADDED
File without changes
|
env-llmeval/lib/python3.10/site-packages/sympy/multipledispatch/tests/__pycache__/__init__.cpython-310.pyc
ADDED
Binary file (193 Bytes). View file
|
|
env-llmeval/lib/python3.10/site-packages/sympy/multipledispatch/tests/__pycache__/test_core.cpython-310.pyc
ADDED
Binary file (7.39 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/sympy/multipledispatch/tests/__pycache__/test_dispatcher.cpython-310.pyc
ADDED
Binary file (10.3 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/sympy/multipledispatch/tests/test_conflict.py
ADDED
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from sympy.multipledispatch.conflict import (supercedes, ordering, ambiguities,
|
2 |
+
ambiguous, super_signature, consistent)
|
3 |
+
|
4 |
+
|
5 |
+
class A: pass
|
6 |
+
class B(A): pass
|
7 |
+
class C: pass
|
8 |
+
|
9 |
+
|
10 |
+
def test_supercedes():
|
11 |
+
assert supercedes([B], [A])
|
12 |
+
assert supercedes([B, A], [A, A])
|
13 |
+
assert not supercedes([B, A], [A, B])
|
14 |
+
assert not supercedes([A], [B])
|
15 |
+
|
16 |
+
|
17 |
+
def test_consistent():
|
18 |
+
assert consistent([A], [A])
|
19 |
+
assert consistent([B], [B])
|
20 |
+
assert not consistent([A], [C])
|
21 |
+
assert consistent([A, B], [A, B])
|
22 |
+
assert consistent([B, A], [A, B])
|
23 |
+
assert not consistent([B, A], [B])
|
24 |
+
assert not consistent([B, A], [B, C])
|
25 |
+
|
26 |
+
|
27 |
+
def test_super_signature():
|
28 |
+
assert super_signature([[A]]) == [A]
|
29 |
+
assert super_signature([[A], [B]]) == [B]
|
30 |
+
assert super_signature([[A, B], [B, A]]) == [B, B]
|
31 |
+
assert super_signature([[A, A, B], [A, B, A], [B, A, A]]) == [B, B, B]
|
32 |
+
|
33 |
+
|
34 |
+
def test_ambiguous():
|
35 |
+
assert not ambiguous([A], [A])
|
36 |
+
assert not ambiguous([A], [B])
|
37 |
+
assert not ambiguous([B], [B])
|
38 |
+
assert not ambiguous([A, B], [B, B])
|
39 |
+
assert ambiguous([A, B], [B, A])
|
40 |
+
|
41 |
+
|
42 |
+
def test_ambiguities():
|
43 |
+
signatures = [[A], [B], [A, B], [B, A], [A, C]]
|
44 |
+
expected = {((A, B), (B, A))}
|
45 |
+
result = ambiguities(signatures)
|
46 |
+
assert set(map(frozenset, expected)) == set(map(frozenset, result))
|
47 |
+
|
48 |
+
signatures = [[A], [B], [A, B], [B, A], [A, C], [B, B]]
|
49 |
+
expected = set()
|
50 |
+
result = ambiguities(signatures)
|
51 |
+
assert set(map(frozenset, expected)) == set(map(frozenset, result))
|
52 |
+
|
53 |
+
|
54 |
+
def test_ordering():
|
55 |
+
signatures = [[A, A], [A, B], [B, A], [B, B], [A, C]]
|
56 |
+
ord = ordering(signatures)
|
57 |
+
assert ord[0] == (B, B) or ord[0] == (A, C)
|
58 |
+
assert ord[-1] == (A, A) or ord[-1] == (A, C)
|
59 |
+
|
60 |
+
|
61 |
+
def test_type_mro():
|
62 |
+
assert super_signature([[object], [type]]) == [type]
|