I have an object that represents a rectangle in python that looks like this:
class cords:
x_start = -1
x_end = -1
y_start = -1
y_end = -1
def __init__(self, x_start, x_end, y_start, y_end):
self.x_start = x_start
self.x_end = x_end
self.y_start = y_start
self.y_end = y_end
Now I have a list of cords named new_cords with values like this:
x_start: 508, x_end: 530, y_start: 843, y_end: 869
x_start: 508, x_end: 530, y_start: 843, y_end: 870
x_start: 401, x_end: 451, y_start: 582, y_end: 620
x_start: 467, x_end: 513, y_start: 583, y_end: 621
x_start: 466, x_end: 512, y_start: 634, y_end: 672
x_start: 533, x_end: 585, y_start: 534, y_end: 561
x_start: 528, x_end: 576, y_start: 583, y_end: 622
I want to remove from new_cords rectangles that are at distance of 3 away from each other to reduce similar rectangles.
I wrote this:
for c_out in new_cords:
for c_in in new_cords:
if abs(c_out.x_start - c_in.x_start) < 3 or abs(c_out.x_end - c_in.x_end) < 3 or abs(c_out.y_start - c_in.y_start) < 3 or abs(c_out.y_end - c_in.y_end) < 3:
new_cords.remove(c_out)
But I get exceptions because it deletes values that don't exist and I don't get a correct list.
What is wrong with my algorithm?
EDIT: the algorithm should only delete one of them. E.g, A and B are close, so we should delete A(or B), but not both of them