Skip to content

Instantly share code, notes, and snippets.

@brunsgaard
Created January 20, 2015 07:20
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 brunsgaard/16e153e95771bc7d41c6 to your computer and use it in GitHub Desktop.
Save brunsgaard/16e153e95771bc7d41c6 to your computer and use it in GitHub Desktop.
from __future__ import division
from itertools import product, combinations
from random import Random, shuffle, uniform, random
import math
d = lambda (x1,y1), (x2,y2): math.sqrt((x2-x1)** 2 + (y2-y1) ** 2)
graynode = r'\filldraw[fill={}, draw=black] ({},{}) circle [radius=1.50mm];'
#grid = [(uniform(0, 10), uniform(0, 5)) for _ in range(100)]
cpoint = [(3,2.5, 'v'), (7,2.5, 'u')]
blackpoints = [(4,4.7), (3,0.3), (8.2,4.5)]
whitepoints = []
graypoints = []
points = lambda: [(x,y) for (x,y,_) in cpoint] + blackpoints + whitepoints + graypoints
while len(points()) < 75:
x_, y_ = round(uniform(0, 10), 2), round(uniform(0, 5),2)
dists = [d((x, y), (x_,y_)) for x, y in points()]
if min(dists) > 0.6:
if random() < .25 and min(d((x, y), (x_,y_)) for x, y, _ in cpoint) > 0.7:
graypoints.append((x_, y_))
else:
whitepoints.append((x_, y_))
for x, y in sorted(whitepoints):
print(graynode.format('white', x,y))
for x, y in sorted(graypoints):
print(graynode.format('gray!50', x,y))
for x, y in sorted(blackpoints):
print(graynode.format('black!80', x,y))
for x, y, n in cpoint:
print(graynode.format('white', x,y))
print(r'\node at ({},{}) {};'.format(x+0.3,y,'{' +'\small${}$'.format(n) + '}'))
pmin, (x_, y_) = sorted([(d((x,y),(x_,y_)), (x_,y_)) for (x_,y_) in graypoints])[0]
print(r'\draw({},{}) circle [radius={}cm];'.format(x,y, pmin))
print(r'\node at ({},{}) {};'.format(x_+0.6,y_,'{' +'\small$p_1({})$'.format(n) + '}'))
print(r'\draw [->] ({},{}) -- ({},{});'.format(x,y,x_,y_))
for x_, y_ in whitepoints:
if d((x,y),(x_,y_)) < pmin:
print(r'\draw [->] ({},{}) -- ({},{});'.format(x,y,x_,y_))
pmin, (x_, y_) = sorted([(d((x,y),(x_,y_)), (x_,y_)) for (x_,y_) in blackpoints])[0]
print(r'\draw({},{}) circle [radius={}cm];'.format(x,y, pmin))
print(r'\node at ({},{}) {};'.format(x_+0.6,y_,'{' +'\small$p_2({})$'.format(n) + '}'))
for x_, y_ in graypoints:
if d((x,y),(x_,y_)) < pmin:
print(r'\draw [->] ({},{}) -- ({},{});'.format(x,y,x_,y_))
for x_, y_ in blackpoints:
print(r'\draw [->] ({},{}) -- ({},{});'.format(x,y,x_,y_))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment