def test_base(self): points = [(0,0), (0,5),(1,8),(3,9),(5,8), (7,5), (6,0),(0,0)] motions = self.__class__.to_translation(list(Point.new(xy) for xy in points)) # -- when -- arranger = CurveArranger() arranged_motions = arranger.arrange(motions) assert (any(isinstance(t, Rotation) for t in arranged_motions)) on_the_spot_positions = get_on_the_spot_indices(arranged_motions) simple_rotation_positions = get_simple_rotations_indices(arranged_motions) assert on_the_spot_positions == [4, 9] assert simple_rotation_positions == [2,7]
def test_find_find_angle_and_chord_vector(self): angle, u, distance = Arc.find_angle_and_chord_vector( Point(-1, 0), Point(1, 0), Point(0, 1)) assert angle == math.pi
def _get_length(self): return Point.distance(self.start, self.end)
def test_intersection_of_parallel_lines(): line_a = Line(point=Point(0, 1), vector=Point(0, 1)) line_b = Line(point=Point(1, 1), vector=Point(0, 1)) with pytest.raises(ValueError): line_a.intersection(line_b)
def test_intersection(): line_a = Line(point=Point(0, 0), vector=Point(1, 0)) line_b = Line(point=Point(0, 0), vector=Point(0, 1)) assert line_a.intersection(line_b) == Point(0, 0)
def test_move_creation(self): m = Translation(Point.new((0, 0)), Point.new((1, 1))) assert m.start == Point.new((0, 0)) assert m.end == Point.new((1, 1)) assert m.length == math.sqrt(2) assert m.vector == Point(math.sqrt(1 / 2), math.sqrt(1 / 2))
def test_rotate_from_axe_with_null(): # when vector = Point(0, 0) reference = NORTH_WEST assert Geometry.transpose_rotation_relative_to(vector, reference) == vector
import pytest from ex02.geometry import Point, Geometry NORTH = Point(0, 1) SOUTH = Point(0, -1) WEST = Point(-1, 0) EAST = Point(1, 0) NORTH_EAST = (NORTH + EAST).normalize() NORTH_WEST = (NORTH + WEST).normalize() SOUTH_EAST = (SOUTH + EAST).normalize() SOUTH_WEST = (SOUTH + WEST).normalize() @pytest.mark.parametrize("vector, reference, expected", [ (NORTH, EAST, NORTH), (NORTH, NORTH, EAST), (WEST, NORTH, NORTH), (EAST, NORTH, SOUTH), (SOUTH, NORTH, WEST), (NORTH, SOUTH, WEST), (WEST, SOUTH, SOUTH), (EAST, SOUTH, NORTH), (SOUTH, SOUTH, EAST), (NORTH * 2, EAST, NORTH * 2), (NORTH * -5, NORTH, EAST * -5), (NORTH, NORTH_EAST, NORTH_EAST), (NORTH, SOUTH_WEST, SOUTH_WEST), ]) def test_rotate_from_axe(vector, reference, expected): assert Geometry.transpose_rotation_relative_to(vector, reference) == expected
def rotate_from_center(c, p, phi): radius = Point.distance(c, p) x = radius * math.cos(phi) y = radius * math.sin(phi) rotated = Point(x, y) + c return rotated
def test_equal_float_int(self): a = Point(4, 1) b = Point(4.0, 1.0) assert a == b
def test_equal(self): a = Point(0.5, 0.5) b = Point(0.5 * (1 + 1e-10), 0.5) assert a == b
class TestGeometry: @pytest.mark.parametrize("vector, reference, expected", [ (NORTH, EAST, NORTH), (NORTH, NORTH, EAST), (WEST, NORTH, NORTH), (EAST, NORTH, SOUTH), (SOUTH, NORTH, WEST), (NORTH, SOUTH, WEST), (WEST, SOUTH, SOUTH), (EAST, SOUTH, NORTH), (SOUTH, SOUTH, EAST), (NORTH * 2, EAST, NORTH * 2), (NORTH * -5, NORTH, EAST * -5), (NORTH, NORTH_EAST, NORTH_EAST), (NORTH, SOUTH_WEST, SOUTH_WEST), ]) def test_rotate_from_axe(self, vector, reference, expected): assert Geometry.transpose_rotation_relative_to(vector, reference) == expected # Reverse assert Geometry.transpose_rotation_relative_to( expected, Point(reference.x, -reference.y)) == vector def test_rotate_from_axe_with_null(self): # when vector = Point(0, 0) reference = NORTH_WEST assert Geometry.transpose_rotation_relative_to(vector, reference) == vector def test_rotate_from_axe_with_reference_null(self): # when vector = NORTH reference = Point(0, 0) with pytest.raises(ZeroDivisionError): Geometry.transpose_rotation_relative_to(vector, reference) @pytest.mark.parametrize("vector, axe, expected", [ (NORTH, EAST, SOUTH), (NORTH, NORTH, NORTH), (NORTH, NORTH_EAST, EAST), (NORTH, SOUTH_WEST, EAST), (NORTH_EAST, NORTH, NORTH_WEST), (NORTH_EAST * 2.3, NORTH, NORTH_WEST * 2.3), ]) def test_geometry_get_symmetrical(self, vector, axe, expected): assert expected == Geometry.get_symmetrical(vector, axe) @pytest.mark.parametrize("params", [ { "line0": Line(point=Point(0, 1), vector=Point(0, 1)), "line1": Line(point=Point(3, 5), vector=Point(1, 0)), "intersection": Point(0, 2) }, { "line0": Line(point=Point(0, 1), vector=Point(0, 1)), "line1": Line(point=Point(5, 3), vector=Point(1, 0)), "intersection": Point(0, -2) }, ]) def test_get_tangent_point_from_lines(self, params): line0 = params['line0'] line1 = params['line1'] intersection = params['intersection'] assert Geometry.get_tangent_point_from_lines(line0, line1) == intersection @pytest.mark.parametrize("params", [{ 'point': Point(1, 0), 'vector': Point(1, 0), 'candidate': Point(3, 0), 'expected': True }, { 'point': Point(1, 0), 'vector': Point(1, 0), 'candidate': Point(-3, 0), 'expected': False }, { 'point': Point(1, 0), 'vector': Point(1, 0), 'candidate': Point(1, 1), 'expected': False }]) def test_is_beyond(self, params): point = params['point'] vector = params['vector'] candidate = params['candidate'] expected = params['expected'] assert Geometry.is_beyond_point(candidate, point, vector) == expected
def test_compute_center_from_both_tangents_impossible(self): with pytest.raises(AssertionError) as ae: Arc.compute_center_from_both_tangents(p0=Point(-5, 2), p1=Point(1, 0), tangent_p0=Point(0, 1), tangent_p1=Point(1, -1))
def test_add(self): a = Point(0.5, 0.5) b = Point(-1, 1) c = a + b assert c.x == -0.5 assert c.y == 1.5
def test_simple_arc(self): candidate = Arc(Point(-1, 0), Point(1, 0), Point(0, 1)) assert candidate.radius == 1 assert candidate.angle == -math.pi assert candidate.center == Point(0., 0.)
def test_circular_arc(self): arc = Arc(start=Point(1, 0), end=Point(0, 1), start_tangent=Point(0, 1)) assert math.isclose(arc.angle, (math.pi / 2.0))
def convert(positions): return list(Point.new(xy) for xy in positions)
def test_equal_10e17(self): a = Point(0.0, 6.123233995736766e-17) b = Point(0., 0.) assert a == b
def test_rotate_from_axe(vector, reference, expected): assert Geometry.transpose_rotation_relative_to(vector, reference) == expected # Reverse assert Geometry.transpose_rotation_relative_to(expected, Point(reference.x, - reference.y)) == vector
def test_equal_tuple(self): a = Point(1.0, 4.0) assert a == (1, 4)
def test_rotate_from_axe_with_reference_null(): # when vector = NORTH reference = Point(0, 0) with pytest.raises(ZeroDivisionError): Geometry.transpose_rotation_relative_to(vector, reference)
def test_are_orthogonal(self): a = Point(0.5, 0.5) b = Point(-1, 1) assert a.is_orthogonal(b)
def to_points(self, positions): return list([Point.new(xy) for xy in positions])
def test_are_not_orthogonal(self): a = Point(1, 0.7) b = Point(0.5, 1) assert not a.is_orthogonal(b)
def test_intersection_2(): line_a = Line(point=Point(3, 1), vector=Point(1, 1)) line_b = Line(point=Point(0, 2), vector=Point(1, 0)) intersection = line_a.intersection(line_b) expected = Point(4, 2) assert intersection == expected, f'{intersection} != {expected}'
def test_are_collinear(self): a = Point(0.5, 0.5) b = Point(1, 1) assert a.is_collinear(b)
def test_coordinate_belongs_to_Line(): A = Point(1, 2) line = Line(point=A, vector=Point(1, 1)) assert line.contains(Point(2, 3))
def test_distance_a_b(self): assert math.isclose(Point.distance(Point(1, 0), Point(0, 1)), math.sqrt(2))
def test_line_with_zero_vector(self): with pytest.raises(ZeroDivisionError): A = Point(1, 2) line = Line(point=A, vector=Point(0, 0))
def test_find_angle_from_2_vectors(self, a, b, angle): def f(u, v): return math.acos(u.scalar_product(v)) assert f(Point(a[0], a[1]), Point(b[0], b[1])) == angle