forked from filfos/random-environment-generator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generator.py
144 lines (101 loc) · 2.87 KB
/
generator.py
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
from Tkinter import *
import random as r
import Image, ImageDraw
class Point:
def __init__(self, x, y):
self.x = x;
self.y = y;
self.visible = 1;
def insideCircle(self, centerPoint,radius):
squareDist = (centerPoint.x-self.x)**2 + (centerPoint.y-self.y)**2
return squareDist < radius**2
def removePoint(self, canvas):
self.visible = 0;
canvas.create_oval(self.x, self.y, self.x, self.y, outline='white')
class Vector:
def __init__(self, x, y):
self.x = x;
self.y = y;
def getVector(p0, p1):
return Vector(p1.x-p0.x, p1.y-p0.y)
def averageVector(vectors):
x = 0
y = 0
i = 0
for v in vectors:
x += v.x
y += v.y
i +=1
return Vector( round(x/i), round(y/i) )
def drawLine(point, vector, canvas, lineWidth):
w.create_line(point.x, point.y, point.x + vector.x, point.y + vector.y, width=lineWidth)
#Draw the same line in memory
draw.line((point.x, point.y, point.x + vector.x, point.y + vector.y), fill=(0,0,0), width=lineWidth)
def findNextSeed(seed, jumps, canvas, draw, lineWidth):
vectors = []
for p in points:
if (p.visible == 0):
continue
if( p.insideCircle(seed, searchRadius) ):
p.removePoint(canvas)
vectors.append( getVector(seed, p) )
if (len(vectors) != 0 and jumps != 0):
v = averageVector(vectors)
seed.removePoint(canvas)
drawLine(seed, v, canvas, lineWidth)
findNextSeed(Point(seed.x + v.x, seed.y + v.y), jumps-1, canvas, draw, lineWidth)
#### PARAMETERS ####
canvasWidth = 1000
canvasHeight = 1000
noofPoints = 1500
noofSeeds = 40
searchRadius = 200
maxSeedJumps = 10
points = []
seeds = []
master = Tk()
###################
###### START ######
###################
w = Canvas(master, width=canvasWidth, height=canvasHeight, bg='white')
w.pack()
#create an empty image in memory
white = (255, 255, 255)
img = Image.new("RGB", (canvasWidth, canvasHeight), white)
draw = ImageDraw.Draw(img)
#generate points
for i in range(noofPoints):
p = Point(r.randrange(0, canvasWidth), r.randrange(0, canvasHeight))
w.create_oval(p.x, p.y, p.x, p.y)
points.append(p)
#generate seeds
for i in range(noofSeeds):
p = Point(r.randrange(0, canvasWidth), r.randrange(0, canvasHeight))
w.create_oval(p.x, p.y, p.x, p.y, outline='red')
seeds.append(p)
# Find points within maxRadius of each seedpoint
# Average the vector between seed and points to create a new seed.
# Connect the seeds with a line
for s in seeds:
jumps = r.randrange(1, maxSeedJumps+1)
findNextSeed(s, jumps, w, draw, 5)
#delete all remaining points
for p in points:
if (p.visible == 1):
p.removePoint(w)
for s in seeds:
if (s.visible == 1):
s.removePoint(w)
#save image to working directory
filename = "env.png"
img.save(filename)
#draw black borders
#top
#w.create_line(0, 0, 1500, 0, width=10)
#left
#w.create_line(0, 0, 0, 900, width=10)
#right
#w.create_line(1500, 0, 1500, 900, width=10)
#bottom
#w.create_line(0, 900, 1500, 900, width=10)
mainloop()