-
Notifications
You must be signed in to change notification settings - Fork 0
/
image_model.py
58 lines (45 loc) · 1.53 KB
/
image_model.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
import random
# mutations:
# reorder shapes
# adjust shape orientation, colour, dimensions
# add/remove shapes
class ImageModel():
# size is (width, height) in pixels
def __init__( self, size ):
self.size_ = size
self.shapes_ = []
def size( self ):
return self.size_
# shape:
# - points: array of (x,y)
# - colour: (r,g,b,a)
def shapes( self ):
return self.shapes_
def set_shapes( self, shapes ):
self.shapes_ = shapes
def randomise( self ):
self.shapes_ = []
ns = self.num_shapes()
for i in range( ns ):
s = self.random_shape()
self.shapes_.append( s )
def random_shape( self ):
ran255 = lambda: random.randint(0, 255)
alpha = min( 255, (ran255()))
return {
"points": [self.random_point() for i in range(3)],
"colour": ( ran255(), ran255(), ran255(), alpha )
}
def random_point( self ):
return (
random.randint( 0, self.size_[0] - 1 ),
random.randint( 0, self.size_[1] - 1 ) )
def num_shapes( self ):
# best-guess values; could adjust this to/towards population mean,
# with some randomness, to improve resolution. Evolving the GA
# simulated annealing may be more appropriate - this is comparable
# to SA's 'temperature' that decreases randomness in exploration
# of solution space.
mean = 100
stdDev = 15
return abs( int( random.gauss( mean, stdDev )))