def test_intersection(): poly1 = Triangle(Point(0, 0), Point(1, 0), Point(0, 1)) poly2 = Polygon(Point(0, 1), Point(-5, 0), Point(0, -4), Point(0, Rational(1, 5)), Point(S.Half, -0.1), Point(1, 0), Point(0, 1)) assert poly1.intersection(poly2) == [Point2D(Rational(1, 3), 0), Segment(Point(0, Rational(1, 5)), Point(0, 0)), Segment(Point(1, 0), Point(0, 1))] assert poly2.intersection(poly1) == [Point(Rational(1, 3), 0), Segment(Point(0, 0), Point(0, Rational(1, 5))), Segment(Point(1, 0), Point(0, 1))] assert poly1.intersection(Point(0, 0)) == [Point(0, 0)] assert poly1.intersection(Point(-12, -43)) == [] assert poly2.intersection(Line((-12, 0), (12, 0))) == [Point(-5, 0), Point(0, 0), Point(Rational(1, 3), 0), Point(1, 0)] assert poly2.intersection(Line((-12, 12), (12, 12))) == [] assert poly2.intersection(Ray((-3, 4), (1, 0))) == [Segment(Point(1, 0), Point(0, 1))] assert poly2.intersection(Circle((0, -1), 1)) == [Point(0, -2), Point(0, 0)] assert poly1.intersection(poly1) == [Segment(Point(0, 0), Point(1, 0)), Segment(Point(0, 1), Point(0, 0)), Segment(Point(1, 0), Point(0, 1))] assert poly2.intersection(poly2) == [Segment(Point(-5, 0), Point(0, -4)), Segment(Point(0, -4), Point(0, Rational(1, 5))), Segment(Point(0, Rational(1, 5)), Point(S.Half, Rational(-1, 10))), Segment(Point(0, 1), Point(-5, 0)), Segment(Point(S.Half, Rational(-1, 10)), Point(1, 0)), Segment(Point(1, 0), Point(0, 1))] assert poly2.intersection(Triangle(Point(0, 1), Point(1, 0), Point(-1, 1))) \ == [Point(Rational(-5, 7), Rational(6, 7)), Segment(Point2D(0, 1), Point(1, 0))] assert poly1.intersection(RegularPolygon((-12, -15), 3, 3)) == []
def test_eulerline(): assert Triangle(Point(0, 0), Point(1, 0), Point(0, 1)).eulerline \ == Line(Point2D(0, 0), Point2D(1/2, 1/2)) assert Triangle(Point(0, 0), Point(10, 0), Point(5, 5*sqrt(3))).eulerline \ == Point2D(5, 5*sqrt(3)/3) assert Triangle(Point(4, -6), Point(4, -1), Point(-3, 3)).eulerline \ == Line(Point2D(64/7, 3), Point2D(-29/14, -7/2))
def test_projection(): p1 = Point(0, 0) p2 = Point3D(0, 0, 0) p3 = Point(-x1, x1) l1 = Line(p1, Point(1, 1)) l2 = Line3D(Point3D(0, 0, 0), Point3D(1, 0, 0)) l3 = Line3D(p2, Point3D(1, 1, 1)) r1 = Ray(Point(1, 1), Point(2, 2)) s1 = Segment(Point2D(0, 0), Point2D(0, 1)) s2 = Segment(Point2D(1, 0), Point2D(2, 1/2)) assert Line(Point(x1, x1), Point(y1, y1)).projection(Point(y1, y1)) == Point(y1, y1) assert Line(Point(x1, x1), Point(x1, 1 + x1)).projection(Point(1, 1)) == Point(x1, 1) assert Segment(Point(-2, 2), Point(0, 4)).projection(r1) == Segment(Point(-1, 3), Point(0, 4)) assert Segment(Point(0, 4), Point(-2, 2)).projection(r1) == Segment(Point(0, 4), Point(-1, 3)) assert s2.projection(s1) == EmptySet assert l1.projection(p3) == p1 assert l1.projection(Ray(p1, Point(-1, 5))) == Ray(Point(0, 0), Point(2, 2)) assert l1.projection(Ray(p1, Point(-1, 1))) == p1 assert r1.projection(Ray(Point(1, 1), Point(-1, -1))) == Point(1, 1) assert r1.projection(Ray(Point(0, 4), Point(-1, -5))) == Segment(Point(1, 1), Point(2, 2)) assert r1.projection(Segment(Point(-1, 5), Point(-5, -10))) == Segment(Point(1, 1), Point(2, 2)) assert r1.projection(Ray(Point(1, 1), Point(-1, -1))) == Point(1, 1) assert r1.projection(Ray(Point(0, 4), Point(-1, -5))) == Segment(Point(1, 1), Point(2, 2)) assert r1.projection(Segment(Point(-1, 5), Point(-5, -10))) == Segment(Point(1, 1), Point(2, 2)) assert l3.projection(Ray3D(p2, Point3D(-1, 5, 0))) == Ray3D(Point3D(0, 0, 0), Point3D(Rational(4, 3), Rational(4, 3), Rational(4, 3))) assert l3.projection(Ray3D(p2, Point3D(-1, 1, 1))) == Ray3D(Point3D(0, 0, 0), Point3D(Rational(1, 3), Rational(1, 3), Rational(1, 3))) assert l2.projection(Point3D(5, 5, 0)) == Point3D(5, 0) assert l2.projection(Line3D(Point3D(0, 1, 0), Point3D(1, 1, 0))).equals(l2)
def test_eulerline(): assert Triangle(Point(0, 0), Point(1, 0), Point(0, 1)).eulerline \ == Line(Point2D(0, 0), Point2D(S(1)/2, S(1)/2)) assert Triangle(Point(0, 0), Point(10, 0), Point(5, 5*sqrt(3))).eulerline \ == Point2D(5, 5*sqrt(3)/3) assert Triangle(Point(4, -6), Point(4, -1), Point(-3, 3)).eulerline \ == Line(Point2D(S(64)/7, 3), Point2D(-S(29)/14, -S(7)/2))
def test_intersection(): poly1 = Triangle(Point(0, 0), Point(1, 0), Point(0, 1)) poly2 = Polygon(Point(0, 1), Point(-5, 0), Point(0, -4), Point(0, S(1)/5), Point(S(1)/2, -0.1), Point(1,0), Point(0, 1)) assert poly1.intersection(poly2) == [Point2D(S(1)/3, 0), Segment(Point(0, S(1)/5), Point(0, 0)), Segment(Point(1, 0), Point(0, 1))] assert poly2.intersection(poly1) == [Point(S(1)/3, 0), Segment(Point(0, 0), Point(0, S(1)/5)), Segment(Point(1, 0), Point(0, 1))] assert poly1.intersection(Point(0, 0)) == [Point(0, 0)] assert poly1.intersection(Point(-12, -43)) == [] assert poly2.intersection(Line((-12, 0), (12, 0))) == [Point(-5, 0), Point(0, 0),Point(S(1)/3, 0), Point(1, 0)] assert poly2.intersection(Line((-12, 12), (12, 12))) == [] assert poly2.intersection(Ray((-3,4), (1,0))) == [Segment(Point(1, 0), Point(0, 1))] assert poly2.intersection(Circle((0, -1), 1)) == [Point(0, -2), Point(0, 0)] assert poly1.intersection(poly1) == [Segment(Point(0, 0), Point(1, 0)), Segment(Point(0, 1), Point(0, 0)), Segment(Point(1, 0), Point(0, 1))] assert poly2.intersection(poly2) == [Segment(Point(-5, 0), Point(0, -4)), Segment(Point(0, -4), Point(0, S(1)/5)), Segment(Point(0, S(1)/5), Point(S(1)/2, -S(1)/10)), Segment(Point(0, 1), Point(-5, 0)), Segment(Point(S(1)/2, -S(1)/10), Point(1, 0)), Segment(Point(1, 0), Point(0, 1))] assert poly2.intersection(Triangle(Point(0, 1), Point(1, 0), Point(-1, 1))) \ == [Point(-S(5)/7, S(6)/7), Segment(Point2D(0, 1), Point(1, 0))] assert poly1.intersection(RegularPolygon((-12, -15), 3, 3)) == []
def test_arguments(): """Functions accepting `Point` objects in `geometry` should also accept tuples, lists, and generators and automatically convert them to points.""" from sympy import subsets singles2d = ((1, 2), [1, 3], Point(1, 5)) doubles2d = subsets(singles2d, 2) l2d = Line(Point2D(1, 2), Point2D(2, 3)) singles3d = ((1, 2, 3), [1, 2, 4], Point(1, 2, 6)) doubles3d = subsets(singles3d, 2) l3d = Line(Point3D(1, 2, 3), Point3D(1, 1, 2)) singles4d = ((1, 2, 3, 4), [1, 2, 3, 5], Point(1, 2, 3, 7)) doubles4d = subsets(singles4d, 2) l4d = Line(Point(1, 2, 3, 4), Point(2, 2, 2, 2)) # test 2D test_single = ['contains', 'distance', 'equals', 'parallel_line', 'perpendicular_line', 'perpendicular_segment', 'projection', 'intersection'] for p in doubles2d: Line2D(*p) for func in test_single: for p in singles2d: getattr(l2d, func)(p) # test 3D for p in doubles3d: Line3D(*p) for func in test_single: for p in singles3d: getattr(l3d, func)(p) # test 4D for p in doubles4d: Line(*p) for func in test_single: for p in singles4d: getattr(l4d, func)(p)
def test_eulerline(): assert Triangle(Point(0, 0), Point(1, 0), Point(0, 1)).eulerline \ == Line(Point2D(0, 0), Point2D(S.Half, S.Half)) assert Triangle(Point(0, 0), Point(10, 0), Point(5, 5*sqrt(3))).eulerline \ == Point2D(5, 5*sqrt(3)/3) assert Triangle(Point(4, -6), Point(4, -1), Point(-3, 3)).eulerline \ == Line(Point2D(Rational(64, 7), 3), Point2D(Rational(-29, 14), Rational(-7, 2)))
def intersection_with_circle(self, point1, point2): r = self.r_small_circle R = self.big_circle[2] try: # Point A, B and C A = Point2D(self.big_circle[0], self.big_circle[1]) B = Point2D(point1[0], point1[1]) C = Point2D(point2[0], point2[1]) # Segment from B to C - line line = Segment2D(B, C) c = Circle(A, R + r) result_of_intersect = c.intersection(line) # print(result_of_intersect) temp1 = result_of_intersect[0] temp2 = result_of_intersect[1] temp1 = str(temp1)[7:] temp2 = str(temp2)[7:] # print(temp1) # print(temp2) # print(eval(temp1)) # print(eval(temp2)) p1 = eval(temp1) p2 = eval(temp2) # point1 = mlines.Line2D([p1[0], p2[0]], [p1[1], p2[1]], color='b') # self.figure.gca().add_line(point1) return p1, p2 except: return -666
def test_are_coplanar(): a = Line3D(Point3D(5, 0, 0), Point3D(1, -1, 1)) b = Line3D(Point3D(0, -2, 0), Point3D(3, 1, 1)) c = Line3D(Point3D(0, -1, 0), Point3D(5, -1, 9)) d = Line(Point2D(0, 3), Point2D(1, 5)) assert are_coplanar(a, b, c) == False assert are_coplanar(a, d) == False
def test_Point2D(): # Test Distance p1 = Point2D(1, 5) p2 = Point2D(4, 2.5) p3 = (6, 3) assert p1.distance(p2) == sqrt(61) / 2 assert p2.distance(p3) == sqrt(17) / 2
def demo(ctx, width, height): map = Map('demo', width, height, cell_size=CELL_SIZE) map.add_room(Room('start', Point2D(width / 4, height / 4))) map.add_room(Room('mid1', Point2D(width / 2 + width / 4, height / 2))) map.add_room(Room('mid2', Point2D(width / 2, height / 2 + height / 4))) map.add_room( Room('end', Point2D(width / 2 + width / 4, height / 2 + height / 4))) map.add_path('start', 'mid1', flags=['arc']) map.add_path('start', 'mid2', flags=['door', 'trapped']) map.add_path('mid1', 'end', flags=['door']) print(map) map.draw(ctx)
def getPointOfIntersection(extreme_points): '''This function returns a unique point of intersection (if it exists) between four points in 2D plane. Input: extreme_points: (4,2) numpy array containing (x,y) coordinates of four points Output: intersection_point: A list containing [xint, yint] NOTE: This function errors out (ValueError) unless the intersection is a unique point. Implement error catching if this is undesired.''' assert isinstance( extreme_points, np.ndarray), "Exteme points should be passed as an ndarray" assert extreme_points.shape == ( 4, 2), "Extreme point array shape must be (4,2)" # We try a random pairing based search. We make three attempts # to try unique pairings of fours points and look for the combination that gives # a unique intersection point. pairings = [[0, 1, 2, 3], [0, 2, 1, 3], [0, 3, 1, 2]] intersection_found = False i = 0 pairs = [] while intersection_found is not True and i < 3: pairs = pairings[i] x1 = Point2D(extreme_points[pairs[0], :]) x2 = Point2D(extreme_points[pairs[1], :]) x3 = Point2D(extreme_points[pairs[2], :]) x4 = Point2D(extreme_points[pairs[3], :]) # We use segment over line to ensure the intersection point lies within # the bounding box defined by the extreme points intersection_point = intersection(Segment2D(x1, x2), Segment2D(x3, x4)) # Ensure that intersection point is a unique point and not a line or empty if not intersection_point == []: if isinstance(intersection_point[0], Point2D): intersection_found = True xint, yint = intersection_point[0] i = i + 1 if intersection_found is not True: raise ValueError( "No intersection point was found for given extreme points using random pairing. Check" ) intersection_point = np.array( [np.float128(xint.evalf()), np.float128(yint.evalf())]) pairs = np.array(pairs) return intersection_point, pairs
def test_object_from_equation(): from sympy.abc import x, y, a, b assert Line(3*x + y + 18) == Line2D(Point2D(0, -18), Point2D(1, -21)) assert Line(3*x + 5 * y + 1) == Line2D(Point2D(0, -1/5), Point2D(1, -4/5)) assert Line(3*a + b + 18, x='a', y='b') == Line2D(Point2D(0, -18), Point2D(1, -21)) assert Line(3*x + y) == Line2D(Point2D(0, 0), Point2D(1, -3)) assert Line(x + y) == Line2D(Point2D(0, 0), Point2D(1, -1)) raises(ValueError, lambda: Line(x)) raises(ValueError, lambda: Line(y)) raises(ValueError, lambda: Line(x/y)) raises(ValueError, lambda: Line(a/b, x='a', y='b')) raises(ValueError, lambda: Line(y/x)) raises(ValueError, lambda: Line(b/a, x='a', y='b')) raises(ValueError, lambda: Line((x + 1)**2 + y))
def get_rectangular_coordinates(self, pos: geom.Point2D): """ Calculate the model coordinates of the pos point inside the init table. """ def get_coeff(p, x, x_len, a, b, c, d): if p is not None: k = geom.intersection(geom.Line2D(p, pos), self.line)[0] return x.distance(k) / x_len k1 = a.distance(d) k2 = b.distance(c) ln = geom.Line2D(a, d).parallel_line(pos) g1 = geom.intersection(ln, geom.Line2D(a, b))[0] g2 = geom.intersection(ln, geom.Line2D(c, d))[0] k3 = g1.distance(g2) return k2 * (k3 - k1) / (k3 * (k2 - k1)) if self.P is None and self.Q is None: l1 = geom.Line2D(self.A, self.D) l2 = geom.Line2D(self.C, self.D) x_coeff = pos.distance(l1) / self.B.distance(l1) y_coeff = pos.distance(l2) / self.B.distance(l2) else: x_coeff = get_coeff(self.Q, self.X1, self.x_len, self.A, self.B, self.C, self.D) y_coeff = get_coeff(self.P, self.Y1, self.y_len, self.D, self.A, self.B, self.C) x = round(x_coeff * self.img_sz[0]) y = round(y_coeff * self.img_sz[1]) return x, y
def find_intersect_circle_and_line(self, x1, y1, x2, y2, x3, y3, r): A = Point2D(x1, y1) B = Point2D(x2, y2) C = Point2D(x3, y3) F = Line2D(A, B) c = Circle(C, sympify(Rational(r), rational=True)) i_0 = F.intersection(c) temp1 = i_0[0] temp2 = i_0[1] temp1 = str(temp1)[7:] temp2 = str(temp2)[7:] temp1 = eval(temp1) temp2 = eval(temp2) x = (temp1[0] + temp2[0]) / 2 y = (temp1[1] + temp2[1]) / 2 return [x, y]
def test_issue_11617(): p1 = Point3D(1, 0, 2) p2 = Point2D(2, 0) with warnings.catch_warnings(record=True) as w: assert p1.distance(p2) == sqrt(5) assert len(w) == 1
def get_dist_from_line_segment(x1, c, x2, p, sig1, sig2): '''Given three colinear points, x1, c, x2 (geometric order), this program checks if point p and x1 are on the same side of a perpendicular to line segment x1x2 drawn at c. If p and x1 are on the same side of perpendicular line, the distance of p from line segment x1x2 is divided by sig1 (for scaling) or by sig2 otherwise. Inputs: x1, c, x2, p: Np.array of size 2. sig1, sig2: scaling standard deviation Output: l1: Equivalent of l1 distance from the line segment x1x2 (scaled by sigma)''' assert isinstance_multiple( [x1, x2, c], np.ndarray), "Either x1,x2 or c isn't a np.ndarray, check" assert x1.size == x2.size == c.size == 2, "x1, x2, c must contain ony 2 elements" assert isinstance_multiple( [sig1, sig2], float), "Either sig1 or sig2 is not of type float, check" assert not (sig1 < 0.001 or sig2 < 0.001), "sig1, sig2 is smaller than 0.001 (eps), check" x1, x2, c, p = [Point2D(i) for i in [x1, x2, c, p]] S = Segment2D(x1, x2) L = S.perpendicular_line(c) Sc = Segment2D(x1, p) l1 = S.distance(p).evalf() if isinstance(L.intersect(Sc), EmptySet): l1 = l1 / sig1 else: l1 = l1 / sig2 return float(l1)
def test_Point2D(): # Test Distance p1 = Point2D(1, 5) p2 = Point2D(4, 2.5) p3 = (6, 3) assert p1.distance(p2) == sqrt(61) / 2 assert p2.distance(p3) == sqrt(17) / 2 # Test coordinates assert p1.x == 1 assert p1.y == 5 assert p2.x == 4 assert p2.y == 2.5 assert p1.coordinates == (1, 5) assert p2.coordinates == (4, 2.5)
def test_transform(): pts = [Point(0, 0), Point(S.Half, Rational(1, 4)), Point(1, 1)] pts_out = [Point(-4, -10), Point(-3, Rational(-37, 4)), Point(-2, -7)] assert Triangle(*pts).scale(2, 3, (4, 5)) == Triangle(*pts_out) assert RegularPolygon((0, 0), 1, 4).scale(2, 3, (4, 5)) == \ Polygon(Point(-2, -10), Point(-4, -7), Point(-6, -10), Point(-4, -13)) # Checks for symmetric scaling assert RegularPolygon((0, 0), 1, 4).scale(2, 2) == \ RegularPolygon(Point2D(0, 0), 2, 4, 0)
def test_bisectors(): p1, p2, p3 = Point(0, 0), Point(1, 0), Point(0, 1) p = Polygon(Point(0, 0), Point(2, 0), Point(1, 1), Point(0, 3)) q = Polygon(Point(1, 0), Point(2, 0), Point(3, 3), Point(-1, 5)) poly = Polygon(Point(3, 4), Point(0, 0), Point(8, 7), Point(-1, 1), Point(19, -19)) t = Triangle(p1, p2, p3) assert t.bisectors()[p2] == Segment(Point(1, 0), Point(0, sqrt(2) - 1)) assert p.bisectors()[Point2D(0, 3)] == Ray2D(Point2D(0, 3), \ Point2D(sin(acos(2*sqrt(5)/5)/2), 3 - cos(acos(2*sqrt(5)/5)/2))) assert q.bisectors()[Point2D(-1, 5)] == \ Ray2D(Point2D(-1, 5), Point2D(-1 + sqrt(29)*(5*sin(acos(9*sqrt(145)/145)/2) + \ 2*cos(acos(9*sqrt(145)/145)/2))/29, sqrt(29)*(-5*cos(acos(9*sqrt(145)/145)/2) + \ 2*sin(acos(9*sqrt(145)/145)/2))/29 + 5)) assert poly.bisectors()[Point2D(-1, 1)] == Ray2D(Point2D(-1, 1), \ Point2D(-1 + sin(acos(sqrt(26)/26)/2 + pi/4), 1 - sin(-acos(sqrt(26)/26)/2 + pi/4)))
def test_geometry(): def do_test(*g, s=GeometrySeries, **kwargs): s1 = _build_series(*g, pt="g", **kwargs) assert isinstance(s1, s) # since the range could be None, it is imperative to test that label # receive the correct value. assert s1.label == str(g[0]) s2 = _build_series(*g, **kwargs) assert isinstance(s2, s) assert s2.label == str(g[0]) assert np.array_equal(s1.get_data(), s2.get_data(), equal_nan=True) x, y, z = symbols("x, y, z") do_test(Point2D(1, 2)) do_test(Point3D(1, 2, 3)) do_test(Ray((1, 2), (3, 4))) do_test(Segment((1, 2), (3, 4))) do_test(Line((1, 2), (3, 4)), (x, -5, 5)) do_test(Ray3D((1, 2, 3), (3, 4, 5))) do_test(Segment3D((1, 2, 3), (3, 4, 5))) do_test(Line3D((1, 2, 3), (3, 4, 5))) do_test(Polygon((1, 2), 3, n=10)) do_test(Circle((1, 2), 3)) do_test(Ellipse((1, 2), hradius=3, vradius=2)) do_test(Plane((0, 0, 0), (1, 1, 1)), (x, -5, 5), (y, -4, 4), (z, -3, 3), s=PlaneSeries) # Interactive series. Note that GeometryInteractiveSeries is an instance of # GeometrySeries do_test(Point2D(x, y), params={x: 1, y: 2}) do_test( Plane((x, y, z), (1, 1, 1)), (x, -5, 5), (y, -4, 4), (z, -3, 3), params={ x: 1, y: 2, z: 3 }, s=PlaneInteractiveSeries, )
def test_convex_hull(): raises(TypeError, lambda: convex_hull(Point(0, 0), 3)) points = [(1, -1), (1, -2), (3, -1), (-5, -2), (15, -4)] assert convex_hull(*points, **dict(polygon=False)) == ([ Point2D(-5, -2), Point2D(1, -1), Point2D(3, -1), Point2D(15, -4) ], [Point2D(-5, -2), Point2D(15, -4)])
def test_arguments(): """Functions accepting `Point` objects in `geometry` should also accept tuples and lists and automatically convert them to points.""" singles2d = ((1, 2), [1, 2], Point(1, 2)) singles2d2 = ((1, 3), [1, 3], Point(1, 3)) doubles2d = cartes(singles2d, singles2d2) p2d = Point2D(1, 2) singles3d = ((1, 2, 3), [1, 2, 3], Point(1, 2, 3)) doubles3d = subsets(singles3d, 2) p3d = Point3D(1, 2, 3) singles4d = ((1, 2, 3, 4), [1, 2, 3, 4], Point(1, 2, 3, 4)) doubles4d = subsets(singles4d, 2) p4d = Point(1, 2, 3, 4) # test 2D test_single = [ 'distance', 'is_scalar_multiple', 'taxicab_distance', 'midpoint', 'intersection', 'dot', 'equals', '__add__', '__sub__' ] test_double = ['is_concyclic', 'is_collinear'] for p in singles2d: Point2D(p) for func in test_single: for p in singles2d: getattr(p2d, func)(p) for func in test_double: for p in doubles2d: getattr(p2d, func)(*p) # test 3D test_double = ['is_collinear'] for p in singles3d: Point3D(p) for func in test_single: for p in singles3d: getattr(p3d, func)(p) for func in test_double: for p in doubles3d: getattr(p3d, func)(*p) # test 4D test_double = ['is_collinear'] for p in singles4d: Point(p) for func in test_single: for p in singles4d: getattr(p4d, func)(p) for func in test_double: for p in doubles4d: getattr(p4d, func)(*p) # test evaluate=False for ops x = Symbol('x') a = Point(0, 1) assert a + (0.1, x) == Point(0.1, 1 + x, evaluate=False) a = Point(0, 1) assert a / 10.0 == Point(0, 0.1, evaluate=False) a = Point(0, 1) assert a * 10.0 == Point(0.0, 10.0, evaluate=False) # test evaluate=False when changing dimensions u = Point(.1, .2, evaluate=False) u4 = Point(u, dim=4, on_morph='ignore') assert u4.args == (.1, .2, 0, 0) assert all(i.is_Float for i in u4.args[:2]) # and even when *not* changing dimensions assert all(i.is_Float for i in Point(u).args) # never raise error if creating an origin assert Point(dim=3, on_morph='error')
def test_issue_11617(): p1 = Point3D(1, 0, 2) p2 = Point2D(2, 0) with warns(UserWarning): assert p1.distance(p2) == sqrt(5)
def point_from_str(point_str): x, y = point_str.split(',') return Point2D(int(x), int(y))
def test_point3D(): x = Symbol('x', real=True) y = Symbol('y', real=True) x1 = Symbol('x1', real=True) x2 = Symbol('x2', real=True) x3 = Symbol('x3', real=True) y1 = Symbol('y1', real=True) y2 = Symbol('y2', real=True) y3 = Symbol('y3', real=True) half = S.Half p1 = Point3D(x1, x2, x3) p2 = Point3D(y1, y2, y3) p3 = Point3D(0, 0, 0) p4 = Point3D(1, 1, 1) p5 = Point3D(0, 1, 2) assert p1 in p1 assert p1 not in p2 assert p2.y == y2 assert (p3 + p4) == p4 assert (p2 - p1) == Point3D(y1 - x1, y2 - x2, y3 - x3) assert -p2 == Point3D(-y1, -y2, -y3) assert Point(34.05, sqrt(3)) == Point(Rational(681, 20), sqrt(3)) assert Point3D.midpoint(p3, p4) == Point3D(half, half, half) assert Point3D.midpoint(p1, p4) == Point3D(half + half * x1, half + half * x2, half + half * x3) assert Point3D.midpoint(p2, p2) == p2 assert p2.midpoint(p2) == p2 assert Point3D.distance(p3, p4) == sqrt(3) assert Point3D.distance(p1, p1) == 0 assert Point3D.distance(p3, p2) == sqrt(p2.x**2 + p2.y**2 + p2.z**2) p1_1 = Point3D(x1, x1, x1) p1_2 = Point3D(y2, y2, y2) p1_3 = Point3D(x1 + 1, x1, x1) Point3D.are_collinear(p3) assert Point3D.are_collinear(p3, p4) assert Point3D.are_collinear(p3, p4, p1_1, p1_2) assert Point3D.are_collinear(p3, p4, p1_1, p1_3) is False assert Point3D.are_collinear(p3, p3, p4, p5) is False assert p3.intersection(Point3D(0, 0, 0)) == [p3] assert p3.intersection(p4) == [] assert p4 * 5 == Point3D(5, 5, 5) assert p4 / 5 == Point3D(0.2, 0.2, 0.2) assert 5 * p4 == Point3D(5, 5, 5) raises(ValueError, lambda: Point3D(0, 0, 0) + 10) # Test coordinate properties assert p1.coordinates == (x1, x2, x3) assert p2.coordinates == (y1, y2, y3) assert p3.coordinates == (0, 0, 0) assert p4.coordinates == (1, 1, 1) assert p5.coordinates == (0, 1, 2) assert p5.x == 0 assert p5.y == 1 assert p5.z == 2 # Point differences should be simplified assert Point3D(x*(x - 1), y, 2) - Point3D(x**2 - x, y + 1, 1) == \ Point3D(0, -1, 1) a, b, c = S.Half, Rational(1, 3), Rational(1, 4) assert Point3D(a, b, c).evalf(2) == \ Point(a.n(2), b.n(2), c.n(2), evaluate=False) raises(ValueError, lambda: Point3D(1, 2, 3) + 1) # test transformations p = Point3D(1, 1, 1) assert p.scale(2, 3) == Point3D(2, 3, 1) assert p.translate(1, 2) == Point3D(2, 3, 1) assert p.translate(1) == Point3D(2, 1, 1) assert p.translate(z=1) == Point3D(1, 1, 2) assert p.translate(*p.args) == Point3D(2, 2, 2) # Test __new__ assert Point3D(0.1, 0.2, evaluate=False, on_morph='ignore').args[0].is_Float # Test length property returns correctly assert p.length == 0 assert p1_1.length == 0 assert p1_2.length == 0 # Test are_colinear type error raises(TypeError, lambda: Point3D.are_collinear(p, x)) # Test are_coplanar assert Point.are_coplanar() assert Point.are_coplanar((1, 2, 0), (1, 2, 0), (1, 3, 0)) assert Point.are_coplanar((1, 2, 0), (1, 2, 3)) with warns(UserWarning): raises(ValueError, lambda: Point2D.are_coplanar((1, 2), (1, 2, 3))) assert Point3D.are_coplanar((1, 2, 0), (1, 2, 3)) assert Point.are_coplanar((0, 0, 0), (1, 1, 0), (1, 1, 1), (1, 2, 1)) is False planar2 = Point3D(1, -1, 1) planar3 = Point3D(-1, 1, 1) assert Point3D.are_coplanar(p, planar2, planar3) == True assert Point3D.are_coplanar(p, planar2, planar3, p3) == False assert Point.are_coplanar(p, planar2) planar2 = Point3D(1, 1, 2) planar3 = Point3D(1, 1, 3) assert Point3D.are_coplanar(p, planar2, planar3) # line, not plane plane = Plane((1, 2, 1), (2, 1, 0), (3, 1, 2)) assert Point.are_coplanar( *[plane.projection(((-1)**i, i)) for i in range(4)]) # all 2D points are coplanar assert Point.are_coplanar(Point(x, y), Point(x, x + y), Point( y, x + 2)) is True # Test Intersection assert planar2.intersection(Line3D(p, planar3)) == [Point3D(1, 1, 2)] # Test Scale assert planar2.scale(1, 1, 1) == planar2 assert planar2.scale(2, 2, 2, planar3) == Point3D(1, 1, 1) assert planar2.scale(1, 1, 1, p3) == planar2 # Test Transform identity = Matrix([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]]) assert p.transform(identity) == p trans = Matrix([[1, 0, 0, 1], [0, 1, 0, 1], [0, 0, 1, 1], [0, 0, 0, 1]]) assert p.transform(trans) == Point3D(2, 2, 2) raises(ValueError, lambda: p.transform(p)) raises(ValueError, lambda: p.transform(Matrix([[1, 0], [0, 1]]))) # Test Equals assert p.equals(x1) == False # Test __sub__ p_4d = Point(0, 0, 0, 1) with warns(UserWarning): assert p - p_4d == Point(1, 1, 1, -1) p_4d3d = Point(0, 0, 1, 0) with warns(UserWarning): assert p - p_4d3d == Point(1, 1, 0, 0)
def test_object_from_equation(): from sympy.abc import x, y, a, b assert Line(3 * x + y + 18) == Line2D(Point2D(0, -18), Point2D(1, -21)) assert Line(3 * x + 5 * y + 1) == Line2D(Point2D(0, Rational(-1, 5)), Point2D(1, Rational(-4, 5))) assert Line(3 * a + b + 18, x="a", y="b") == Line2D(Point2D(0, -18), Point2D(1, -21)) assert Line(3 * x + y) == Line2D(Point2D(0, 0), Point2D(1, -3)) assert Line(x + y) == Line2D(Point2D(0, 0), Point2D(1, -1)) assert Line(Eq(3 * a + b, -18), x="a", y=b) == Line2D(Point2D(0, -18), Point2D(1, -21)) raises(ValueError, lambda: Line(x)) raises(ValueError, lambda: Line(y)) raises(ValueError, lambda: Line(x / y)) raises(ValueError, lambda: Line(a / b, x="a", y="b")) raises(ValueError, lambda: Line(y / x)) raises(ValueError, lambda: Line(b / a, x="a", y="b")) raises(ValueError, lambda: Line((x + 1)**2 + y))
def test_point3D(): x = Symbol('x', real=True) y = Symbol('y', real=True) x1 = Symbol('x1', real=True) x2 = Symbol('x2', real=True) x3 = Symbol('x3', real=True) y1 = Symbol('y1', real=True) y2 = Symbol('y2', real=True) y3 = Symbol('y3', real=True) half = Rational(1, 2) p1 = Point3D(x1, x2, x3) p2 = Point3D(y1, y2, y3) p3 = Point3D(0, 0, 0) p4 = Point3D(1, 1, 1) p5 = Point3D(0, 1, 2) assert p1 in p1 assert p1 not in p2 assert p2.y == y2 assert (p3 + p4) == p4 assert (p2 - p1) == Point3D(y1 - x1, y2 - x2, y3 - x3) assert p4*5 == Point3D(5, 5, 5) assert -p2 == Point3D(-y1, -y2, -y3) assert Point(34.05, sqrt(3)) == Point(Rational(681, 20), sqrt(3)) assert Point3D.midpoint(p3, p4) == Point3D(half, half, half) assert Point3D.midpoint(p1, p4) == Point3D(half + half*x1, half + half*x2, half + half*x3) assert Point3D.midpoint(p2, p2) == p2 assert p2.midpoint(p2) == p2 assert Point3D.distance(p3, p4) == sqrt(3) assert Point3D.distance(p1, p1) == 0 assert Point3D.distance(p3, p2) == sqrt(p2.x**2 + p2.y**2 + p2.z**2) p1_1 = Point3D(x1, x1, x1) p1_2 = Point3D(y2, y2, y2) p1_3 = Point3D(x1 + 1, x1, x1) Point3D.are_collinear(p3) assert Point3D.are_collinear(p3, p4) assert Point3D.are_collinear(p3, p4, p1_1, p1_2) assert Point3D.are_collinear(p3, p4, p1_1, p1_3) is False assert Point3D.are_collinear(p3, p3, p4, p5) is False assert p3.intersection(Point3D(0, 0, 0)) == [p3] assert p3.intersection(p4) == [] assert p4 * 5 == Point3D(5, 5, 5) assert p4 / 5 == Point3D(0.2, 0.2, 0.2) raises(ValueError, lambda: Point3D(0, 0, 0) + 10) # Point differences should be simplified assert Point3D(x*(x - 1), y, 2) - Point3D(x**2 - x, y + 1, 1) == \ Point3D(0, -1, 1) a, b = Rational(1, 2), Rational(1, 3) assert Point(a, b).evalf(2) == \ Point(a.n(2), b.n(2)) raises(ValueError, lambda: Point(1, 2) + 1) # test transformations p = Point3D(1, 1, 1) assert p.scale(2, 3) == Point3D(2, 3, 1) assert p.translate(1, 2) == Point3D(2, 3, 1) assert p.translate(1) == Point3D(2, 1, 1) assert p.translate(z=1) == Point3D(1, 1, 2) assert p.translate(*p.args) == Point3D(2, 2, 2) # Test __new__ assert Point3D(0.1, 0.2, evaluate=False, on_morph='ignore').args[0].is_Float # Test length property returns correctly assert p.length == 0 assert p1_1.length == 0 assert p1_2.length == 0 # Test are_colinear type error raises(TypeError, lambda: Point3D.are_collinear(p, x)) # Test are_coplanar assert Point.are_coplanar() assert Point.are_coplanar((1, 2, 0), (1, 2, 0), (1, 3, 0)) assert Point.are_coplanar((1, 2, 0), (1, 2, 3)) with warnings.catch_warnings(record=True) as w: raises(ValueError, lambda: Point2D.are_coplanar((1, 2), (1, 2, 3))) assert Point3D.are_coplanar((1, 2, 0), (1, 2, 3)) assert Point.are_coplanar((0, 0, 0), (1, 1, 0), (1, 1, 1), (1, 2, 1)) is False planar2 = Point3D(1, -1, 1) planar3 = Point3D(-1, 1, 1) assert Point3D.are_coplanar(p, planar2, planar3) == True assert Point3D.are_coplanar(p, planar2, planar3, p3) == False assert Point.are_coplanar(p, planar2) planar2 = Point3D(1, 1, 2) planar3 = Point3D(1, 1, 3) assert Point3D.are_coplanar(p, planar2, planar3) # line, not plane plane = Plane((1, 2, 1), (2, 1, 0), (3, 1, 2)) assert Point.are_coplanar(*[plane.projection(((-1)**i, i)) for i in range(4)]) # all 2D points are coplanar assert Point.are_coplanar(Point(x, y), Point(x, x + y), Point(y, x + 2)) is True # Test Intersection assert planar2.intersection(Line3D(p, planar3)) == [Point3D(1, 1, 2)] # Test Scale assert planar2.scale(1, 1, 1) == planar2 assert planar2.scale(2, 2, 2, planar3) == Point3D(1, 1, 1) assert planar2.scale(1, 1, 1, p3) == planar2 # Test Transform identity = Matrix([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]]) assert p.transform(identity) == p trans = Matrix([[1, 0, 0, 1], [0, 1, 0, 1], [0, 0, 1, 1], [0, 0, 0, 1]]) assert p.transform(trans) == Point3D(2, 2, 2) raises(ValueError, lambda: p.transform(p)) raises(ValueError, lambda: p.transform(Matrix([[1, 0], [0, 1]]))) # Test Equals assert p.equals(x1) == False # Test __sub__ p_4d = Point(0, 0, 0, 1) with warnings.catch_warnings(record=True) as w: assert p - p_4d == Point(1, 1, 1, -1) assert len(w) == 1 p_4d3d = Point(0, 0, 1, 0) with warnings.catch_warnings(record=True) as w: assert p - p_4d3d == Point(1, 1, 0, 0) assert len(w) == 1
def test_cross_section(): I = Symbol('I') l = Symbol('l') E = Symbol('E') C3, C4 = symbols('C3, C4') a, c, g, h, r, n = symbols('a, c, g, h, r, n') # test for second_moment and cross_section setter b0 = Beam(l, E, I) assert b0.second_moment == I assert b0.cross_section == None b0.cross_section = Circle((0, 0), 5) assert b0.second_moment == pi*Rational(625, 4) assert b0.cross_section == Circle((0, 0), 5) b0.second_moment = 2*n - 6 assert b0.second_moment == 2*n-6 assert b0.cross_section == None with raises(ValueError): b0.second_moment = Circle((0, 0), 5) # beam with a circular cross-section b1 = Beam(50, E, Circle((0, 0), r)) assert b1.cross_section == Circle((0, 0), r) assert b1.second_moment == pi*r*Abs(r)**3/4 b1.apply_load(-10, 0, -1) b1.apply_load(R1, 5, -1) b1.apply_load(R2, 50, -1) b1.apply_load(90, 45, -2) b1.solve_for_reaction_loads(R1, R2) assert b1.load == (-10*SingularityFunction(x, 0, -1) + 82*SingularityFunction(x, 5, -1)/S(9) + 90*SingularityFunction(x, 45, -2) + 8*SingularityFunction(x, 50, -1)/9) assert b1.bending_moment() == (-10*SingularityFunction(x, 0, 1) + 82*SingularityFunction(x, 5, 1)/9 + 90*SingularityFunction(x, 45, 0) + 8*SingularityFunction(x, 50, 1)/9) q = (-5*SingularityFunction(x, 0, 2) + 41*SingularityFunction(x, 5, 2)/S(9) + 90*SingularityFunction(x, 45, 1) + 4*SingularityFunction(x, 50, 2)/S(9))/(pi*E*r*Abs(r)**3) assert b1.slope() == C3 + 4*q q = (-5*SingularityFunction(x, 0, 3)/3 + 41*SingularityFunction(x, 5, 3)/27 + 45*SingularityFunction(x, 45, 2) + 4*SingularityFunction(x, 50, 3)/27)/(pi*E*r*Abs(r)**3) assert b1.deflection() == C3*x + C4 + 4*q # beam with a recatangular cross-section b2 = Beam(20, E, Polygon((0, 0), (a, 0), (a, c), (0, c))) assert b2.cross_section == Polygon((0, 0), (a, 0), (a, c), (0, c)) assert b2.second_moment == a*c**3/12 # beam with a triangular cross-section b3 = Beam(15, E, Triangle((0, 0), (g, 0), (g/2, h))) assert b3.cross_section == Triangle(Point2D(0, 0), Point2D(g, 0), Point2D(g/2, h)) assert b3.second_moment == g*h**3/36 # composite beam b = b2.join(b3, "fixed") b.apply_load(-30, 0, -1) b.apply_load(65, 0, -2) b.apply_load(40, 0, -1) b.bc_slope = [(0, 0)] b.bc_deflection = [(0, 0)] assert b.second_moment == Piecewise((a*c**3/12, x <= 20), (g*h**3/36, x <= 35)) assert b.cross_section == None assert b.length == 35 assert b.slope().subs(x, 7) == 8400/(E*a*c**3) assert b.slope().subs(x, 25) == 52200/(E*g*h**3) + 39600/(E*a*c**3) assert b.deflection().subs(x, 30) == 537000/(E*g*h**3) + 712000/(E*a*c**3)
def test_cut_section(): # concave polygon p = Polygon((-1, -1), (1, S(5)/2), (2, 1), (3, S(5)/2), (4, 2), (5, 3), (-1, 3)) l = Line((0, 0), (S(9)/2, 3)) p1 = p.cut_section(l)[0] p2 = p.cut_section(l)[1] assert p1 == Polygon( Point2D(-S(9)/13, -S(6)/13), Point2D(1, S(5)/2), Point2D(S(24)/13, S(16)/13), Point2D(S(12)/5, S(8)/5), Point2D(3, S(5)/2), Point2D(S(24)/7, S(16)/7), Point2D(S(9)/2, 3), Point2D(-1, 3), Point2D(-1, -S(2)/3)) assert p2 == Polygon(Point2D(-1, -1), Point2D(-S(9)/13, -S(6)/13), Point2D(S(24)/13, S(16)/13), Point2D(2, 1), Point2D(S(12)/5, S(8)/5), Point2D(S(24)/7, S(16)/7), Point2D(4, 2), Point2D(5, 3), Point2D(S(9)/2, 3), Point2D(-1, -S(2)/3)) # convex polygon p = RegularPolygon(Point2D(0,0), 6, 6) s = p.cut_section(Line((0, 0), slope=1)) assert s[0] == Polygon(Point2D(-3*sqrt(3) + 9, -3*sqrt(3) + 9), Point2D(3, 3*sqrt(3)), Point2D(-3, 3*sqrt(3)), Point2D(-6, 0), Point2D(-9 + 3*sqrt(3), -9 + 3*sqrt(3))) assert s[1] == Polygon(Point2D(6, 0), Point2D(-3*sqrt(3) + 9, -3*sqrt(3) + 9), Point2D(-9 + 3*sqrt(3), -9 + 3*sqrt(3)), Point2D(-3, -3*sqrt(3)), Point2D(3, -3*sqrt(3))) # case where line does not intersects but coincides with the edge of polygon a, b = 20, 10 t1, t2, t3, t4 = [(0, b), (0, 0), (a, 0), (a, b)] p = Polygon(t1, t2, t3, t4) p1, p2 = p.cut_section(Line((0, b), slope=0)) assert p1 == None assert p2 == Polygon(Point2D(0, 10), Point2D(0, 0), Point2D(20, 0), Point2D(20, 10)) p3, p4 = p.cut_section(Line((0, 0), slope=0)) assert p3 == Polygon(Point2D(0, 10), Point2D(0, 0), Point2D(20, 0), Point2D(20, 10)) assert p4 == None
def test_nine_point_circle(): assert Triangle(Point(0, 0), Point(1, 0), Point(0, 1)).nine_point_circle \ == Circle(Point2D(S(1)/4, S(1)/4), sqrt(2)/4)