Skip to content

Commit

Permalink
move blob object to new file
Browse files Browse the repository at this point in the history
  • Loading branch information
saadati944 committed Jan 12, 2021
1 parent dd0345a commit 401e5ef
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 58 deletions.
61 changes: 3 additions & 58 deletions beadfinder.py
Original file line number Diff line number Diff line change
@@ -1,63 +1,7 @@
import sys
import math
from stdlibrary import picture, color


class Blob:
'''
The blob object contains the coordinates of connected pixels.
'''

def __init__(self):
'''
create new instance of blob object
'''
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])

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
'''
xs = 0
ys = 0
for p in self.pixels:
xs += p[0]
ys += p[1]
return xs/len(self.pixels), ys/len(self.pixels)

def __str__(self):
return f"{str(len(self.pixels))} {self.center()}"


class BeadFinder:
def __init__(self, pic, tau):
'''
Expand Down Expand Up @@ -190,5 +134,6 @@ def main():
for blb in bf.getBeads(min_pixels):
print(blb)

if __name__=="__main__":
main()

if __name__ == "__main__":
main()
56 changes: 56 additions & 0 deletions blob.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import math


class Blob:
'''
The blob object contains the coordinates of connected pixels.
'''

def __init__(self):
'''
create new instance of blob object
'''
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])

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
'''
xs = 0
ys = 0
for p in self.pixels:
xs += p[0]
ys += p[1]
return xs/len(self.pixels), ys/len(self.pixels)

def __str__(self):
return f"{str(len(self.pixels))} {self.center()}"

0 comments on commit 401e5ef

Please sign in to comment.