boxespy/boxes/generators/jigsaw.py

100 lines
3.1 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
# Copyright (C) 2013-2016 Florian Festi
#
# This program 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.
#
# This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
from boxes import *
import random
2016-08-17 15:07:41 +02:00
class JigsawPuzzle(Boxes): # change class name here and below
"""Fractal jigsaw puzzle. Still aplha"""
2016-08-17 15:07:41 +02:00
webinterface = False # Change to make visible in web interface
def __init__(self):
Boxes.__init__(self)
self.count = 0
self.argparser.add_argument(
2016-08-17 15:07:41 +02:00
"--size", action="store", type=float, default=100,
help="size of the puzzle in mm")
self.argparser.add_argument(
2016-08-17 15:07:41 +02:00
"--depth", action="store", type=int, default=5,
help="depth of the recursion/level of detail")
def peano(self, level):
if level == 0:
2016-08-17 15:07:41 +02:00
self.edge(self.size / self.depth)
return
2016-08-17 15:07:41 +02:00
self.peano(self, level - 1)
self.corner()
def edge(self, l):
self.count += 1
Boxes.edge(self, l)
2016-08-17 15:07:41 +02:00
# if (self.count % 2**5) == 0: #level == 3 and parity>0:
# self.corner(-360, 0.25*self.size/2**self.depth)
def hilbert(self, level, parity=1):
if level == 0:
return
# rotate and draw first subcurve with opposite parity to big curve
self.corner(parity * 90)
self.hilbert(level - 1, -parity)
2016-08-17 15:07:41 +02:00
# interface to and draw second subcurve with same parity as big curve
2016-08-17 15:07:41 +02:00
self.edge(self.size / 2 ** self.depth)
self.corner(parity * -90)
self.hilbert(level - 1, parity)
2016-08-17 15:07:41 +02:00
# third subcurve
2016-08-17 15:07:41 +02:00
self.edge(self.size / 2 ** self.depth)
self.hilbert(level - 1, parity)
2016-08-17 15:07:41 +02:00
# if level == 3: self.corner(-360, 0.4*self.size/2**self.depth)
# fourth subcurve
self.corner(parity * -90)
2016-08-17 15:07:41 +02:00
self.edge(self.size / 2 ** self.depth)
self.hilbert(level - 1, -parity)
# a final turn is needed to make the turtle
# end up facing outward from the large square
self.corner(parity * 90)
2016-08-17 15:07:41 +02:00
# if level == 3 and parity>0: # and random.random() < 100*0.5**(self.depth-2):
# self.corner(-360, 0.4*self.size/2**self.depth)
2016-08-17 15:07:41 +02:00
# self.ctx.save()
# self.corner(parity*-90)
# self.edge(self.size/2**self.depth)
# self.ctx.restore()
def render(self):
size = self.size
t = self.thickness
# Initialize canvas
self.open()
self.burn = 0.0
self.moveTo(10, 10)
self.hilbert(self.depth)
self.close()
2016-08-17 15:07:41 +02:00
def main():
b = JigsawPuzzle()
b.parseArgs()
b.render()
2016-08-17 15:07:41 +02:00
if __name__ == '__main__':
main()