Ejemplo n.º 1
0
def test_concave_polygon():
	polygon = Polygon()
	polygon.add_vertex(Point2D(10,-10))
	polygon.add_vertex(Point2D(5,0))
	polygon.add_vertex(Point2D(10,10))
	polygon.add_vertex(Point2D(-10,10))
	polygon.add_vertex(Point2D(-5,0))
	polygon.add_vertex(Point2D(-10,-10))
	# test convexity
	c_true = False
	c_test = polygon.determine_convexity()
	assert c_test == c_true
	# test complexity
	c_true = False
	c_test = polygon.determine_complexity()
	assert c_test == c_true
	# test points in polygon
	point = Point2D(0,1)
	assert polygon.determine_point_location(point) == 1
	point = Point2D(-8,-10)
	assert polygon.determine_point_location(point) == 2
	point = Point2D(10,10)
	assert polygon.determine_point_location(point) == 2
	point = Point2D(10,0)
	assert polygon.determine_point_location(point) == 0
Ejemplo n.º 2
0
def test_init_distance():
    """
	Tests the basic and distance methods of the Point2D class.
	"""
    p1 = Point2D(1, 5)
    p2 = Point2D(-2, 2)
    d_true = 4.242640687119286
    d_test = p1.calculate_distance(p2)
    assert d_test == pytest.approx(d_true, 1e-12)
Ejemplo n.º 3
0
def test_point_distance():
	p1 = Point2D(-4,2)
	p2 = Point2D(6,2)
	p3 = Point2D(0,5.75)
	l = Line2D()
	l.add_endpoint(p1)
	l.add_endpoint(p2)
	d_true = 3.75
	d_test = l.calculate_point_distance(p3)
	assert d_test == d_true
Ejemplo n.º 4
0
def test_left_of():
    """
	Test the `is_left_of` method.
	"""
    p1 = Point2D(-2, 2)
    p2 = Point2D(2, -2)
    p = Point2D(0, 2)
    assert p.is_left_of(p1, p2)
    p = Point2D(0, -2)
    assert not p.is_left_of(p1, p2)
Ejemplo n.º 5
0
def test_init_add_vector():
	"""
	Tests the basic methods of the Line2D class.
	"""
	p1 = Point2D(1,5)
	p2 = Point2D(-2,2)
	l = Line2D(point1=p1)
	l.add_endpoint(p2)
	v = l.as_vector()
	assert isinstance(v,Vector)
Ejemplo n.º 6
0
def test_line_intersection_point():
	p1 = Point2D(-4,2)
	p2 = Point2D(6,2)
	l = Line2D()
	l.add_endpoint(p1)
	l.add_endpoint(p2) 
	l2 = Line2D(Point2D(2.45,-5),Point2D(2.45,10))
	p_true = Point2D(2.45,2)
	p_test = l.calculate_line_intersection_point(l2)
	assert p_test.x == p_true.x
	assert p_test.y == p_true.y
Ejemplo n.º 7
0
def test_point_location():
	p1 = Point2D(-4,2)
	p2 = Point2D(6,2)
	l = Line2D()
	l.add_endpoint(p1)
	l.add_endpoint(p2)        
	p = Point2D(0,5.75)
	assert l.determine_point_location(p) == 0
	p = Point2D(0,2)
	assert l.determine_point_location(p) == 1
	p = Point2D(-100,2)
	assert l.determine_point_location(p) == 2
Ejemplo n.º 8
0
def test_length_epsilon():
	p1 = Point2D(-4,2)
	p2 = Point2D(6,2)
	l = Line2D()
	l.add_endpoint(p1)
	l.add_endpoint(p2)
	d_true = 10
	d_test = l.calculate_length()
	assert d_test == d_true
	e_true = 1e-4
	e_test = l.calculate_epsilon()
	assert e_test == e_true
Ejemplo n.º 9
0
def test_line_intersection():
	p1 = Point2D(-4,2)
	p2 = Point2D(6,2)
	l = Line2D()
	l.add_endpoint(p1)
	l.add_endpoint(p2) 
	l2 = Line2D(Point2D(-2,-2),Point2D(2,3))
	assert l.determine_line_intersection(l2) == 1
	l2 = Line2D(Point2D(7,-2),Point2D(2,-3))
	assert l.determine_line_intersection(l2) == 2
	l2 = Line2D(Point2D(5,10),Point2D(-10,10))
	assert l.determine_line_parallel(l2)
Ejemplo n.º 10
0
def test_export_polygon(tmp_path):
    p1 = Polygon()
    p1.add_vertex(Point2D(-1, -1))
    p1.add_vertex(Point2D(-2, 1))
    p1.add_vertex(Point2D(-1, 5))
    p1.add_vertex(Point2D(-4, 3))
    p1.add_vertex(Point2D(-5, -3))
    p1.add_vertex(Point2D(-3, 0))
    p2 = Polygon()
    p2.add_vertex(Point2D(5, 5))
    p2.add_vertex(Point2D(6, 3))
    p2.add_vertex(Point2D(3, -3))
    p2.add_vertex(Point2D(2, 2))
    multi_polygon = MultiPolygon()
    multi_polygon.add_polygon(p1)
    multi_polygon.add_polygon(p2)
    export_polygon(multi_polygon, os.path.join(tmp_path, 'test.txt'))
    multi_polygon_test = read_polygon_file(os.path.join(tmp_path, 'test.txt'))
    assert multi_polygon_test.polygons[0].vertices[0].x == -1
    assert multi_polygon_test.polygons[1].vertices[2].y == -3
Ejemplo n.º 11
0
def test_line_parallel():
	p1 = Point2D(-4,2)
	p2 = Point2D(6,2)
	l = Line2D()
	l.add_endpoint(p1)
	l.add_endpoint(p2) 
	l2 = Line2D(Point2D(5,10),Point2D(-10,10))
	assert l.determine_line_parallel(l2)
	l2 = Line2D(Point2D(0,0),Point2D(10,10))
	assert not l.determine_line_parallel(l2)
Ejemplo n.º 12
0
def test_multipolygon():
	p1 = Polygon()
	p1.add_vertex(Point2D(-1,-1))
	p1.add_vertex(Point2D(-2,1))
	p1.add_vertex(Point2D(-1,5))
	p1.add_vertex(Point2D(-4,3))
	p1.add_vertex(Point2D(-5,-3))
	p1.add_vertex(Point2D(-3,0))
	p2 = Polygon()
	p2.add_vertex(Point2D(5,5))
	p2.add_vertex(Point2D(6,3))
	p2.add_vertex(Point2D(3,-3))
	p2.add_vertex(Point2D(2,2))        
	multi_polygon = MultiPolygon()
	multi_polygon.add_polygon(p1)
	multi_polygon.add_polygon(p2)        
	# test points in polygon
	point = Point2D(5,2)
	assert multi_polygon.determine_point_location(point) == 1
	point = Point2D(-4,-1)
	assert multi_polygon.determine_point_location(point) == 1
	point = Point2D(-2,0)
	assert multi_polygon.determine_point_location(point) == 1
	point = Point2D(-2,1)
	assert multi_polygon.determine_point_location(point) == 2
	point = Point2D(0,0)
	assert multi_polygon.determine_point_location(point) == 0
	point = Point2D(4,-3)
	assert multi_polygon.determine_point_location(point) == 0
	point = Point2D(-6,1)
	assert multi_polygon.determine_point_location(point) == 0
Ejemplo n.º 13
0
def test_triangle():
	P1 = Point2D(5,0)
	P2 = Point2D(0,5)
	P3 = Point2D(-5,0)
	points = (P1,P2,P3)
	a = 10.0
	b = np.sqrt(5.0**2.0+5.0**2.0)
	c = np.sqrt(5.0**2.0+5.0**2.0)
	triangle = Polygon(points)
	# test perimeter
	p_true = a+b+c
	p_test = triangle.calculate_perimeter()
	assert p_test == pytest.approx(p_true,1e-12)
	# test area
	s = (a+b+c)/2.0
	A_true = np.sqrt(s*(s-a)*(s-b)*(s-c))
	A_test = triangle.calculate_area()
	assert A_test == pytest.approx(A_true,1e-12)
	# test centroid
	x_true = (5.0+0-5.0)/3.0
	y_true = (0+5.0+0)/3.0
	c = triangle.calculate_centroid()
	x_test = c.x
	y_test = c.y
	assert x_test == pytest.approx(x_true,1e-12)
	assert y_test == pytest.approx(y_true,1e-12)
	# test orientation
	o_true = 'ccw'
	o_test = triangle.determine_orientation()
	assert o_test == o_true
	# test convexity
	c_true = True
	c_test = triangle.determine_convexity()
	assert c_test == c_true
	# test complexity
	c_true = False
	c_test = triangle.determine_complexity()
	assert c_test == c_true
	# tests for bounding box
	bbox = triangle.determine_bounding_box()
	p_true = 5.0*2.0+10.0*2.0
	p_test = bbox.calculate_perimeter()
	assert p_test == pytest.approx(p_true,1e-12)
	A_true = 5.0*10.0
	A_test = bbox.calculate_area()
	assert A_test == pytest.approx(A_true,1e-12)
	x_true = (5.0-5.0)/2.0
	y_true = (0+5.0)/2.0
	c = bbox.calculate_centroid()
	x_test = c.x
	y_test = c.y
	assert x_test == pytest.approx(x_true,1e-12)
	assert y_test == pytest.approx(y_true,1e-12)
	o_true = 'ccw'
	o_test = bbox.determine_orientation()
	assert o_test == o_true
	c_true = True
	c_test = bbox.determine_convexity()
	assert c_test == c_true
	c_true = False
	c_test = bbox.determine_complexity()
	assert c_test == c_true
	# test points in polygon
	point = Point2D(0,1)
	assert triangle.determine_point_location(point) == 1
	point = Point2D(-4,0.5)
	assert triangle.determine_point_location(point) == 1
	point = Point2D(4,1)
	assert triangle.determine_point_location(point) == 2
	point = Point2D(0,0)
	assert triangle.determine_point_location(point) == 2
	point = Point2D(-1,-1)
	assert triangle.determine_point_location(point) == 0