-
Notifications
You must be signed in to change notification settings - Fork 0
/
blob.py
80 lines (64 loc) · 1.82 KB
/
blob.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
import math
class Blob:
'''
The blob object contains the coordinates of connected pixels.
'''
def __init__(self):
'''
create new instance of blob object
'''
self.xs = 0
self.ys = 0
self.pixels = []
def check_new_pixel(self, x, y):
'''
Check if the [x, y] is in the list of pixels
'''
return [x, y] in self.pixels
def add(self, x, y):
'''
add one pixle to pixels list
'''
if not self.check_new_pixel(x, y):
self.pixels.append([x, y])
self.xs += x
self.ys += y
def mass(self):
'''
return mass of the Blob
'''
return len(self.pixels)
def distanceTo(self, c):
'''
calculate distance to another Blob
formula :
d = radical( (x2-x1)^2 + (y2-y1)^2)
'''
center1 = self.center()
center2 = c.center()
return math.sqrt((center2[0]-center1[0])**2 + (center2[1] - center1[1])**2)
def center(self):
'''
return center of the Blob
'''
return self.xs/len(self.pixels), self.ys/len(self.pixels)
def __str__(self):
center = self.center()
return "{mass} ({x:.4f}, {y:.4f})".format(mass=self.mass(), x=center[0], y=center[1])
def main():
'''
test blob object
'''
import random
b = Blob()
for i in range(random.randrange(100, 200)):
b.add(random.random()*20, random.random()*20)
c = Blob()
for i in range(random.randrange(100, 200)):
c.add(random.random()*20, random.random()*20)
print('first blob :', b)
print('second blob :', c)
print('distance from b to c :', b.distanceTo(c))
print('distance from b to c :', c.distanceTo(b))
if __name__ == "__main__":
main()