Exemple #1
0
    def reflect(self, prev_position: Point, circle: Circle) -> None:
        """
        Reflects the photon on the given circle.

         - calculates distance covered and subtracts it from self.distance
         - calculates new direction vector
         - jumps to the new position and prints it

        :rtype: None
        """
        if prev_position.distance(self.position) > self.distance:
            self.position = prev_position.translate(
                self.ray.direction.x * self.distance,
                self.ray.direction.y * self.distance)
            self.distance = 0
            return

        self.distance -= prev_position.distance(self.position).evalf(PRECISION)
        self.checked_circles = [circle]

        tangent = circle.tangent_lines(self.position)[0]
        self.position = Point(self.position.x.evalf(PRECISION),
                              self.position.y.evalf(PRECISION))
        self.imprecise_position = self.position

        direction = self.position + self.ray.reflect(tangent).direction
        direction = Point(direction.x.evalf(PRECISION),
                          direction.y.evalf(PRECISION))

        self.ray = Ray(self.position, direction)

        self.jump_to_position()
        dot()
        self.print_position()
Exemple #2
0
def test_point():
    p1 = Point(x1, x2)
    p2 = Point(y1, y2)
    p3 = Point(0, 0)
    p4 = Point(1, 1)

    assert len(p1) == 1
    assert p1 in p1
    assert p1 not in p2
    assert p2[1] == y2
    assert (p3 + p4) == p4
    assert (p2 - p1) == Point(y1 - x1, y2 - x2)
    assert p4 * 5 == Point(5, 5)
    assert -p2 == Point(-y1, -y2)

    assert Point.midpoint(p3, p4) == Point(half, half)
    assert Point.midpoint(p1, p4) == Point(half + half * x1, half + half * x2)
    assert Point.midpoint(p2, p2) == p2
    assert p2.midpoint(p2) == p2

    assert Point.distance(p3, p4) == sqrt(2)
    assert Point.distance(p1, p1) == 0
    assert Point.distance(p3, p2) == sqrt(p2.x**2 + p2.y**2)

    p1_1 = Point(x1, x1)
    p1_2 = Point(y2, y2)
    p1_3 = Point(x1 + 1, x1)
    assert Point.is_collinear(p3)
    assert Point.is_collinear(p3, p4)
    assert Point.is_collinear(p3, p4, p1_1, p1_2)
    assert Point.is_collinear(p3, p4, p1_1, p1_3) == False

    assert p3.intersection(Point(0, 0)) == [p3]
    assert p3.intersection(p4) == []

    x_pos = Symbol('x', real=True, positive=True)
    p2_1 = Point(x_pos, 0)
    p2_2 = Point(0, x_pos)
    p2_3 = Point(-x_pos, 0)
    p2_4 = Point(0, -x_pos)
    p2_5 = Point(x_pos, 5)
    assert Point.is_concyclic(p2_1)
    assert Point.is_concyclic(p2_1, p2_2)
    assert Point.is_concyclic(p2_1, p2_2, p2_3, p2_4)
    assert Point.is_concyclic(p2_1, p2_2, p2_3, p2_5) == False
    assert Point.is_concyclic(p4, p4 * 2, p4 * 3) == False

    assert p4.scale(2, 3) == Point(2, 3)
    assert p3.scale(2, 3) == p3

    assert p4.rotate(pi, Point(0.5, 0.5)) == p3
    assert p1.__radd__(p2) == p1.midpoint(p2).scale(2, 2)
    assert (-p3).__rsub__(p4) == p3.midpoint(p4).scale(2, 2)

    assert p4 * 5 == Point(5, 5)
    assert p4 / 5 == Point(0.2, 0.2)

    raises(ValueError, 'Point(0,0) + 10')
Exemple #3
0
def test_point():
    p1 = Point(x1, x2)
    p2 = Point(y1, y2)
    p3 = Point(0, 0)
    p4 = Point(1, 1)

    assert len(p1) == 1
    assert p1 in p1
    assert p1 not in p2
    assert p2[1] == y2
    assert (p3+p4) == p4
    assert (p2-p1) == Point(y1-x1, y2-x2)
    assert p4*5 == Point(5, 5)
    assert -p2 == Point(-y1, -y2)

    assert Point.midpoint(p3, p4) == Point(half, half)
    assert Point.midpoint(p1, p4) == Point(half + half*x1, half + half*x2)
    assert Point.midpoint(p2, p2) == p2
    assert p2.midpoint(p2) == p2

    assert Point.distance(p3, p4) == sqrt(2)
    assert Point.distance(p1, p1) == 0
    assert Point.distance(p3, p2) == sqrt(p2.x**2 + p2.y**2)

    p1_1 = Point(x1, x1)
    p1_2 = Point(y2, y2)
    p1_3 = Point(x1 + 1, x1)
    assert Point.is_collinear(p3)
    assert Point.is_collinear(p3, p4)
    assert Point.is_collinear(p3, p4, p1_1, p1_2)
    assert Point.is_collinear(p3, p4, p1_1, p1_3) == False

    assert p3.intersection(Point(0, 0)) == [p3]
    assert p3.intersection(p4) == []

    x_pos = Symbol('x', real=True, positive=True)
    p2_1 = Point(x_pos, 0)
    p2_2 = Point(0, x_pos)
    p2_3 = Point(-x_pos, 0)
    p2_4 = Point(0, -x_pos)
    p2_5 = Point(x_pos, 5)
    assert Point.is_concyclic(p2_1)
    assert Point.is_concyclic(p2_1, p2_2)
    assert Point.is_concyclic(p2_1, p2_2, p2_3, p2_4)
    assert Point.is_concyclic(p2_1, p2_2, p2_3, p2_5) == False
    assert Point.is_concyclic(p4, p4 * 2, p4 * 3) == False

    assert p4.scale(2, 3) == Point(2, 3)
    assert p3.scale(2, 3) == p3

    assert p4.rotate(pi, Point(0.5, 0.5)) == p3
    assert p1.__radd__(p2) == p1.midpoint(p2).scale(2, 2)
    assert (-p3).__rsub__(p4) == p3.midpoint(p4).scale(2, 2)

    assert p4 * 5 == Point(5, 5)
    assert p4 / 5 == Point(0.2, 0.2)

    raises(ValueError, 'Point(0,0) + 10')
Exemple #4
0
def closest_station_of(pos, stations):
    station_coords = [station.coords for station in stations]
    p = Point(pos)
    best_dist = p.distance(stations[0].coords)
    best_station = stations[0]
    for station in stations[1:]:
        new_dist = p.distance(station.coords)
        if new_dist < best_dist:
            best_dist = new_dist
            best_station = station
    return best_station
Exemple #5
0
def test_point():
    p1 = Point(x1, x2)
    p2 = Point(y1, y2)
    p3 = Point(0, 0)
    p4 = Point(1, 1)

    assert p3.x == 0
    assert p3.y == 0

    assert p4.x == 1
    assert p4.y == 1

    assert len(p1) == 2
    assert p2[1] == y2
    assert (p3+p4) == p4
    assert (p2-p1) == Point(y1-x1, y2-x2)
    assert p4*5 == Point(5, 5)
    assert -p2 == Point(-y1, -y2)

    assert Point.midpoint(p3, p4) == Point(half, half)
    assert Point.midpoint(p1, p4) == Point(half + half*x1, half + half*x2)
    assert Point.midpoint(p2, p2) == p2

    assert Point.distance(p3, p4) == sqrt(2)
    assert Point.distance(p1, p1) == 0
    #assert Point.distance(p3, p2) == abs(p2)

    p1_1 = Point(x1, x1)
    p1_2 = Point(y2, y2)
    p1_3 = Point(x1 + 1, x1)
    assert Point.is_collinear(p3)
    assert Point.is_collinear(p3, p4)
    assert Point.is_collinear(p3, p4, p1_1, p1_2)
    assert Point.is_collinear(p3, p4, p1_1, p1_3) == False

    x_pos = Symbol('x', real=True, positive=True)
    p2_1 = Point(x_pos, 0)
    p2_2 = Point(0, x_pos)
    p2_3 = Point(-x_pos, 0)
    p2_4 = Point(0, -x_pos)
    p2_5 = Point(x_pos, 5)
    assert Point.is_concyclic(p2_1)
    assert Point.is_concyclic(p2_1, p2_2)
    assert Point.is_concyclic(p2_1, p2_2, p2_3, p2_4)
    assert Point.is_concyclic(p2_1, p2_2, p2_3, p2_5) == False
Exemple #6
0
def test_point():
    p1 = Point(x1, x2)
    p2 = Point(y1, y2)
    p3 = Point(0, 0)
    p4 = Point(1, 1)

    assert p3.x == 0
    assert p3.y == 0

    assert p4.x == 1
    assert p4.y == 1

    assert len(p1) == 2
    assert p2[1] == y2
    assert (p3 + p4) == p4
    assert (p2 - p1) == Point(y1 - x1, y2 - x2)
    assert p4 * 5 == Point(5, 5)
    assert -p2 == Point(-y1, -y2)

    assert Point.midpoint(p3, p4) == Point(half, half)
    assert Point.midpoint(p1, p4) == Point(half + half * x1, half + half * x2)
    assert Point.midpoint(p2, p2) == p2

    assert Point.distance(p3, p4) == sqrt(2)
    assert Point.distance(p1, p1) == 0
    #assert Point.distance(p3, p2) == abs(p2)

    p1_1 = Point(x1, x1)
    p1_2 = Point(y2, y2)
    p1_3 = Point(x1 + 1, x1)
    assert Point.is_collinear(p3)
    assert Point.is_collinear(p3, p4)
    assert Point.is_collinear(p3, p4, p1_1, p1_2)
    assert Point.is_collinear(p3, p4, p1_1, p1_3) == False

    x_pos = Symbol('x', real=True, positive=True)
    p2_1 = Point(x_pos, 0)
    p2_2 = Point(0, x_pos)
    p2_3 = Point(-x_pos, 0)
    p2_4 = Point(0, -x_pos)
    p2_5 = Point(x_pos, 5)
    assert Point.is_concyclic(p2_1)
    assert Point.is_concyclic(p2_1, p2_2)
    assert Point.is_concyclic(p2_1, p2_2, p2_3, p2_4)
    assert Point.is_concyclic(p2_1, p2_2, p2_3, p2_5) == False
def store_midpoints_for_line(column_lines):
    column_lines['cluster_group'] = 0
    column_lines['linelength'] = 0
    for index in range(len(column_lines)):
        p1, p2 = Point(column_lines.iloc[index]['x1'], column_lines.iloc[index]['y1']), Point(column_lines.iloc[index]['x2'], column_lines.iloc[index]['y2'])
        column_lines.iloc[index, column_lines.columns.get_loc('linelength')] = p1.distance(p2)
        column_lines['x_midpoint'] = (column_lines['x1'] + column_lines['x2']) / 2.0
        column_lines['y_midpoint'] = (column_lines['y1'] + column_lines['y2']) / 2.0
    return column_lines
Exemple #8
0
 def compute_station_list(self, stations, shortest_path_stations):
     start = Point(self.start)
     end = Point(self.end)
     start_station = closest_station_of(start, stations)
     end_station = closest_station_of(end, stations)
     shortest_path_transport, shortest_length_transport = shortest_path_stations[
         start_station][end_station]
     shortest_by_foot = start.distance(end) / (5 * 60)
     if shortest_by_foot > shortest_length_transport:
         self.stations_list = shortest_path_transport
Exemple #9
0
def navigator():
    pub = rospy.Publisher('/nav_target', Pose2D, queue_size=10)
    rospy.Subscriber('/trajectory', Pose2D_Array, callbackTrajectory)
    rospy.Subscriber('/y_r0', Pose2D, callbackSelf)
    rospy.init_node('navigation')
    rate = rospy.Rate(1)  # Hz
    while not rospy.is_shutdown():
        pSelf = Point(poseSelf.x, poseSelf.y)
        pNav = Point(poseNav.x, poseNav.y)
        dist = pSelf.distance(pNav)
        print "Dist: ", dist
        yDiff = (poseNav.y - poseSelf.y)
        xDiff = (poseNav.x - poseSelf.x)
        print "Y difference: ", yDiff
        print "X difference: ", xDiff
        s = math.atan2(yDiff, xDiff)
        print "Angle: ", s
        print "Robot angle: ", poseSelf.theta

        angleErr1 = s - poseSelf.theta
        angleErr2 = poseSelf.theta - s

        if (abs(angleErr1) > abs(angleErr2)):
            angleErr = angleErr2
        else:
            angleErr = angleErr1

        print "Angle error: ", angleErr
        velVector.theta = -0.3 * (angleErr)
        if (dist > 500):
            v = 350
        elif (dist > 100):
            v = 0.3 * dist + 230
        else:
            v = 200
        velVector.x = v

        pub.publish(velVector)
        rate.sleep()
Exemple #10
0
def test_point():
    x = Symbol('x', real=True)
    y = Symbol('y', real=True)
    x1 = Symbol('x1', real=True)
    x2 = Symbol('x2', real=True)
    y1 = Symbol('y1', real=True)
    y2 = Symbol('y2', real=True)
    half = S.Half
    p1 = Point(x1, x2)
    p2 = Point(y1, y2)
    p3 = Point(0, 0)
    p4 = Point(1, 1)
    p5 = Point(0, 1)
    line = Line(Point(1, 0), slope=1)

    assert p1 in p1
    assert p1 not in p2
    assert p2.y == y2
    assert (p3 + p4) == p4
    assert (p2 - p1) == Point(y1 - x1, y2 - x2)
    assert -p2 == Point(-y1, -y2)
    raises(ValueError, lambda: Point(3, I))
    raises(ValueError, lambda: Point(2 * I, I))
    raises(ValueError, lambda: Point(3 + I, I))

    assert Point(34.05, sqrt(3)) == Point(Rational(681, 20), sqrt(3))
    assert Point.midpoint(p3, p4) == Point(half, half)
    assert Point.midpoint(p1, p4) == Point(half + half * x1, half + half * x2)
    assert Point.midpoint(p2, p2) == p2
    assert p2.midpoint(p2) == p2

    assert Point.distance(p3, p4) == sqrt(2)
    assert Point.distance(p1, p1) == 0
    assert Point.distance(p3, p2) == sqrt(p2.x**2 + p2.y**2)

    # distance should be symmetric
    assert p1.distance(line) == line.distance(p1)
    assert p4.distance(line) == line.distance(p4)

    assert Point.taxicab_distance(p4, p3) == 2

    assert Point.canberra_distance(p4, p5) == 1

    p1_1 = Point(x1, x1)
    p1_2 = Point(y2, y2)
    p1_3 = Point(x1 + 1, x1)
    assert Point.is_collinear(p3)

    with warns(UserWarning):
        assert Point.is_collinear(p3, Point(p3, dim=4))
    assert p3.is_collinear()
    assert Point.is_collinear(p3, p4)
    assert Point.is_collinear(p3, p4, p1_1, p1_2)
    assert Point.is_collinear(p3, p4, p1_1, p1_3) is False
    assert Point.is_collinear(p3, p3, p4, p5) is False

    raises(TypeError, lambda: Point.is_collinear(line))
    raises(TypeError, lambda: p1_1.is_collinear(line))

    assert p3.intersection(Point(0, 0)) == [p3]
    assert p3.intersection(p4) == []

    x_pos = Symbol('x', real=True, positive=True)
    p2_1 = Point(x_pos, 0)
    p2_2 = Point(0, x_pos)
    p2_3 = Point(-x_pos, 0)
    p2_4 = Point(0, -x_pos)
    p2_5 = Point(x_pos, 5)
    assert Point.is_concyclic(p2_1)
    assert Point.is_concyclic(p2_1, p2_2)
    assert Point.is_concyclic(p2_1, p2_2, p2_3, p2_4)
    for pts in permutations((p2_1, p2_2, p2_3, p2_5)):
        assert Point.is_concyclic(*pts) is False
    assert Point.is_concyclic(p4, p4 * 2, p4 * 3) is False
    assert Point(0, 0).is_concyclic((1, 1), (2, 2), (2, 1)) is False

    assert p4.scale(2, 3) == Point(2, 3)
    assert p3.scale(2, 3) == p3

    assert p4.rotate(pi, Point(0.5, 0.5)) == p3
    assert p1.__radd__(p2) == p1.midpoint(p2).scale(2, 2)
    assert (-p3).__rsub__(p4) == p3.midpoint(p4).scale(2, 2)

    assert p4 * 5 == Point(5, 5)
    assert p4 / 5 == Point(0.2, 0.2)
    assert 5 * p4 == Point(5, 5)

    raises(ValueError, lambda: Point(0, 0) + 10)

    # Point differences should be simplified
    assert Point(x * (x - 1), y) - Point(x**2 - x, y + 1) == Point(0, -1)

    a, b = S.Half, Rational(1, 3)
    assert Point(a, b).evalf(2) == \
        Point(a.n(2), b.n(2), evaluate=False)
    raises(ValueError, lambda: Point(1, 2) + 1)

    # test transformations
    p = Point(1, 0)
    assert p.rotate(pi / 2) == Point(0, 1)
    assert p.rotate(pi / 2, p) == p
    p = Point(1, 1)
    assert p.scale(2, 3) == Point(2, 3)
    assert p.translate(1, 2) == Point(2, 3)
    assert p.translate(1) == Point(2, 1)
    assert p.translate(y=1) == Point(1, 2)
    assert p.translate(*p.args) == Point(2, 2)

    # Check invalid input for transform
    raises(ValueError, lambda: p3.transform(p3))
    raises(ValueError, lambda: p.transform(Matrix([[1, 0], [0, 1]])))
Exemple #11
0
def test_point():
    p1 = Point(x1, x2)
    p2 = Point(y1, y2)
    p3 = Point(0, 0)
    p4 = Point(1, 1)
    p5 = Point(0, 1)

    assert p1 in p1
    assert p1 not in p2
    assert p2.y == y2
    assert (p3 + p4) == p4
    assert (p2 - p1) == Point(y1 - x1, y2 - x2)
    assert p4 * 5 == Point(5, 5)
    assert -p2 == Point(-y1, -y2)

    assert Point.midpoint(p3, p4) == Point(half, half)
    assert Point.midpoint(p1, p4) == Point(half + half * x1, half + half * x2)
    assert Point.midpoint(p2, p2) == p2
    assert p2.midpoint(p2) == p2

    assert Point.distance(p3, p4) == sqrt(2)
    assert Point.distance(p1, p1) == 0
    assert Point.distance(p3, p2) == sqrt(p2.x**2 + p2.y**2)

    p1_1 = Point(x1, x1)
    p1_2 = Point(y2, y2)
    p1_3 = Point(x1 + 1, x1)
    assert Point.is_collinear(p3)
    assert Point.is_collinear(p3, p4)
    assert Point.is_collinear(p3, p4, p1_1, p1_2)
    assert Point.is_collinear(p3, p4, p1_1, p1_3) is False
    assert Point.is_collinear(p3, p3, p4, p5) is False

    assert p3.intersection(Point(0, 0)) == [p3]
    assert p3.intersection(p4) == []

    x_pos = Symbol('x', real=True, positive=True)
    p2_1 = Point(x_pos, 0)
    p2_2 = Point(0, x_pos)
    p2_3 = Point(-x_pos, 0)
    p2_4 = Point(0, -x_pos)
    p2_5 = Point(x_pos, 5)
    assert Point.is_concyclic(p2_1)
    assert Point.is_concyclic(p2_1, p2_2)
    assert Point.is_concyclic(p2_1, p2_2, p2_3, p2_4)
    assert Point.is_concyclic(p2_1, p2_2, p2_3, p2_5) is False
    assert Point.is_concyclic(p4, p4 * 2, p4 * 3) is False

    assert p4.scale(2, 3) == Point(2, 3)
    assert p3.scale(2, 3) == p3

    assert p4.rotate(pi, Point(0.5, 0.5)) == p3
    assert p1.__radd__(p2) == p1.midpoint(p2).scale(2, 2)
    assert (-p3).__rsub__(p4) == p3.midpoint(p4).scale(2, 2)

    assert p4 * 5 == Point(5, 5)
    assert p4 / 5 == Point(0.2, 0.2)

    raises(ValueError, lambda: Point(0, 0) + 10)

    # Point differences should be simplified
    assert Point(x * (x - 1), y) - Point(x**2 - x, y + 1) == Point(0, -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 = Point(1, 0)
    assert p.rotate(pi / 2) == Point(0, 1)
    assert p.rotate(pi / 2, p) == p
    p = Point(1, 1)
    assert p.scale(2, 3) == Point(2, 3)
    assert p.translate(1, 2) == Point(2, 3)
    assert p.translate(1) == Point(2, 1)
    assert p.translate(y=1) == Point(1, 2)
    assert p.translate(*p.args) == Point(2, 2)
def generate_map(): 
 

	screen.fill((0, 0, 0))

	points = generate_random_points(num_points, width, height, buf)


	#for x, y in points:
	#	pygame.draw.circle(screen, WHITE, (x,y), 2, 1)

	voronoi_context = voronoi(points)

	voronoi_point_dict = {}
	point_to_segment_dict = {}
	segments = []
	vertices = []

	top_l =  Point(0,0)
	top_r = Point(width,0)
	bottom_l = Point(0, height)
	bottom_r = Point(width, height) 

	top = Line(top_l, top_r) 
	left = Line(top_l, bottom_l) 
	right = Line(top_r, bottom_r) 
	bottom = Line(bottom_l, bottom_r) 

	boundaries = [top, right, bottom, left]

	for edge in voronoi_context.edges:
		il, i1, i2 = edge # index of line, index of vertex 1, index of vertex 2

		line_color = RED 

		vert1 = None
		vert2 = None
		print_line = True

		if i1 is not -1 and i2 is not -1:
			vert1 = voronoi_context.vertices[i1]
			vert2 = voronoi_context.vertices[i2]

		else:
			line_point = None

			if i1 is -1:
				line_p = voronoi_context.vertices[i2]
			if i2 is -1: 
				line_p = voronoi_context.vertices[i1]

			line_point = Point(line_p[0], line_p[1])
			line = voronoi_context.lines[il] 

			p1 = None
			p2 = None
			if line[1] == 0:
				p1 = line_point
				p2 = Point(line[0]/line[2], 1)
			else: 
				p1 = Point(0, line[2]/line[1])
				p2 = line_point

			l = Line(p1, p2)

			top_intersect = l.intersection(top)
			bottom_intersect = l.intersection(bottom)
			right_intersect = l.intersection(right)
			left_intersect = l.intersection(left)

			distances = []

			top_dist = None
			bottom_dist = None
			right_dist = None
			left_dist = None

			if len(top_intersect) != 0: 
				top_dist = abs(line_point.distance(top_intersect[0]))
				distances.append(top_dist)
			if len(bottom_intersect) != 0 : 
				bottom_dist = abs(line_point.distance(bottom_intersect[0]))
				distances.append(bottom_dist)
			if len(right_intersect) != 0:
				right_dist = abs(line_point.distance(right_intersect[0]))
				distances.append(right_dist)
			if len(left_intersect) != 0: 
				left_dist = abs(line_point.distance(left_intersect[0]))
				distances.append(left_dist)

			vert1 = line_p 
			v2 = None

			if top_dist == min(distances):
				v2 = top_intersect[0]
			elif bottom_dist == min(distances):
				v2 = bottom_intersect[0]
			elif right_dist == min(distances):
				v2 = right_intersect[0]
			elif left_dist == min(distances):
				v2 = left_intersect[0]
			else: 
				v2 = Point(0, 0)
			
			vert2 = (v2.x, v2.y)

			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:
				print_line = False

		if print_line:
			vert1, vert2 = adjust_out_of_bounds_points(vert1, vert2, boundaries)

		seg = None
		if vert1 == None or vert2 == None:
			print_line = False 
		if print_line: 
			vert1_p = Point(vert1)
			vert2_p = Point(vert2)
			seg = Segment(vert1_p, vert2_p)
			segments.append(seg)
		
			if not vert1_p in voronoi_point_dict:
				voronoi_point_dict[vert1_p] = []
			if not vert2_p in voronoi_point_dict:
				voronoi_point_dict[vert2_p] = []	

	 		voronoi_point_dict[vert1_p].append(vert2_p)
	 		voronoi_point_dict[vert2_p].append(vert1_p) 

	 		if not vert1_p in point_to_segment_dict:
	 			point_to_segment_dict[vert1_p] = []
	 		if not vert2_p in point_to_segment_dict:
	 			point_to_segment_dict[vert2_p] = []

	 		point_to_segment_dict[vert1_p].append(seg)
	 		point_to_segment_dict[vert2_p].append(seg)
	 	
			pygame.draw.line(screen, line_color, vert1, vert2, 1)

	pygame.display.flip()

	top_intersects = []
	bottom_intersects = [] 
	right_intersects = [] 
	left_intersects = [] 

	for seg in segments:
		if seg.p1.y <= 1:
			top_intersects.append(seg.p1)
		if seg.p2.y <= 1:
			top_intersects.append(seg.p2)
		if seg.p1.x >= width -1: 
			right_intersects.append(seg.p1)
		if seg.p2.x >= width-1: 
			right_intersects.append(seg.p2)
		if seg.p1.x <= 1:
			left_intersects.append(seg.p1)
		if seg.p2.x <= 1:
			left_intersects.append(seg.p2)
		if seg.p1.y >= height-1: 
			bottom_intersects.append(seg.p1)
		if seg.p2.y >= height-1: 
			bottom_intersects.append(seg.p2)

	top_intersects = sorted(top_intersects, key=lambda point: point.x)
	bottom_intersects = sorted(bottom_intersects, key=lambda point: point.x)
	left_intersects = sorted(left_intersects, key=lambda point: point.y)
	right_intersects = sorted(right_intersects, key=lambda point: point.y)

	for i in range(0, 4):
		intersect = None
		prev_vertex = None
		final_vertex = None

		if i == 0:
			prev_vertex = top_l
			intersect = top_intersects
			intersect.append(top_r)
		if i == 1:
			prev_vertex = bottom_l
			intersect = bottom_intersects
			intersect.append(bottom_r)
		if i == 2:
			prev_vertex = top_l
			intersect = left_intersects
			intersect.append(bottom_l)
		if i == 3:
			prev_vertex = top_r
			intersect = right_intersects
			intersect.append(bottom_r)

		if not prev_vertex in voronoi_point_dict:
			voronoi_point_dict[prev_vertex] = []
		if not final_vertex in voronoi_point_dict:
			voronoi_point_dict[final_vertex] = []

		if not prev_vertex in point_to_segment_dict:
	 		point_to_segment_dict[prev_vertex] = []
	 	if not final_vertex in point_to_segment_dict:
	 		point_to_segment_dict[final_vertex] = []

	 		

		for vertex in intersect:
			if not vertex in voronoi_point_dict:
				voronoi_point_dict[vertex] = []
			if not prev_vertex in voronoi_point_dict:
				voronoi_point_dict[prev_vertex] = []	
			s = Segment(prev_vertex, vertex)
		 	voronoi_point_dict[vertex].append(prev_vertex)
		 	voronoi_point_dict[prev_vertex].append(vertex)

		 	if not vertex in point_to_segment_dict:
	 			point_to_segment_dict[vertex] = []
	 		if not prev_vertex in point_to_segment_dict:
	 			point_to_segment_dict[prev_vertex] = []

	 		point_to_segment_dict[vertex].append(s)
	 		point_to_segment_dict[prev_vertex].append(s)

		 	prev_vertex = vertex
	 
	try: 
		polygons, segments_to_polygons = generate_polygons(voronoi_point_dict, segments, points, point_to_segment_dict)
	except Exception as e:
		print e 
		print "crashed"
		while 1:
			""" helllo"""
	for seg, gons in segments_to_polygons.iteritems():
		for gon in gons:
			gon.connect_adjacent_nodes(gons)

	for polygon in polygons:
		for node in polygon.adjacent_nodes:
			s = Segment(node.center, polygon.center)
			draw_segment(s, WHITE)

	highest_points_of_elevation = []
	frontiers = []

	for i in range(0, number_of_peaks):
		p = random.choice(polygons)
		p.elevation = max_elevation
		highest_points_of_elevation.append(p)

		frontiers.append(set(p.adjacent_nodes))

	marked_polygons = set([])	

	elevation = max_elevation
	while len(marked_polygons) < num_points:
		elevation -= 1 
		for i in range(0, number_of_peaks):
			new_frontier = set([])

			while len(frontiers[i]) > 0:
				node = frontiers[i].pop()
				node.elevation = elevation
				draw_point(node.center, ORANGE)

				for n in node.adjacent_nodes:
					if n not in marked_polygons:
						new_frontier.add(n)

				marked_polygons.add(node)
			frontiers[i] = new_frontier


	for polygon in polygons:
		if polygon.elevation <= 0:
			vertices = []
			for edge in polygon.edge_list:
				p = (edge.x, edge.y)
				vertices.append(p)
			pygame.draw.polygon(screen, BLUE, vertices, 0)
			pygame.display.flip()
	pygame.display.flip()
Exemple #13
0
def intersectionsInsideSquareBASE(lineSegmentT, xCoordinates, yCoordinates):
    p1 = Point(xCoordinates[0], yCoordinates[0])
    p2 = Point(xCoordinates[1], yCoordinates[1])
    p3 = Point(xCoordinates[2], yCoordinates[2])
    p4 = Point(xCoordinates[3], yCoordinates[3])

    lp1 = Point(lineSegmentT.X1, lineSegmentT.Y1)
    lp2 = Point(lineSegmentT.X2, lineSegmentT.Y2)

    gcodeSegment = Segment(lp1, lp2)
    square = Polygon(p1, p2, p3, p4)

    insideSquare = False

    startTime = parser.parse(lineSegmentT.timeStart)
    stopTime = parser.parse(lineSegmentT.timeStop)

    startTime1 = startTime

    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')

    ax.plot([lineSegmentT.X1, lineSegmentT.X2],
            [lineSegmentT.Y1, lineSegmentT.Y2],
            color='r',
            linewidth=5.0)

    for i in range(4):
        ax.plot([xCoordinates[i], xCoordinates[i + 1]],
                [yCoordinates[i], yCoordinates[i + 1]],
                color='k',
                linewidth=1.0)

    interSections = gcodeSegment.intersection(square)

    print('intersection size: ', np.size(interSections))
    print('Intersection results', interSections)
    insideSquare = False
    print('Square Encloses: ', lp1, square.encloses(lp1))
    print('Square Encloses: ', lp2, square.encloses(lp2))

    d = lp1.distance(lp2)
    timewidth = stopTime - startTime

    if np.size(interSections) == 4:
        insideSquare = True

        d1 = lp1.distance(interSections[0])
        d2 = lp1.distance(interSections[1])

        xt1, yt1 = interSections[0]
        xt2, yt2 = interSections[1]
        ax.plot([xt1, xt2], [yt1, yt2], color='g', linewidth=5.0)
        if d1 < d2:
            startTime = startTime1 + timewidth * float(d1 / d)
            stopTime = startTime1 + timewidth * float(d2 / d)

        else:
            startTime = startTime1 + timewidth * float(d2 / d)
            stopTime = startTime1 + timewidth * float(d1 / d)

    elif np.size(interSections) == 2:
        if square.encloses(lp1):
            insideSquare = True
            d1 = lp1.distance(interSections[0])
            xt1, yt1 = lp1
            xt2, yt2 = interSections[0]
            ax.plot([xt1, xt2], [yt1, yt2], color='g', linewidth=5.0)
            stopTime = startTime1 + timewidth * float(d1 / d)

        elif square.encloses(lp2):
            insideSquare = True
            d1 = lp1.distance(interSections[0])
            xt1, yt1 = lp2
            xt2, yt2 = interSections[0]
            ax.plot([xt1, xt2], [yt1, yt2], color='g', linewidth=5.0)
            startTime = startTime1 + timewidth * float(d1 / d)

        elif ('Segment' in str(type(interSections[0]))):
            insideSquare = True
            xt1, yt1 = interSections[0].p1
            xt2, yt2 = interSections[0].p2
            ax.plot([xt1, xt2], [yt1, yt2], color='g', linewidth=5.0)

            d1 = lp1.distance(interSections[0].p1)
            d2 = lp1.distance(interSections[0].p2)

            lengthtointersection = lp1.distance(interSections[1])

            if interSections[0].contains(lp2):
                startTime = startTime1 + timewidth * float(
                    lengthtointersection / d)
            else:
                stopTime = startTime1 + timewidth * float(
                    lengthtointersection / d)

        else:
            insideSquare = False

    elif np.size(interSections) == 1:
        if ('Segment' in str(type(interSections[0]))):
            insideSquare = True
            xt1, yt1 = interSections[0].p1
            xt2, yt2 = interSections[0].p2
            ax.plot([xt1, xt2], [yt1, yt2], color='g', linewidth=5.0)

    elif np.size(interSections) == 3:
        insideSquare = True
        for val in interSections:
            if ('Segment' in str(type(val))):
                xt1, yt1 = val.p1
                xt2, yt2 = val.p2
                ax.plot([xt1, xt2], [yt1, yt2], color='g', linewidth=5.0)

                d1 = lp1.distance(val.p1)
                d2 = lp1.distance(val.p2)

                if d1 < d2:
                    startTime = startTime1 + timewidth * float(d1 / d)
                    stopTime = startTime1 + timewidth * float(d2 / d)
                else:
                    startTime = startTime1 + timewidth * float(d2 / d)
                    stopTime = startTime1 + timewidth * float(d1 / d)

    elif np.size(interSections) == 0:
        if square.encloses(lp1) and square.encloses(lp2):
            insideSquare = True
            xt1, yt1 = lp1
            xt2, yt2 = lp2
            ax.plot([xt1, xt2], [yt1, yt2], color='g', linewidth=5.0)

    else:
        insideSquare = False

    plt.show()

    return insideSquare, startTime, stopTime
Exemple #14
0
def test_point():
    x = Symbol('x', real=True)
    y = Symbol('y', real=True)
    x1 = Symbol('x1', real=True)
    x2 = Symbol('x2', real=True)
    y1 = Symbol('y1', real=True)
    y2 = Symbol('y2', real=True)
    half = Rational(1, 2)
    p1 = Point(x1, x2)
    p2 = Point(y1, y2)
    p3 = Point(0, 0)
    p4 = Point(1, 1)
    p5 = Point(0, 1)

    assert p1 in p1
    assert p1 not in p2
    assert p2.y == y2
    assert (p3 + p4) == p4
    assert (p2 - p1) == Point(y1 - x1, y2 - x2)
    assert p4*5 == Point(5, 5)
    assert -p2 == Point(-y1, -y2)
    raises(ValueError, lambda: Point(3, I))
    raises(ValueError, lambda: Point(2*I, I))
    raises(ValueError, lambda: Point(3 + I, I))

    assert Point(34.05, sqrt(3)) == Point(Rational(681, 20), sqrt(3))
    assert Point.midpoint(p3, p4) == Point(half, half)
    assert Point.midpoint(p1, p4) == Point(half + half*x1, half + half*x2)
    assert Point.midpoint(p2, p2) == p2
    assert p2.midpoint(p2) == p2

    assert Point.distance(p3, p4) == sqrt(2)
    assert Point.distance(p1, p1) == 0
    assert Point.distance(p3, p2) == sqrt(p2.x**2 + p2.y**2)

    assert Point.taxicab_distance(p4, p3) == 2

    p1_1 = Point(x1, x1)
    p1_2 = Point(y2, y2)
    p1_3 = Point(x1 + 1, x1)
    assert Point.is_collinear(p3)
    assert Point.is_collinear(p3, p4)
    assert Point.is_collinear(p3, p4, p1_1, p1_2)
    assert Point.is_collinear(p3, p4, p1_1, p1_3) is False
    assert Point.is_collinear(p3, p3, p4, p5) is False
    line = Line(Point(1,0), slope = 1)
    raises(TypeError, lambda: Point.is_collinear(line))
    raises(TypeError, lambda: p1_1.is_collinear(line))

    assert p3.intersection(Point(0, 0)) == [p3]
    assert p3.intersection(p4) == []

    assert p1.dot(p4) == x1 + x2
    assert p3.dot(p4) == 0
    assert p4.dot(p5) == 1

    x_pos = Symbol('x', real=True, positive=True)
    p2_1 = Point(x_pos, 0)
    p2_2 = Point(0, x_pos)
    p2_3 = Point(-x_pos, 0)
    p2_4 = Point(0, -x_pos)
    p2_5 = Point(x_pos, 5)
    assert Point.is_concyclic(p2_1)
    assert Point.is_concyclic(p2_1, p2_2)
    assert Point.is_concyclic(p2_1, p2_2, p2_3, p2_4)
    assert Point.is_concyclic(p2_1, p2_2, p2_3, p2_5) is False
    assert Point.is_concyclic(p4, p4 * 2, p4 * 3) is False

    assert p4.scale(2, 3) == Point(2, 3)
    assert p3.scale(2, 3) == p3

    assert p4.rotate(pi, Point(0.5, 0.5)) == p3
    assert p1.__radd__(p2) == p1.midpoint(p2).scale(2, 2)
    assert (-p3).__rsub__(p4) == p3.midpoint(p4).scale(2, 2)

    assert p4 * 5 == Point(5, 5)
    assert p4 / 5 == Point(0.2, 0.2)

    raises(ValueError, lambda: Point(0, 0) + 10)

    # Point differences should be simplified
    assert Point(x*(x - 1), y) - Point(x**2 - x, y + 1) == Point(0, -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 = Point(1, 0)
    assert p.rotate(pi/2) == Point(0, 1)
    assert p.rotate(pi/2, p) == p
    p = Point(1, 1)
    assert p.scale(2, 3) == Point(2, 3)
    assert p.translate(1, 2) == Point(2, 3)
    assert p.translate(1) == Point(2, 1)
    assert p.translate(y=1) == Point(1, 2)
    assert p.translate(*p.args) == Point(2, 2)

    # Check invalid input for transform
    raises(ValueError, lambda: p3.transform(p3))
    raises(ValueError, lambda: p.transform(Matrix([[1, 0], [0, 1]])))
Exemple #15
0
def generate_map():

    screen.fill((0, 0, 0))

    points = generate_random_points(num_points, width, height, buf)

    #for x, y in points:
    #	pygame.draw.circle(screen, WHITE, (x,y), 2, 1)

    voronoi_context = voronoi(points)

    voronoi_point_dict = {}
    point_to_segment_dict = {}
    segments = []
    vertices = []

    top_l = Point(0, 0)
    top_r = Point(width, 0)
    bottom_l = Point(0, height)
    bottom_r = Point(width, height)

    top = Line(top_l, top_r)
    left = Line(top_l, bottom_l)
    right = Line(top_r, bottom_r)
    bottom = Line(bottom_l, bottom_r)

    boundaries = [top, right, bottom, left]

    for edge in voronoi_context.edges:
        il, i1, i2 = edge  # index of line, index of vertex 1, index of vertex 2

        line_color = RED

        vert1 = None
        vert2 = None
        print_line = True

        if i1 is not -1 and i2 is not -1:
            vert1 = voronoi_context.vertices[i1]
            vert2 = voronoi_context.vertices[i2]

        else:
            line_point = None

            if i1 is -1:
                line_p = voronoi_context.vertices[i2]
            if i2 is -1:
                line_p = voronoi_context.vertices[i1]

            line_point = Point(line_p[0], line_p[1])
            line = voronoi_context.lines[il]

            p1 = None
            p2 = None
            if line[1] == 0:
                p1 = line_point
                p2 = Point(line[0] / line[2], 1)
            else:
                p1 = Point(0, line[2] / line[1])
                p2 = line_point

            l = Line(p1, p2)

            top_intersect = l.intersection(top)
            bottom_intersect = l.intersection(bottom)
            right_intersect = l.intersection(right)
            left_intersect = l.intersection(left)

            distances = []

            top_dist = None
            bottom_dist = None
            right_dist = None
            left_dist = None

            if len(top_intersect) != 0:
                top_dist = abs(line_point.distance(top_intersect[0]))
                distances.append(top_dist)
            if len(bottom_intersect) != 0:
                bottom_dist = abs(line_point.distance(bottom_intersect[0]))
                distances.append(bottom_dist)
            if len(right_intersect) != 0:
                right_dist = abs(line_point.distance(right_intersect[0]))
                distances.append(right_dist)
            if len(left_intersect) != 0:
                left_dist = abs(line_point.distance(left_intersect[0]))
                distances.append(left_dist)

            vert1 = line_p
            v2 = None

            if top_dist == min(distances):
                v2 = top_intersect[0]
            elif bottom_dist == min(distances):
                v2 = bottom_intersect[0]
            elif right_dist == min(distances):
                v2 = right_intersect[0]
            elif left_dist == min(distances):
                v2 = left_intersect[0]
            else:
                v2 = Point(0, 0)

            vert2 = (v2.x, v2.y)

            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:
                print_line = False

        if print_line:
            vert1, vert2 = adjust_out_of_bounds_points(vert1, vert2,
                                                       boundaries)

        seg = None
        if vert1 == None or vert2 == None:
            print_line = False
        if print_line:
            vert1_p = Point(vert1)
            vert2_p = Point(vert2)
            seg = Segment(vert1_p, vert2_p)
            segments.append(seg)

            if not vert1_p in voronoi_point_dict:
                voronoi_point_dict[vert1_p] = []
            if not vert2_p in voronoi_point_dict:
                voronoi_point_dict[vert2_p] = []

            voronoi_point_dict[vert1_p].append(vert2_p)
            voronoi_point_dict[vert2_p].append(vert1_p)

            if not vert1_p in point_to_segment_dict:
                point_to_segment_dict[vert1_p] = []
            if not vert2_p in point_to_segment_dict:
                point_to_segment_dict[vert2_p] = []

            point_to_segment_dict[vert1_p].append(seg)
            point_to_segment_dict[vert2_p].append(seg)

            pygame.draw.line(screen, line_color, vert1, vert2, 1)

    pygame.display.flip()

    top_intersects = []
    bottom_intersects = []
    right_intersects = []
    left_intersects = []

    for seg in segments:
        if seg.p1.y <= 1:
            top_intersects.append(seg.p1)
        if seg.p2.y <= 1:
            top_intersects.append(seg.p2)
        if seg.p1.x >= width - 1:
            right_intersects.append(seg.p1)
        if seg.p2.x >= width - 1:
            right_intersects.append(seg.p2)
        if seg.p1.x <= 1:
            left_intersects.append(seg.p1)
        if seg.p2.x <= 1:
            left_intersects.append(seg.p2)
        if seg.p1.y >= height - 1:
            bottom_intersects.append(seg.p1)
        if seg.p2.y >= height - 1:
            bottom_intersects.append(seg.p2)

    top_intersects = sorted(top_intersects, key=lambda point: point.x)
    bottom_intersects = sorted(bottom_intersects, key=lambda point: point.x)
    left_intersects = sorted(left_intersects, key=lambda point: point.y)
    right_intersects = sorted(right_intersects, key=lambda point: point.y)

    for i in range(0, 4):
        intersect = None
        prev_vertex = None
        final_vertex = None

        if i == 0:
            prev_vertex = top_l
            intersect = top_intersects
            intersect.append(top_r)
        if i == 1:
            prev_vertex = bottom_l
            intersect = bottom_intersects
            intersect.append(bottom_r)
        if i == 2:
            prev_vertex = top_l
            intersect = left_intersects
            intersect.append(bottom_l)
        if i == 3:
            prev_vertex = top_r
            intersect = right_intersects
            intersect.append(bottom_r)

        if not prev_vertex in voronoi_point_dict:
            voronoi_point_dict[prev_vertex] = []
        if not final_vertex in voronoi_point_dict:
            voronoi_point_dict[final_vertex] = []

        if not prev_vertex in point_to_segment_dict:
            point_to_segment_dict[prev_vertex] = []
        if not final_vertex in point_to_segment_dict:
            point_to_segment_dict[final_vertex] = []

        for vertex in intersect:
            if not vertex in voronoi_point_dict:
                voronoi_point_dict[vertex] = []
            if not prev_vertex in voronoi_point_dict:
                voronoi_point_dict[prev_vertex] = []
            s = Segment(prev_vertex, vertex)
            voronoi_point_dict[vertex].append(prev_vertex)
            voronoi_point_dict[prev_vertex].append(vertex)

            if not vertex in point_to_segment_dict:
                point_to_segment_dict[vertex] = []
            if not prev_vertex in point_to_segment_dict:
                point_to_segment_dict[prev_vertex] = []

            point_to_segment_dict[vertex].append(s)
            point_to_segment_dict[prev_vertex].append(s)

            prev_vertex = vertex

    try:
        polygons, segments_to_polygons = generate_polygons(
            voronoi_point_dict, segments, points, point_to_segment_dict)
    except Exception as e:
        print e
        print "crashed"
        while 1:
            """ helllo"""
    for seg, gons in segments_to_polygons.iteritems():
        for gon in gons:
            gon.connect_adjacent_nodes(gons)

    for polygon in polygons:
        for node in polygon.adjacent_nodes:
            s = Segment(node.center, polygon.center)
            draw_segment(s, WHITE)

    highest_points_of_elevation = []
    frontiers = []

    for i in range(0, number_of_peaks):
        p = random.choice(polygons)
        p.elevation = max_elevation
        highest_points_of_elevation.append(p)

        frontiers.append(set(p.adjacent_nodes))

    marked_polygons = set([])

    elevation = max_elevation
    while len(marked_polygons) < num_points:
        elevation -= 1
        for i in range(0, number_of_peaks):
            new_frontier = set([])

            while len(frontiers[i]) > 0:
                node = frontiers[i].pop()
                node.elevation = elevation
                draw_point(node.center, ORANGE)

                for n in node.adjacent_nodes:
                    if n not in marked_polygons:
                        new_frontier.add(n)

                marked_polygons.add(node)
            frontiers[i] = new_frontier

    for polygon in polygons:
        if polygon.elevation <= 0:
            vertices = []
            for edge in polygon.edge_list:
                p = (edge.x, edge.y)
                vertices.append(p)
            pygame.draw.polygon(screen, BLUE, vertices, 0)
            pygame.display.flip()
    pygame.display.flip()
Exemple #16
0
def test_point():
    x = Symbol('x', real=True)
    y = Symbol('y', real=True)
    x1 = Symbol('x1', real=True)
    x2 = Symbol('x2', real=True)
    y1 = Symbol('y1', real=True)
    y2 = Symbol('y2', real=True)
    half = S.Half
    p1 = Point(x1, x2)
    p2 = Point(y1, y2)
    p3 = Point(0, 0)
    p4 = Point(1, 1)
    p5 = Point(0, 1)
    line = Line(Point(1, 0), slope=1)

    assert p1 in p1
    assert p1 not in p2
    assert p2.y == y2
    assert (p3 + p4) == p4
    assert (p2 - p1) == Point(y1 - x1, y2 - x2)
    assert -p2 == Point(-y1, -y2)
    raises(TypeError, lambda: Point(1))
    raises(ValueError, lambda: Point([1]))
    raises(ValueError, lambda: Point(3, I))
    raises(ValueError, lambda: Point(2*I, I))
    raises(ValueError, lambda: Point(3 + I, I))

    assert Point(34.05, sqrt(3)) == Point(Rational(681, 20), sqrt(3))
    assert Point.midpoint(p3, p4) == Point(half, half)
    assert Point.midpoint(p1, p4) == Point(half + half*x1, half + half*x2)
    assert Point.midpoint(p2, p2) == p2
    assert p2.midpoint(p2) == p2
    assert p1.origin == Point(0, 0)

    assert Point.distance(p3, p4) == sqrt(2)
    assert Point.distance(p1, p1) == 0
    assert Point.distance(p3, p2) == sqrt(p2.x**2 + p2.y**2)
    raises(TypeError, lambda: Point.distance(p1, 0))
    raises(TypeError, lambda: Point.distance(p1, GeometryEntity()))

    # distance should be symmetric
    assert p1.distance(line) == line.distance(p1)
    assert p4.distance(line) == line.distance(p4)

    assert Point.taxicab_distance(p4, p3) == 2

    assert Point.canberra_distance(p4, p5) == 1
    raises(ValueError, lambda: Point.canberra_distance(p3, p3))

    p1_1 = Point(x1, x1)
    p1_2 = Point(y2, y2)
    p1_3 = Point(x1 + 1, x1)
    assert Point.is_collinear(p3)

    with warns(UserWarning, test_stacklevel=False):
        assert Point.is_collinear(p3, Point(p3, dim=4))
    assert p3.is_collinear()
    assert Point.is_collinear(p3, p4)
    assert Point.is_collinear(p3, p4, p1_1, p1_2)
    assert Point.is_collinear(p3, p4, p1_1, p1_3) is False
    assert Point.is_collinear(p3, p3, p4, p5) is False

    raises(TypeError, lambda: Point.is_collinear(line))
    raises(TypeError, lambda: p1_1.is_collinear(line))

    assert p3.intersection(Point(0, 0)) == [p3]
    assert p3.intersection(p4) == []
    assert p3.intersection(line) == []
    with warns(UserWarning, test_stacklevel=False):
        assert Point.intersection(Point(0, 0, 0), Point(0, 0)) == [Point(0, 0, 0)]

    x_pos = Symbol('x', positive=True)
    p2_1 = Point(x_pos, 0)
    p2_2 = Point(0, x_pos)
    p2_3 = Point(-x_pos, 0)
    p2_4 = Point(0, -x_pos)
    p2_5 = Point(x_pos, 5)
    assert Point.is_concyclic(p2_1)
    assert Point.is_concyclic(p2_1, p2_2)
    assert Point.is_concyclic(p2_1, p2_2, p2_3, p2_4)
    for pts in permutations((p2_1, p2_2, p2_3, p2_5)):
        assert Point.is_concyclic(*pts) is False
    assert Point.is_concyclic(p4, p4 * 2, p4 * 3) is False
    assert Point(0, 0).is_concyclic((1, 1), (2, 2), (2, 1)) is False
    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

    assert p1.is_scalar_multiple(p1)
    assert p1.is_scalar_multiple(2*p1)
    assert not p1.is_scalar_multiple(p2)
    assert Point.is_scalar_multiple(Point(1, 1), (-1, -1))
    assert Point.is_scalar_multiple(Point(0, 0), (0, -1))
    # test when is_scalar_multiple can't be determined
    raises(Undecidable, lambda: Point.is_scalar_multiple(Point(sympify("x1%y1"), sympify("x2%y2")), Point(0, 1)))

    assert Point(0, 1).orthogonal_direction == Point(1, 0)
    assert Point(1, 0).orthogonal_direction == Point(0, 1)

    assert p1.is_zero is None
    assert p3.is_zero
    assert p4.is_zero is False
    assert p1.is_nonzero is None
    assert p3.is_nonzero is False
    assert p4.is_nonzero

    assert p4.scale(2, 3) == Point(2, 3)
    assert p3.scale(2, 3) == p3

    assert p4.rotate(pi, Point(0.5, 0.5)) == p3
    assert p1.__radd__(p2) == p1.midpoint(p2).scale(2, 2)
    assert (-p3).__rsub__(p4) == p3.midpoint(p4).scale(2, 2)

    assert p4 * 5 == Point(5, 5)
    assert p4 / 5 == Point(0.2, 0.2)
    assert 5 * p4 == Point(5, 5)

    raises(ValueError, lambda: Point(0, 0) + 10)

    # Point differences should be simplified
    assert Point(x*(x - 1), y) - Point(x**2 - x, y + 1) == Point(0, -1)

    a, b = S.Half, Rational(1, 3)
    assert Point(a, b).evalf(2) == \
        Point(a.n(2), b.n(2), evaluate=False)
    raises(ValueError, lambda: Point(1, 2) + 1)

    # test project
    assert Point.project((0, 1), (1, 0)) == Point(0, 0)
    assert Point.project((1, 1), (1, 0)) == Point(1, 0)
    raises(ValueError, lambda: Point.project(p1, Point(0, 0)))

    # test transformations
    p = Point(1, 0)
    assert p.rotate(pi/2) == Point(0, 1)
    assert p.rotate(pi/2, p) == p
    p = Point(1, 1)
    assert p.scale(2, 3) == Point(2, 3)
    assert p.translate(1, 2) == Point(2, 3)
    assert p.translate(1) == Point(2, 1)
    assert p.translate(y=1) == Point(1, 2)
    assert p.translate(*p.args) == Point(2, 2)

    # Check invalid input for transform
    raises(ValueError, lambda: p3.transform(p3))
    raises(ValueError, lambda: p.transform(Matrix([[1, 0], [0, 1]])))

    # test __contains__
    assert 0 in Point(0, 0, 0, 0)
    assert 1 not in Point(0, 0, 0, 0)

    # test affine_rank
    assert Point.affine_rank() == -1
Exemple #17
0
def test_point():
    p1 = Point(x1, x2)
    p2 = Point(y1, y2)
    p3 = Point(0, 0)
    p4 = Point(1, 1)

    assert p1 in p1
    assert p1 not in p2
    assert p2.y == y2
    assert (p3 + p4) == p4
    assert (p2 - p1) == Point(y1 - x1, y2 - x2)
    assert p4 * 5 == Point(5, 5)
    assert -p2 == Point(-y1, -y2)

    assert Point.midpoint(p3, p4) == Point(half, half)
    assert Point.midpoint(p1, p4) == Point(half + half * x1, half + half * x2)
    assert Point.midpoint(p2, p2) == p2
    assert p2.midpoint(p2) == p2

    assert Point.distance(p3, p4) == sqrt(2)
    assert Point.distance(p1, p1) == 0
    assert Point.distance(p3, p2) == sqrt(p2.x ** 2 + p2.y ** 2)

    p1_1 = Point(x1, x1)
    p1_2 = Point(y2, y2)
    p1_3 = Point(x1 + 1, x1)
    assert Point.is_collinear(p3)
    assert Point.is_collinear(p3, p4)
    assert Point.is_collinear(p3, p4, p1_1, p1_2)
    assert Point.is_collinear(p3, p4, p1_1, p1_3) == False

    assert p3.intersection(Point(0, 0)) == [p3]
    assert p3.intersection(p4) == []

    x_pos = Symbol("x", real=True, positive=True)
    p2_1 = Point(x_pos, 0)
    p2_2 = Point(0, x_pos)
    p2_3 = Point(-x_pos, 0)
    p2_4 = Point(0, -x_pos)
    p2_5 = Point(x_pos, 5)
    assert Point.is_concyclic(p2_1)
    assert Point.is_concyclic(p2_1, p2_2)
    assert Point.is_concyclic(p2_1, p2_2, p2_3, p2_4)
    assert Point.is_concyclic(p2_1, p2_2, p2_3, p2_5) == False
    assert Point.is_concyclic(p4, p4 * 2, p4 * 3) == False

    assert p4.scale(2, 3) == Point(2, 3)
    assert p3.scale(2, 3) == p3

    assert p4.rotate(pi, Point(0.5, 0.5)) == p3
    assert p1.__radd__(p2) == p1.midpoint(p2).scale(2, 2)
    assert (-p3).__rsub__(p4) == p3.midpoint(p4).scale(2, 2)

    assert p4 * 5 == Point(5, 5)
    assert p4 / 5 == Point(0.2, 0.2)

    raises(ValueError, lambda: Point(0, 0) + 10)

    # Point differences should be simplified
    assert Point(x * (x - 1), y) - Point(x ** 2 - x, y + 1) == Point(0, -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 = Point(1, 0)
    assert p.rotate(pi / 2) == Point(0, 1)
    assert p.rotate(pi / 2, p) == p
    p = Point(1, 1)
    assert p.scale(2, 3) == Point(2, 3)
    assert p.translate(1, 2) == Point(2, 3)
    assert p.translate(1) == Point(2, 1)
    assert p.translate(y=1) == Point(1, 2)
    assert p.translate(*p.args) == Point(2, 2)
Exemple #18
0
    def point_within_tolerance(self, point1, point2, tolerance=None):
        if tolerance is None:
            tolerance = self.tolerance['point_distance'] / self.xscale

        p1 = Point(*point1)
        return p1.distance(Point(*point2)) < tolerance