I’m facing a problem of algorithm. Here’s the thing : I’ve got an image of a ball, it was done by analysing a array. It’s pretty much something like this :
....####......
.##########....
...############.....
.##########....
....####......
How do i found the center of the ball (approximativly) with an algorithm ? And displaying something like this :
....####......
.##########....
...#####0######.....
.##########....
....####......
I was thinking of using something like the width of the longer line of # and the height.
for the height :
k = 0
for i in range (0, 10) :
for j in range (0, 20) :
# if one line contain a # then k = k+1
center = (k/2)
but i don’t know from there ..
Computing the ball’s centre of mass should do the trick. Basically, it’s the average of the coordinates of all pixels that are part of the ball. This neatly decomposes so you can compute the average for x and y separately. Something along these lines:
(I’m using
xandyhere because their meaning is clearer thaniandj. Adjust to taste.)