Created
October 24, 2019 21:26
-
-
Save dustinandrews/e592ef803455fadfdc5996fd09fc824a to your computer and use it in GitHub Desktop.
Binary Space Partitioning didactic example in Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
""" | |
Pure Python 3.6 example of doing binary space partioning for rectangles | |
Note the example is meant to be illustrative rather than performant, idiomatic or compact | |
Copyright 2019 Dustin Andrews | |
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
""" | |
import random | |
from collections import namedtuple | |
Rect = namedtuple('Rect', ['top','left', 'h', 'w']) | |
height = 20 | |
width = 10 | |
my_map = {} | |
for h in range(height): | |
for w in range(width): | |
my_map[f'{w},{h}'] = "00 " | |
def print_map(_map: dict): | |
for h in range(height): | |
for w in range(width): | |
print(_map[f'{w},{h}'], end="") | |
print() | |
print() | |
def partition(rect :Rect, _map :dict, label: str): | |
# Set all cells in rect to label | |
return_map = _map.copy() | |
for x in range(rect.left, rect.w + rect.left): | |
for y in range(rect.top, rect.h + rect.top): | |
return_map[f'{x},{y}'] = f'{label}' | |
return return_map | |
def select_partion(rect: Rect): | |
# randomly slice the rect on the short axis | |
# Small rect will be at least 1/4th the long axis | |
# Return two smaller Rects | |
top = rect.top | |
left = rect.left | |
h = rect.h | |
w = rect.w | |
top2 = top | |
left2 = left | |
h2 = h | |
w2 = w | |
if(h > w): | |
# vertical | |
onequarter = max(2,rect.h // 4) | |
newsize = random.randint(onequarter, rect.h - onequarter) | |
top2 = top + newsize | |
h2 = h - newsize | |
h = h - h2 | |
else: | |
#horizontal | |
onequarter = max(2,rect.w // 4) | |
newsize = random.randint(onequarter, rect.w - onequarter) | |
left2 = left + newsize | |
w2 = w - newsize | |
w = w - w2 | |
rect_one = Rect(top, left, h, w) | |
rect_two = Rect(top2, left2, h2, w2) | |
return rect_one, rect_two | |
def recursive_partition(rect: Rect): | |
# Partition a rectangle into a number of smaller rectangles | |
# Returns a tree of rectangles. | |
if rect.h < 5 and rect.w < 5: | |
return [] | |
else: | |
r1, r2 = select_partion(rect) | |
# use this line to return a tree struture | |
children = [r1, r2, recursive_partition(r1) + recursive_partition(r2)] | |
return children | |
def read_tree(tree : list): | |
# Flatten branch tree to list | |
branches=[] | |
if type(tree) == type([]): | |
for t in tree: | |
branches = read_tree(t) + branches | |
else: | |
return [tree] | |
return branches | |
rect = Rect(0,0, height, width) | |
children = recursive_partition(rect) | |
branches = read_tree(children) | |
index = 1 | |
nmap = my_map.copy() | |
# Show each partition in turn. Starting from the smallest | |
# and continuing until the largest 2 | |
for b in branches: | |
nmap = partition(b, nmap, f'{index:0{2}} ') | |
print(index) | |
index += 1 | |
print_map(nmap) | |
print('q to stop, enter to continue') | |
inp = input() | |
if inp == 'q': | |
break |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment