def coords_to_lines(cls, coords, **kwargs): if not coords: return [] last_coord = coords[0] lines = [Line([coords[-1], coords[0]])] for coord in coords[1:]: lines.append(Line([last_coord, coord])) last_coord = coord return lines
def manufacture(coords, x=0, y=0, rotation=0): last_coord = coords[0] lines = [Line([coords[-1], coords[0]])] for coord in coords[1:]: lines.append(Line([last_coord, coord])) last_coord = coord polygon = Polygon(lines) polygon.set_position_rotation(x, y, rotation) polygon.freeze() return polygon
def find_lines_of_ear(cls, lines, clockwise): for line1, line2 in cls.iterate_overlapping_pairs(lines): if not line1.delta_radii_to(line2) > 0 == clockwise: continue line3 = Line([(line2.x2, line2.y2), (line1.x1, line1.y1)]) if any(map(lambda line: line1.on_left_side(line.x1, line.y1) == clockwise and line2.on_left_side(line.x1, line.y1) == clockwise and line3.on_left_side(line.x1, line.y1) == clockwise, filterfalse([line1, line2].__contains__, lines))): continue return line1, line2, line3 raise AttributeError("No ears found, are you sure this polygon is " + (clockwise and "clockwise?" or "counter-clockwise?"))
class TestLineRotation(object): def setup(self): self.target = Line([(-1, 0), (1, 0)]) def test_line_is_vertical(self): self.target.rotate(radians(90)) assert round(self.target.x1, 4) == round(self.target.x2, 4) def test_line_is_same_length(self): self.target.rotate(radians(90)) assert abs(round(self.target.dy, 4)) == 2 def test_line_radi_is_zero(self): assert self.target.radii == radians(90)
class TestLineIntersectionMutation(object): def setup(self): self.line1 = Line([(-2, 0), (-1, 0)]) self.line2 = Line([(0, -2), (0, -1)]) self.line1.intersection_point(self.line2) def test_finding_intersection_point_does_not_alter_left_hand_line(self): assert self.line1.x1 == -2 assert self.line1.x2 == -1 assert self.line1.y1 == 0 assert self.line1.y2 == 0 def test_finding_intersection_point_does_not_alter_right_hand_line(self): assert self.line2.x1 == 0 assert self.line2.x2 == 0 assert self.line2.y1 == -2 assert self.line2.y2 == -1
class TestLineIntersectionMutationOnHorizontalLine(object): def setup(self): self.line1 = Line([(-10, 0), (0, 0)]) self.line2 = Line([(10, 0), (0, 0)]) self.line1.intersection_point(self.line2) def test_finding_intersection_point_does_not_alter_left_hand_line(self): assert self.line1.x1 == -10 assert self.line1.x2 == 0 assert self.line1.y1 == 0 assert self.line1.y2 == 0 def test_finding_intersection_point_does_not_alter_right_hand_line(self): assert self.line2.x1 == 10 assert self.line2.x2 == 0 assert self.line2.y1 == 0 assert self.line2.y2 == 0
def build_polygon(self): if self._n_parts_alive <= 1: self._callback("broken") raise PartConnectionError("Not enough parts") lines = [] for part, next_part in self._pairwise(): lines.append(Line([(part.position.x, part.position.z), (next_part.position.x, next_part.position.z)])) polygon = Polygon(lines) #if self.validate_connection_function(polygon): return polygon
def setup(self): line1 = Line([(-2, 0), (-1, 0)]) line2 = Line([(0, -2), (0, -1)]) self.target = line1.intersection_point(line2)
def setup(self): self.target = Line([(-1, 0), (1, 0)])
def setup(self): line1 = Line([(1, 1), (-1, -1)]) line2 = Line([(-1, 1), (1, -1)]) self.target = line1.intersection_point(line2)
def setup(self): line1 = Line([(-2, -3), (1, 0)]) line2 = Line([(2, 3), (5, 0)]) self.target = line1.intersection_point(line2) self.target2 = line2.intersection_point(line1)
def setup(self): self.line1 = Line([(-10, 0), (0, 0)]) self.line2 = Line([(10, 0), (0, 0)]) self.line1.intersection_point(self.line2)
def setup(self): self.target = Line([(0, 0), (10, 10)])
class TestLine(object): def setup(self): self.target = Line([(0, 0), (10, 10)]) def test_set_position_rotation(self): self.target.set_position_rotation(10, 0, 0) assert self.target.x1 == 10 assert self.target.x2 == 20 assert self.target.y1 == 0 assert self.target.y2 == 10 def test_freeze(self): self.target.set_position_rotation(10, 0, 0) self.target.freeze() assert self.target.x1 == 10 assert self.target.x2 == 20 assert self.target.y1 == 0 assert self.target.y2 == 10 def test_moving_after_freeze(self): self.target.set_position_rotation(10, 0, 0) self.target.freeze() self.target.set_position_rotation(10, 0, 0) assert self.target.x1 == 20 assert self.target.x2 == 30 assert self.target.y1 == 0 assert self.target.y2 == 10
def __iadd__(self, other): self._lines += [ Line([(line.x1, line.y1), (line.x2, line.y2)]) for line in other.lines ] return self
def setup(self): self.line1 = Line([(-2, 0), (-1, 0)]) self.line2 = Line([(0, -2), (0, -1)]) self.line1.intersection_point(self.line2)
def point_inside(self, x, y): outside_point = (self.moving_polygon.left - 1, self.moving_polygon.bottom - 1) break_line = Line([(x, y), outside_point]) n_broken_lines = sum(1 for l in self.moving_polygon.lines if l.intersection_point(break_line)[0]) return n_broken_lines % 2 == 1