Example #1
0
def test_centroid():
    p = Polygon((0, 0), (10, 0), (10, 10))
    q = p.translate(0, 20)
    assert centroid(p, q) == Point(20, 40) / 3
    p = Segment((0, 0), (2, 0))
    q = Segment((0, 0), (2, 2))
    assert centroid(p, q) == Point(1, -sqrt(2) + 2)
    assert centroid(Point(0, 0), Point(2, 0)) == Point(2, 0) / 2
    assert centroid(Point(0, 0), Point(0, 0), Point(2, 0)) == Point(2, 0) / 3
Example #2
0
def test_perpendicular_bisector():
    s1 = Segment(Point(0, 0), Point(1, 1))
    aline = Line(Point(S.Half, S.Half), Point(Rational(3, 2), Rational(-1, 2)))
    on_line = Segment(Point(S.Half, S.Half),
                      Point(Rational(3, 2), Rational(-1, 2))).midpoint

    assert s1.perpendicular_bisector().equals(aline)
    assert s1.perpendicular_bisector(on_line).equals(
        Segment(s1.midpoint, on_line))
    assert s1.perpendicular_bisector(on_line + (1, 0)).equals(aline)
Example #3
0
def test_perpendicular_bisector():
    s1 = Segment(Point(0, 0), Point(1, 1))
    aline = Line(Point(S(1) / 2, S(1) / 2), Point(S(3) / 2, -S(1) / 2))
    on_line = Segment(Point(S(1) / 2,
                            S(1) / 2), Point(S(3) / 2, -S(1) / 2)).midpoint

    assert s1.perpendicular_bisector().equals(aline)
    assert s1.perpendicular_bisector(on_line).equals(
        Segment(s1.midpoint, on_line))
    assert s1.perpendicular_bisector(on_line + (1, 0)).equals(aline)
Example #4
0
def test_is_similar():
    p1 = Point(2000, 2000)
    p2 = p1.scale(2, 2)

    r1 = Ray3D(Point3D(1, 1, 1), Point3D(1, 0, 0))
    r2 = Ray(Point(0, 0), Point(0, 1))

    s1 = Segment(Point(0, 0), p1)

    assert s1.is_similar(Segment(p1, p2))
    assert s1.is_similar(r2) is False
    assert r1.is_similar(Line3D(Point3D(1, 1, 1), Point3D(1, 0, 0))) is True
    assert r1.is_similar(Line3D(Point3D(0, 0, 0), Point3D(0, 1, 0))) is False
Example #5
0
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))

    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 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(S(4) / 3,
                                  S(4) / 3,
                                  S(4) / 3))
    assert l3.projection(Ray3D(p2, Point3D(-1, 1, 1))) == Ray3D(
        Point3D(0, 0, 0), Point3D(S(1) / 3,
                                  S(1) / 3,
                                  S(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)
Example #6
0
def test_convex_hull():
    p = [Point(-5,-1), Point(-2,1), Point(-2,-1), Point(-1,-3), Point(0,0),
         Point(1,1), Point(2,2), Point(2,-1), Point(3,1), Point(4,-1), Point(6,2)]
    ch = Polygon(p[0], p[3], p[9], p[10], p[6], p[1])
    #test handling of duplicate points
    p.append(p[3])

    #more than 3 collinear points
    another_p = [Point(-45, -85), Point(-45, 85), Point(-45,26),Point(-45,-24)]
    ch2 = Segment(another_p[0],another_p[1])

    assert convex_hull(another_p) == ch2
    assert convex_hull(p) == ch
    assert convex_hull(p[0]) == p[0]
    assert convex_hull(p[0], p[1]) == Segment(p[0], p[1])
Example #7
0
    def point_is_on_boundary(self, point, tolerance=None):
        """Return the polygon on whose boundary the given point lies,
           within tolerance.

        Args:
            point: an list [x, y] defining a point, or
                   a Point object from a GradeableFunction grader
            tolerance: a pixel distance tolerance
        Returns:
            list:
            The first polygon, defined as a list of points, that contains
            the given point, or None.
        """
        if tolerance is None:
            tolerance = self.tolerance['point_distance'] / self.xscale

        if isinstance(point, SR_Point):
            point = [point.x, point.y]

        for polygon in self.polygons:
            for i, pt in enumerate(polygon.points):
                if i < len(polygon.points) - 1:
                    pt2 = polygon.points[i + 1]
                else:
                    pt2 = polygon.points[0]

                poly_seg = Segment(pt, pt2)
                distance = poly_seg.distance(Point(*point))
                if distance < tolerance:
                    return polygon

        return None
Example #8
0
def test_booleans():
    """ test basic unions and intersections """
    assert Union(l1, l2).equals(l1)
    assert Intersection(l1, l2).equals(l1)
    assert Intersection(l1, l4) == FiniteSet(Point(1, 1))
    assert Intersection(Union(l1, l4),
                        l3) == FiniteSet(Point(-1 / 3, -1 / 3), Point(5, 1))
    assert Intersection(l1, FiniteSet(Point(7, -7))) == EmptySet()
    assert Intersection(Circle(Point(0, 0), 3),
                        Line(p1, p2)) == FiniteSet(Point(-3, 0), Point(3, 0))

    fs = FiniteSet(Point(1 / 3, 1), Point(2 / 3, 0), Point(9 / 5, 1 / 5),
                   Point(7 / 3, 1))
    # test the intersection of polygons
    assert Intersection(poly1, poly2) == fs
    # make sure if we union polygons with subsets, the subsets go away
    assert Union(poly1, poly2, fs) == Union(poly1, poly2)
    # make sure that if we union with a FiniteSet that isn't a subset,
    # that the points in the intersection stop being listed
    assert Union(poly1, FiniteSet(Point(0, 0),
                                  Point(3,
                                        5))) == Union(poly1,
                                                      FiniteSet(Point(3, 5)))
    # intersect two polygons that share an edge
    assert Intersection(poly1, poly3) == Union(
        FiniteSet(Point(3 / 2, 1), Point(2, 1)),
        Segment(Point(0, 0), Point(1, 0)))
Example #9
0
def test_router_corridor_close(tmp_path, env, router_id):
    """

    :param tmp_path: working directory of test execution
    :param env: global environment object
    """
    input_location = (env.systemtest_path / "router_tests" /
                      "test_corridor_close")
    template_path = input_location / "inifile.template"
    inifile_path = tmp_path / "inifile.xml"
    instanciate_tempalte(
        src=template_path,
        args={"router_id": router_id},
        dest=inifile_path,
    )
    copy_files(
        sources=[input_location / "geometry.xml"],
        dest=tmp_path,
    )
    jpscore_driver = JpsCoreDriver(jpscore_path=env.jpscore_path,
                                   working_directory=tmp_path)
    jpscore_driver.run()

    trajectories = load_trajectory(jpscore_driver.traj_file)
    agent_path = trajectories.path(2)
    assert get_intersetions_path_segment(
        agent_path, Segment(Point(9.5, -5), Point(9.5, 5)))
Example #10
0
def test_svg():
    a = Line(Point(1, 1),Point(1, 2))
    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)"/>'
    a = Segment(Point(1, 0),Point(1, 1))
    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" />'
    a = Ray(Point(2, 3), Point(3, 5))
    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)"/>'
def rotate_segment(segment: Segment, angle: int) -> Segment:
    a = [segment.p1.x, segment.p1.y]
    b = [segment.p2.x, segment.p2.y]

    angle = angle * pi / 180

    midpoint = [(a[0] + b[0]) / 2, (a[1] + b[1]) / 2]
    a_mid = [a[0] - midpoint[0], a[1] - midpoint[1]]
    b_mid = [b[0] - midpoint[0], b[1] - midpoint[1]]

    a_rotated = [
        cos(angle) * a_mid[0] - sin(angle) * a_mid[1],
        sin(angle) * a_mid[0] + cos(angle) * a_mid[1]
    ]
    b_rotated = [
        cos(angle) * b_mid[0] - sin(angle) * b_mid[1],
        sin(angle) * b_mid[0] + cos(angle) * b_mid[1]
    ]

    a_rotated[0] = (a_rotated[0] + midpoint[0])
    a_rotated[1] = (a_rotated[1] + midpoint[1])
    b_rotated[0] = (b_rotated[0] + midpoint[0])
    b_rotated[1] = (b_rotated[1] + midpoint[1])

    rotated_segment = Segment(Point(int(a_rotated[0]), int(a_rotated[1])),
                              Point(int(b_rotated[0]), int(b_rotated[1])))

    return rotated_segment
def recalc(inter, centro,circ):
    a= inter[0]
    b= inter[1]
    seg= Segment(a,b)
    c= seg.midpoint
    line= Ray(centro,c)
    return intersection(line,circ);
Example #13
0
def test_subs():
    x = Symbol('x', real=True)
    y = Symbol('y', real=True)
    p = Point(x, 2)
    q = Point(1, 1)
    r = Point(3, 4)
    for o in [
            p,
            Segment(p, q),
            Ray(p, q),
            Line(p, q),
            Triangle(p, q, r),
            RegularPolygon(p, 3, 6),
            Polygon(p, q, r, Point(5, 4)),
            Circle(p, 3),
            Ellipse(p, 3, 4)
    ]:
        assert 'y' in str(o.subs(x, y))
    assert p.subs({x: 1}) == Point(1, 2)
    assert Point(1, 2).subs(Point(1, 2), Point(3, 4)) == Point(3, 4)
    assert Point(1, 2).subs((1, 2), Point(3, 4)) == Point(3, 4)
    assert Point(1, 2).subs(Point(1, 2), Point(3, 4)) == Point(3, 4)
    assert Point(1, 2).subs(set([(1, 2)])) == Point(2, 2)
    raises(ValueError, lambda: Point(1, 2).subs(1))
    raises(ValueError, lambda: Point(1, 1).subs(
        (Point(1, 1), Point(1, 2)), 1, 2))
Example #14
0
def change_size_segment(segment: Segment, coefficient: float) -> Segment:
    """
    Change size of a segment according to give coefficient

    :param segment: Reflective segment
    :param coefficient: Coefficient for resizing
    :return: Resized segment
    """
    a = [segment.p1.x, segment.p1.y]
    b = [segment.p2.x, segment.p2.y]

    midpoint = [(a[0] + b[0]) / 2, (a[1] + b[1]) / 2]
    x_diff_half = (b[0] - a[0]) / 2
    y_diff_half = (b[1] - a[1]) / 2

    a_changed = [
        midpoint[0] - coefficient * x_diff_half,
        midpoint[1] - coefficient * y_diff_half
    ]
    b_changed = [
        midpoint[0] + coefficient * x_diff_half,
        midpoint[1] + coefficient * y_diff_half
    ]

    changed_segment = Segment(Point(int(a_changed[0]), int(a_changed[1])),
                              Point(int(b_changed[0]), int(b_changed[1])))

    return changed_segment
Example #15
0
def rotate_segment(segment: Segment, angle: int) -> Segment:
    """
    Rotate segment clockwise or counterclockwise according to given angle

    :param segment: Reflective segment
    :param angle: Angle for rotation (in degrees)
    :return: Rotated segment
    """
    a = [segment.p1.x, segment.p1.y]
    b = [segment.p2.x, segment.p2.y]

    angle = angle * pi / 180

    midpoint = [(a[0] + b[0]) / 2, (a[1] + b[1]) / 2]
    a_mid = [a[0] - midpoint[0], a[1] - midpoint[1]]
    b_mid = [b[0] - midpoint[0], b[1] - midpoint[1]]

    a_rotated = [
        cos(angle) * a_mid[0] - sin(angle) * a_mid[1],
        sin(angle) * a_mid[0] + cos(angle) * a_mid[1]
    ]
    b_rotated = [
        cos(angle) * b_mid[0] - sin(angle) * b_mid[1],
        sin(angle) * b_mid[0] + cos(angle) * b_mid[1]
    ]

    a_rotated[0] = (a_rotated[0] + midpoint[0])
    a_rotated[1] = (a_rotated[1] + midpoint[1])
    b_rotated[0] = (b_rotated[0] + midpoint[0])
    b_rotated[1] = (b_rotated[1] + midpoint[1])

    rotated_segment = Segment(Point(int(a_rotated[0]), int(a_rotated[1])),
                              Point(int(b_rotated[0]), int(b_rotated[1])))

    return rotated_segment
Example #16
0
def test_line_intersection():
    assert asa(120, 8, 52) == \
           Triangle(
               Point(0, 0),
               Point(8, 0),
               Point(-4 * cos(19 * pi / 90) / sin(2 * pi / 45),
                     4 * sqrt(3) * cos(19 * pi / 90) / sin(2 * pi / 45)))
    assert Line((0, 0), (1, 1)).intersection(Ray((1, 0), (1, 2))) == [Point(1, 1)]
    assert Line((0, 0), (1, 1)).intersection(Segment((1, 0), (1, 2))) == [Point(1, 1)]
    assert Ray((0, 0), (1, 1)).intersection(Ray((1, 0), (1, 2))) == [Point(1, 1)]
    assert Ray((0, 0), (1, 1)).intersection(Segment((1, 0), (1, 2))) == [Point(1, 1)]
    assert Ray((0, 0), (10, 10)).contains(Segment((1, 1), (2, 2))) is True
    assert Segment((1, 1), (2, 2)) in Line((0, 0), (10, 10))
    x = 8 * tan(13 * pi / 45) / (tan(13 * pi / 45) + sqrt(3))
    y = (-8 * sqrt(3) * tan(13 * pi / 45) ** 2 + 24 * tan(13 * pi / 45)) / (-3 + tan(13 * pi / 45) ** 2)
    assert Line(Point(0, 0), Point(1, -sqrt(3))).contains(Point(x, y)) is True
Example #17
0
    def get_intersections_with_polygon_boundary(self,
                                                polygon,
                                                line_segment,
                                                tolerance=None):
        """Return a list of intersection points of the given line segment
           with the given polygon.

        Args:
            polygon: a list of points [[x1,y1], ..., [xn,yn]] defining a polygon,
                     or a Polygon object
            line_segment: an list of two points [[x1, y1], [x2,y2]], or
                          a LineSegment object from a LineSegment grader
            tolerance: a pixel distance tolerance
        Returns:
            list:
            A list of intersection points [x,y].
        """
        intersections = []
        if isinstance(line_segment, LineSegment):
            point1 = line_segment.getStartPoint()
            point2 = line_segment.getEndPoint()
        else:
            point1 = line_segment[0]
            point2 = line_segment[1]

        if isinstance(polygon, Polygon):
            polygon = polygon.points

        in_seg = Segment(Point(*point1), Point(*point2))

        for i, pt in enumerate(polygon):
            if i < len(polygon) - 1:
                pt2 = polygon[i + 1]
            else:
                pt2 = polygon[0]

            poly_seg = Segment(pt, pt2)
            intersection_points = intersection(in_seg, poly_seg)
            for ip in intersection_points:
                intersections.append([ip.x, ip.y])

        intersections = self.filter_intersections_for_endpoints(
            intersections, point1, point2, tolerance=tolerance)

        return intersections
Example #18
0
def adjust_out_of_bounds_points(vert1, vert2, boundaries):
    line_seg = None
    if vert1[0] < 0 or vert1[1] < 0 or vert2[0] < 0 or vert2[1] < 0 or vert1[
            0] > width or vert1[1] > height or vert2[0] > width or vert2[
                1] > height:
        line_seg = Segment(Point(vert1), Point(vert2))

        if vert1[0] < 0:
            intrscts = line_seg.intersection(boundaries[3])
            if len(intrscts) == 0:
                return None, None
            intrsct = intrscts[0]
            vert1 = intrsct.x, intrsct.y

        if vert1[1] < 0:
            intrscts = line_seg.intersection(boundaries[0])
            if len(intrscts) == 0:
                return None, None
            intrsct = intrscts[0]
            vert1 = intrsct.x, intrsct.y
        if vert2[0] < 0:
            intrscts = line_seg.intersection(boundaries[3])
            if len(intrscts) == 0:
                return None, None
            intrsct = intrscts[0]
            vert2 = intrsct.x, intrsct.y
        if vert2[1] < 0:
            intrscts = line_seg.intersection(boundaries[0])
            if len(intrscts) == 0:
                return None, None
            intrsct = intrscts[0]
            vert2 = intrsct.x, intrsct.y
        if vert1[0] > width:
            intrscts = line_seg.intersection(boundaries[1])
            if len(intrscts) == 0:
                return None, None
            intrsct = intrscts[0]
            vert1 = intrsct.x, intrsct.y
        if vert1[1] > height:
            intrscts = line_seg.intersection(boundaries[2])
            if len(intrscts) == 0:
                return None, None
            intrsct = intrscts[0]
            vert1 = intrsct.x, intrsct.y
        if vert2[0] > width:
            intrscts = line_seg.intersection(boundaries[1])
            if len(intrscts) == 0:
                return None, None
            intrsct = intrscts[0]
            vert2 = intrsct.x, intrsct.y
        if vert2[1] > height:
            intrscts = line_seg.intersection(boundaries[2])
            if len(intrscts) == 0:
                return None, None
            intrsct = intrscts[0]
            vert2 = intrsct.x, intrsct.y
    return vert1, vert2
Example #19
0
def test_contains():
    p1 = Point(0, 0)

    r = Ray(p1, Point(4, 4))
    r1 = Ray3D(p1, Point3D(0, 0, -1))
    r2 = Ray3D(p1, Point3D(0, 1, 0))
    r3 = Ray3D(p1, Point3D(0, 0, 1))

    l = Line(Point(0, 1), Point(3, 4))
    # Segment contains
    assert Point(0, (a + b) / 2) in Segment((0, a), (0, b))
    assert Point((a + b) / 2, 0) in Segment((a, 0), (b, 0))
    assert Point3D(0, 1, 0) in Segment3D((0, 1, 0), (0, 1, 0))
    assert Point3D(1, 0, 0) in Segment3D((1, 0, 0), (1, 0, 0))
    assert Segment3D(Point3D(0, 0, 0), Point3D(1, 0, 0)).contains([]) is True
    assert Segment3D(Point3D(0, 0, 0), Point3D(1, 0, 0)).contains(
        Segment3D(Point3D(2, 2, 2), Point3D(3, 2, 2))) is False
    # Line contains
    assert l.contains(Point(0, 1)) is True
    assert l.contains((0, 1)) is True
    assert l.contains((0, 0)) is False
    # Ray contains
    assert r.contains(p1) is True
    assert r.contains((1, 1)) is True
    assert r.contains((1, 3)) is False
    assert r.contains(Segment((1, 1), (2, 2))) is True
    assert r.contains(Segment((1, 2), (2, 5))) is False
    assert r.contains(Ray((2, 2), (3, 3))) is True
    assert r.contains(Ray((2, 2), (3, 5))) is False
    assert r1.contains(Segment3D(p1, Point3D(0, 0, -10))) is True
    assert r1.contains(Segment3D(Point3D(1, 1, 1), Point3D(2, 2, 2))) is False
    assert r2.contains(Point3D(0, 0, 0)) is True
    assert r3.contains(Point3D(0, 0, 0)) is True
    assert Ray3D(Point3D(1, 1, 1), Point3D(1, 0, 0)).contains([]) is False
    assert Line3D((0, 0, 0), (x, y, z)).contains((2 * x, 2 * y, 2 * z))
    with warnings.catch_warnings(record=True) as w:
        assert Line3D(p1, Point3D(0, 1, 0)).contains(Point(1.0, 1.0)) is False
        assert len(w) == 1

    with warnings.catch_warnings(record=True) as w:
        assert r3.contains(Point(1.0, 1.0)) is False
        assert len(w) == 1
Example #20
0
def test_vector_integrate():
    halfdisc = ParametricRegion((r * cos(theta), r * sin(theta)), (r, -2, 2),
                                (theta, 0, pi))
    assert vector_integrate(C.x**2, halfdisc) == 4 * pi
    vector_integrate(C.x, ParametricRegion(
        (t, t**2), (t, 2, 3))) == -17 * sqrt(17) / 12 + 37 * sqrt(37) / 12

    assert vector_integrate(C.y**3 * C.z, (C.x, 0, 3),
                            (C.y, -1, 4)) == 765 * C.z / 4

    s1 = Segment(Point(0, 0), Point(0, 1))
    assert vector_integrate(-15 * C.y, s1) == S(-15) / 2
    s2 = Segment(Point(4, 3, 9), Point(1, 1, 7))
    assert vector_integrate(C.y * C.i, s2) == -6

    curve = Curve((sin(t), cos(t)), (t, 0, 2))
    assert vector_integrate(5 * C.z, curve) == 10 * C.z

    c1 = Circle(Point(2, 3), 6)
    assert vector_integrate(C.x * C.y, c1) == 72 * pi
    c2 = Circle(Point(0, 0), Point(1, 1), Point(1, 0))
    assert vector_integrate(1, c2) == c2.circumference

    triangle = Polygon((0, 0), (1, 0), (1, 1))
    assert vector_integrate(C.x * C.i - 14 * C.y * C.j, triangle) == 0
    p1, p2, p3, p4 = [(0, 0), (1, 0), (5, 1), (0, 1)]
    poly = Polygon(p1, p2, p3, p4)
    assert vector_integrate(-23 * C.z,
                            poly) == -161 * C.z - 23 * sqrt(17) * C.z

    point = Point(2, 3)
    assert vector_integrate(C.i * C.y - C.z, point) == ParametricIntegral(
        C.y * C.i, ParametricRegion((2, 3)))

    c3 = ImplicitRegion((x, y), x**2 + y**2 - 4)
    assert vector_integrate(45, c3) == 360 * pi
    c4 = ImplicitRegion((x, y), (x - 3)**2 + (y - 4)**2 - 9)
    assert vector_integrate(1, c4) == 12 * pi

    pl = Plane(Point(1, 1, 1), Point(2, 3, 4), Point(2, 2, 2))
    raises(ValueError, lambda: vector_integrate(C.x * C.z * C.i + C.k, pl))
Example #21
0
    def reducer(self, key, values):
        lines = []
        for value in values:
            lines.append(Segment(Point(value[0], evaluate=False), Point(value[1], evaluate=False)))

        for i in range(len(lines)):
            for j in range(len(lines)):
                if j > i:
                    heat = lines[i].intersection(lines[j])
                    if len(heat) >0:
                        if isinstance(heat[0], Point):
                            yield str(heat[0].x), str(heat[0].y)
Example #22
0
def test_convex_hull():
    p = [
        Point(-5, -1),
        Point(-2, 1),
        Point(-2, -1),
        Point(-1, -3),
        Point(0, 0),
        Point(1, 1),
        Point(2, 2),
        Point(2, -1),
        Point(3, 1),
        Point(4, -1),
        Point(6, 2)
    ]
    ch = Polygon(p[0], p[3], p[9], p[10], p[6], p[1])
    #test handling of duplicate points
    p.append(p[3])

    #more than 3 collinear points
    another_p = [
        Point(-45, -85),
        Point(-45, 85),
        Point(-45, 26),
        Point(-45, -24)
    ]
    ch2 = Segment(another_p[0], another_p[1])

    assert convex_hull(*another_p) == ch2
    assert convex_hull(*p) == ch
    assert convex_hull(p[0]) == p[0]
    assert convex_hull(p[0], p[1]) == Segment(p[0], p[1])

    # no unique points
    assert convex_hull(*[p[-1]] * 3) == p[-1]

    # collection of items
    assert convex_hull(*[Point(0,0),
                        Segment(Point(1, 0), Point(1, 1)),
                        RegularPolygon(Point(2, 0), 2, 4)]) == \
            Polygon(Point(0, 0), Point(2, -2), Point(4, 0), Point(2, 2))
Example #23
0
def test_intersection():
    assert intersection(Point(0, 0)) == []
    raises(TypeError, lambda: intersection(Point(0, 0), 3))
    assert intersection(
            Segment((0, 0), (2, 0)),
            Segment((-1, 0), (1, 0)),
            Line((0, 0), (0, 1)), pairwise=True) == [
        Point(0, 0), Segment((0, 0), (1, 0))]
    assert intersection(
            Line((0, 0), (0, 1)),
            Segment((0, 0), (2, 0)),
            Segment((-1, 0), (1, 0)), pairwise=True) == [
        Point(0, 0), Segment((0, 0), (1, 0))]
    assert intersection(
            Line((0, 0), (0, 1)),
            Segment((0, 0), (2, 0)),
            Segment((-1, 0), (1, 0)),
            Line((0, 0), slope=1), pairwise=True) == [
        Point(0, 0), Segment((0, 0), (1, 0))]
Example #24
0
def test_raises():
    d, e = symbols('a,b', real=True)
    s = Segment((d, 0), (e, 0))

    raises(TypeError, lambda: Line((1, 1), 1))
    raises(ValueError, lambda: Line(Point(0, 0), Point(0, 0)))
    raises(Undecidable, lambda: Point(2 * d, 0) in s)
    raises(ValueError, lambda: Ray3D(Point(1.0, 1.0)))
    raises(ValueError, lambda: Line3D(Point3D(0, 0, 0), Point3D(0, 0, 0)))
    raises(TypeError, lambda: Line3D((1, 1), 1))
    raises(ValueError, lambda: Line3D(Point3D(0, 0, 0)))
    raises(TypeError, lambda: Ray((1, 1), 1))
    raises(GeometryError, lambda: Line(Point(0, 0), Point(1, 0))
           .projection(Circle(Point(0, 0), 1)))
Example #25
0
def test_subs():
    p = Point(x, 2)
    q = Point(1, 1)
    r = Point(3, 4)
    for o in [p,
              Segment(p, q),
              Ray(p, q),
              Line(p, q),
              Triangle(p, q, r),
              RegularPolygon(p, 3, 6),
              Polygon(p, q, r, Point(5,4)),
              Circle(p, 3),
              Ellipse(p, 3, 4)]:
        assert 'y' in str(o.subs(x, y))
Example #26
0
def checkSimple(A, r):
    """
    This function checks if the given polygon with coordinates is simple.
    :param A: Coordinate matrices of the polygon.
    :param r: Side lengths as a vector.
    :return: True or False
    """
    n = len(r)
    An = np.vstack((A, A[0, :], A[1, :]))
    flag = 0
    for k in range(n):
        if (k < n - 1):
            for l in range(max(0, k - 2)):
                a = Segment(tuple(An[l, :]), tuple(An[l + 1, :]))
                b = Segment(tuple(An[k, :]), tuple(An[k + 1, :]))
                pts = a.intersect(b)
                if (len(pts) == 1):
                    flag = 1
                    break
            if (flag == 1):
                break
        else:
            for l in range(1, max(0, k - 2)):
                a = Segment(tuple(An[l, :]), tuple(An[l + 1, :]))
                b = Segment(tuple(An[k, :]), tuple(An[k + 1, :]))
                pts = a.intersect(b)
                if (len(pts) == 1):
                    flag = 1
                    break
            if (flag == 1):
                break

    if (flag == 1):
        print('The polygon is not simple!')
        return False
    else:
        return True
Example #27
0
def test_parametric_region_list():

    point = Point(-5, 12)
    assert parametric_region_list(point) == [ParametricRegion((-5, 12))]

    e = Ellipse(Point(2, 8), 2, 6)
    assert parametric_region_list(e, t) == [
        ParametricRegion((2 * cos(t) + 2, 6 * sin(t) + 8), (t, 0, 2 * pi))
    ]

    c = Curve((t, t**3), (t, 5, 3))
    assert parametric_region_list(c) == [
        ParametricRegion((t, t**3), (t, 5, 3))
    ]

    s = Segment(Point(2, 11, -6), Point(0, 2, 5))
    assert parametric_region_list(s, t) == [
        ParametricRegion((2 - 2 * t, 11 - 9 * t, 11 * t - 6), (t, 0, 1))
    ]
    s1 = Segment(Point(0, 0), (1, 0))
    assert parametric_region_list(s1,
                                  t) == [ParametricRegion((t, 0), (t, 0, 1))]
    s2 = Segment(Point(1, 2, 3), Point(1, 2, 5))
    assert parametric_region_list(
        s2, t) == [ParametricRegion((1, 2, 2 * t + 3), (t, 0, 1))]
    s3 = Segment(Point(12, 56), Point(12, 56))
    assert parametric_region_list(s3) == [ParametricRegion((12, 56))]

    poly = Polygon((1, 3), (-3, 8), (2, 4))
    assert parametric_region_list(poly, t) == [
        ParametricRegion((1 - 4 * t, 5 * t + 3), (t, 0, 1)),
        ParametricRegion((5 * t - 3, 8 - 4 * t), (t, 0, 1)),
        ParametricRegion((2 - t, 4 - t), (t, 0, 1))
    ]

    p1 = Parabola(Point(0, 0), Line(Point(5, 8), Point(7, 8)))
    raises(ValueError, lambda: parametric_region_list(p1))
Example #28
0
def test_arbitrary_point():
    l1 = Line3D(Point3D(0, 0, 0), Point3D(1, 1, 1))
    l2 = Line(Point(x1, x1), Point(y1, y1))
    assert l2.arbitrary_point() in l2
    assert Ray((1, 1), angle=pi / 4).arbitrary_point() == \
           Point(t + 1, t + 1)
    assert Segment((1, 1), (2, 3)).arbitrary_point() == Point(1 + t, 1 + 2 * t)
    assert l1.perpendicular_segment(l1.arbitrary_point()) == l1.arbitrary_point()
    assert Ray3D((1, 1, 1), direction_ratio=[1, 2, 3]).arbitrary_point() == \
           Point3D(t + 1, 2 * t + 1, 3 * t + 1)
    assert Segment3D(Point3D(0, 0, 0), Point3D(1, 1, 1)).midpoint == \
           Point3D(Rational(1, 2), Rational(1, 2), Rational(1, 2))
    assert Segment3D(Point3D(x1, x1, x1), Point3D(y1, y1, y1)).length == sqrt(3) * sqrt((x1 - y1) ** 2)
    assert Segment3D((1, 1, 1), (2, 3, 4)).arbitrary_point() == \
           Point3D(t + 1, 2 * t + 1, 3 * t + 1)
Example #29
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)))
Example #30
0
def findcells(x0, x1, z0, z1, lines):
    clusCells = []
    clusPos = []
    nonClusCells = []
    stop = True

    vertices = findPointsofRect(x0, x1, z0, z1)

    segment1 = Segment(vertices[0], vertices[1])
    segment2 = Segment(vertices[1], vertices[3])
    segment3 = Segment(vertices[3], vertices[2])
    segment4 = Segment(vertices[2], vertices[0])

    for i in range(len(lines)):
        line = lines[i]
        if (line[0][0] == 0 and line[0][1] == 0):
            nonClusCells.append([(0, 0)])
            continue
        x1, x2, y1, y2 = findLine(line[0][0], line[0][1])
        lineF = Line((x1, y1), (x2, y2))
        # I don't think finding intersection is reqd
        i1 = lineF.intersection(segment1)
        i2 = lineF.intersection(segment2)
        i3 = lineF.intersection(segment3)
        i4 = lineF.intersection(segment4)

        if (not (len(i1) == 0 and len(i2) == 0 and len(i3) == 0
                 and len(i4) == 0)):
            clusCells.append(line)
            clusPos.append(i)
            nonClusCells.append([(0, 0)])
            stop = False
        else:
            nonClusCells.append(line)

    return clusCells, clusPos, np.array(nonClusCells), stop, vertices