def shape_intersects(distance): tx, ty = coordinates(0, 0, distance, angle) t = Transform() t.translate(tx, ty) translated_shape = t.map(shape) if use_bounding_box: b = Path() b.cornerRect(translated_shape.bounds) else: b = translated_shape # If the shape intersects it is too close (the distance is too low). if bounding_path.intersects(b): return -1 return 1
def angle_pack(shapes, seed, limit, maximum_radius, angle_tries=1, use_bounding_box=False): if shapes is None: return None _seed(seed) def center_and_translate(shape, tx=0, ty=0): bx, by, bw, bh = list(shape.bounds) t = Transform() t.translate(-bw / 2 - bx, -bh / 2 - by) return t.map(shape) geo = Geometry() bounding_path = Path() # Center first shape first_shape = center_and_translate(shapes[0]) geo.add(first_shape) bounding_path.cornerRect(first_shape.bounds) for shape in shapes[1:]: centered_shape = center_and_translate(shape) angles = [] for i in range(angle_tries): a = uniform(0, 360) if use_bounding_box: d = try_angle(bounding_path, centered_shape, a, limit, maximum_radius, use_bounding_box) else: d = try_angle(geo, centered_shape, a, limit, maximum_radius, use_bounding_box) angles.append([d, a]) chosen_distance, chosen_angle = sorted(angles)[0] tx, ty = coordinates(0, 0, chosen_distance, chosen_angle) t = Transform() t.translate(tx, ty) translated_shape = t.map(centered_shape) bounding_path.cornerRect(translated_shape.bounds) geo.add(translated_shape) return geo