X-Git-Url: https://svn.cri.ensmp.fr/git/linpy.git/blobdiff_plain/51e97eade63b2f4c7b500feb503436cc4a886e59..d585b06ccf67b2837519f4b48c6800dcdb924d9d:/examples/menger.py diff --git a/examples/menger.py b/examples/menger.py index b6ab90d..8d258b1 100755 --- a/examples/menger.py +++ b/examples/menger.py @@ -1,21 +1,17 @@ #!/usr/bin/env python3 -""" - This file is part of Linpy. - - Linpy is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - Linpy is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with Linpy. If not, see . -""" +# Plot a Menger sponge. +# +# The construction of a Menger sponge can be described as follows: +# +# 1. Begin with a cube. +# 2. Divide every face of the cube into 9 squares, like a Rubik's Cube. This +# will sub-divide the cube into 27 smaller cubes. +# 3. Remove the smaller cube in the middle of each face, and remove the smaller +# cube in the very center of the larger cube, leaving 20 smaller cubes. This +# is a level-1 Menger sponge (resembling a Void Cube). +# 4. Repeat steps 2 and 3 for each of the remaining smaller cubes, and continue +# to iterate. import argparse @@ -24,20 +20,22 @@ import matplotlib.pyplot as plt from math import ceil from matplotlib import pylab -from mpl_toolkits.mplot3d import Axes3D -from pypol import * +from linpy import Le, Polyhedron, symbols + x, y, z = symbols('x y z') _x, _y, _z = x.asdummy(), y.asdummy(), z.asdummy() + def translate(domain, *, dx=0, dy=0, dz=0): domain &= Polyhedron([x - _x + dx, y - _y + dy, z - _z + dz]) domain = domain.project([x, y, z]) domain = domain.subs({_x: x, _y: y, _z: z}) return domain + def _menger(domain, size): result = domain result |= translate(domain, dx=0, dy=size, dz=0) @@ -61,6 +59,7 @@ def _menger(domain, size): result |= translate(domain, dx=2*size, dy=2*size, dz=2*size) return result + def menger(domain, count=1, cut=False): size = 1 for i in range(count): @@ -73,9 +72,11 @@ def menger(domain, count=1, cut=False): if __name__ == '__main__': parser = argparse.ArgumentParser( description='Compute a Menger sponge.') - parser.add_argument('-n', '--iterations', type=int, default=1, - help='number of iterations (default: 1)') - parser.add_argument('-c', '--cut', action='store_true', default=False, + parser.add_argument( + '-n', '--iterations', type=int, default=2, + help='number of iterations (default: 2)') + parser.add_argument( + '-c', '--cut', action='store_true', default=False, help='cut the sponge') args = parser.parse_args() cube = Le(0, x) & Le(x, 1) & Le(0, y) & Le(y, 1) & Le(0, z) & Le(z, 1) @@ -84,5 +85,3 @@ if __name__ == '__main__': plot = fig.add_subplot(1, 1, 1, projection='3d', aspect='equal') fractal.plot(plot) pylab.show() - -# Copyright 2014 MINES ParisTech