Skip to content

Instantly share code, notes, and snippets.

@infinitewarp
Created April 4, 2016 21:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save infinitewarp/7b408b84462e5edf299ef574f438d9b3 to your computer and use it in GitHub Desktop.
Save infinitewarp/7b408b84462e5edf299ef574f438d9b3 to your computer and use it in GitHub Desktop.
quick visualization of coordinates in a conical frustum
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import print_function
import argparse
def get_args():
parser = argparse.ArgumentParser()
parser.add_argument('depth', type=int)
parser.add_argument('diameter1', type=int)
parser.add_argument('diameter2', type=int)
args = parser.parse_args()
return args
def calculate_conical_frustum(depth, diameter1, diameter2):
max_diameter = max(diameter1, diameter2)
center_x = 0.5 * max_diameter
center_y = 0.5 * max_diameter
coords = []
for z in range(0, depth):
layer = []
coords.append(layer)
ratio = 1.0 * z / (depth - 1)
layer_radius = ((diameter2 - diameter1) * ratio + diameter1) * 0.5
for x in range(0, max_diameter):
row = []
layer.append(row)
for y in range(0, max_diameter):
# we're adding 0.5 so we are check if the *center* of the voxel
# is inside the defined circle.
radius = abs(layer_radius)
padded_x = x + 0.5
padded_y = y + 0.5
row.append(in_circle(center_x, center_y, radius, padded_x, padded_y, hollow=True))
return coords
def in_circle(center_x, center_y, radius, x, y, hollow=False):
square_dist = (center_x - x) ** 2 + (center_y - y) ** 2
is_in = square_dist <= (radius) ** 2
if is_in and hollow:
inner_radius = max(0, radius - 1)
return not in_circle(center_x, center_y, inner_radius, x, y)
return is_in
def print_shape(coords):
layer_counter = 0
for layer in coords:
layer_counter += 1
print ('layer {}:'.format(layer_counter))
for x in layer:
for y in x:
print('X' if y else u'\u00B7', end="")
print()
print()
print()
if __name__ == '__main__':
args = get_args()
coords = calculate_conical_frustum(args.depth, args.diameter1, args.diameter2)
print_shape(coords)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment