forked from rougier/from-python-to-numpy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDART_sampling_python.py
More file actions
46 lines (40 loc) · 1.24 KB
/
DART_sampling_python.py
File metadata and controls
46 lines (40 loc) · 1.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# -----------------------------------------------------------------------------
# From Numpy to Python
# Copyright (2017) Nicolas P. Rougier - BSD license
# More information at https://github.com/rougier/numpy-book
# -----------------------------------------------------------------------------
import math
import random
import matplotlib.pyplot as plt
def DART_sampling_python(width=1.0, height=1.0, radius=0.025, k=100):
def squared_distance(p0, p1):
dx, dy = p0[0]-p1[0], p0[1]-p1[1]
return dx*dx+dy*dy
points = []
i = 0
last_success = 0
while True:
x = random.uniform(0, width)
y = random.uniform(0, height)
accept = True
for p in points:
if squared_distance(p, (x, y)) < radius*radius:
accept = False
break
if accept is True:
points.append((x, y))
if i-last_success > k:
break
last_success = i
i += 1
return points
if __name__ == '__main__':
plt.figure()
plt.subplot(1, 1, 1, aspect=1)
points = DART_sampling_python()
X = [x for (x, y) in points]
Y = [y for (x, y) in points]
plt.scatter(X, Y, s=10)
plt.xlim(0, 1)
plt.ylim(0, 1)
plt.show()