Created
February 18, 2016 19:00
-
-
Save astrolitterbox/ff32744a47f058d58780 to your computer and use it in GitHub Desktop.
Random locations for lightbulbs. More at http://blog.technarium.lt
This file contains hidden or 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
from __future__ import division | |
import math | |
import matplotlib.pyplot as plt | |
import numpy as np | |
width = 200 | |
length = 300 | |
n_lines = 6 | |
min_dist = 10 | |
min_separation = 50 | |
min_depth = 180 | |
max_depth = 280 | |
min_depth_separation = 30 | |
# Drawing lines | |
line_separation = length/(n_lines - 1) | |
line_locations = [] | |
length_remaining = length | |
while (length_remaining) >= 0: | |
line_locations.append(length_remaining) | |
length_remaining = length_remaining - line_separation | |
#drawing lightbulbs | |
n_bulbs_on_line = [3, 3, 3, 3, 2, 3] | |
available_width = width - 2*min_dist | |
def populate_lightbulbs(n): | |
pos = [] | |
for i in range(1, n+1): | |
pos.append(min_dist + np.random.random_integers(0, available_width)) | |
if check_distances(pos, min_separation) == True: | |
#if (np.abs(np.diff(pos)) < 500).any(): | |
print "retrying" | |
pos = populate_lightbulbs(n) | |
print "Result: ", pos | |
return pos | |
def populate_depths(n): | |
pos = [] | |
for i in range(1, n+1): | |
pos.append(np.random.random_integers(min_depth, max_depth)) | |
if check_distances(pos, min_depth_separation) == True: | |
print "retrying" | |
pos = populate_depths(n) | |
print "Result: ", pos | |
return pos | |
def check_distances(arr, min_val): | |
arr = np.asarray(arr) | |
print arr, 'arr' | |
diff = np.zeros(arr.shape) | |
for i, a in enumerate(arr): | |
#diff.append(np.abs(arr[arr != a] - a)) | |
diff[i] = np.abs(a - arr[i-1]) | |
print arr, 'arr', diff, 'diff' | |
print (diff < min_val).any() | |
return (diff < min_val).any() | |
fig = plt.figure(figsize = [40, 30]) | |
fig, axes = plt.subplots(1, 3) | |
for row in axes: | |
print "Drawing subplot" | |
for i, d in enumerate(line_locations): | |
n = n_bulbs_on_line[i] | |
print "Drawing random location.. at line ",i, | |
pos = populate_lightbulbs(n) | |
depths = populate_depths(n) | |
labels = ['{0}'.format(i) for i in pos] | |
print np.subtract(width, pos), width, pos | |
right_labels = ['{0}'.format(i) for i in np.subtract(width, pos)] | |
depth_labels = ['{0}'.format(i) for i in depths] | |
for i, point in enumerate(pos): | |
row.scatter(point, d, c='k', s=10) | |
row.annotate( | |
labels[i], fontsize=6, | |
xy = (point, d), xytext = (-10, 5), | |
textcoords = 'offset points', ha = 'right', va = 'bottom', | |
bbox = None, | |
arrowprops = dict(arrowstyle = '->', connectionstyle = 'arc3,rad=0')) | |
#right labels | |
row.annotate( | |
right_labels[i], fontsize=6, | |
xy = (point, d), xytext = (-10, 0), | |
textcoords = 'offset points', ha = 'right', va = 'bottom', | |
bbox = None, | |
arrowprops = dict(arrowstyle = '->', connectionstyle = 'arc3,rad=0')) | |
#line depths | |
row.annotate(depth_labels[i], fontsize=6, xy = (point, d), xytext = (6, depths[i]/10), textcoords = 'offset points', ha = 'right', va = 'bottom', bbox = None, arrowprops = dict(arrowstyle = '-', connectionstyle = 'arc3,rad=0')) | |
row.axhline(d, c='k') | |
row.xaxis.set_visible(False) | |
row.yaxis.set_visible(False) | |
row.axis([0, 200, 0, 300]) | |
plt.subplots_adjust() | |
#plt.tight_layout() | |
plt.savefig("output.pdf") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment