From c5110585c76f56c987bb596aa91498aa3f99413e Mon Sep 17 00:00:00 2001 From: guille Date: Thu, 23 Feb 2023 15:35:13 -0500 Subject: [PATCH] untested neighbours detection implemented. --- hub/helpers/geometry_helper.py | 62 ++++++++++++++++++++++------------ 1 file changed, 40 insertions(+), 22 deletions(-) diff --git a/hub/helpers/geometry_helper.py b/hub/helpers/geometry_helper.py index fdf3cbdb..912105d4 100644 --- a/hub/helpers/geometry_helper.py +++ b/hub/helpers/geometry_helper.py @@ -16,14 +16,35 @@ from hub.helpers.location import Location from PIL import Image + class MapPoint: def __init__(self, x, y): - self.x = int(x) - self.y = int(y) + self._x = int(x) + self._y = int(y) + + @property + def x(self): + return self._x + + @property + def y(self): + return self._y def __str__(self): return f'({self.x}, {self.y})' + def __len__(self): + return 1 + + def __getitem__(self, index): + if index == 0: + return self._x + elif index == 1: + return self._y + else: + raise IndexError('Index error') + + class GeometryHelper: """ Geometry helper class @@ -38,41 +59,38 @@ class GeometryHelper: @staticmethod def coordinate_to_map_point(coordinate, city): - return MapPoint((city.upper_corner[0] - coordinate[0])*2, (city.upper_corner[1] - coordinate[1])*2) - - @staticmethod - def point_between_point(point_1, point_2, x): - m = (point_1.y - point_2.y)/(point_1.x - point_2.x) - c = point_2.y - (m*point_2.x) - y = (m*x)+c - return MapPoint(x,y) + return MapPoint((city.upper_corner[0] - coordinate[0] / 1), (city.upper_corner[1] - coordinate[1] / 1)) @staticmethod def city_mapping(city, building_names=None): if building_names is None: building_names = [b.name for b in city.buildings] - x = int((city.upper_corner[0] - city.lower_corner[0]) * 2) - y = int((city.upper_corner[1] - city.lower_corner[1]) * 2) - city_map = [['' for _ in range(y+1)] for _ in range(x+1)] - img = Image.new('RGB', (x+1, y+1), "black") # create a new black image + x = int((city.upper_corner[0] - city.lower_corner[0]) / 1) + y = int((city.upper_corner[1] - city.lower_corner[1]) / 1) + city_map = [['' for _ in range(y + 1)] for _ in range(x + 1)] + img = Image.new('RGB', (x + 1, y + 1), "black") # create a new black image city_image = img.load() # create the pixel map - for building_name in building_names: building = city.city_object(building_name) for ground in building.grounds: length = len(ground.perimeter_polygon.coordinates) - 1 for i, coordinate in enumerate(ground.perimeter_polygon.coordinates): - j = i+1 + j = i + 1 if i == length: j = 0 next_coordinate = ground.perimeter_polygon.coordinates[j] - point_1 = GeometryHelper.coordinate_to_map_point(coordinate, city) - point_2 = GeometryHelper.coordinate_to_map_point(next_coordinate, city) - for x in range(point_1.x, point_2.x): - y = GeometryHelper.point_between_point(point_1, point_2, x).y + point = GeometryHelper.coordinate_to_map_point(coordinate, city) + distance = GeometryHelper.distance_between_points(coordinate, next_coordinate) + if distance == 0: + continue + delta_x = (coordinate[0] - next_coordinate[0]) / distance + delta_y = (coordinate[1] - next_coordinate[1]) / distance + for i in range(0, int(distance)): + x = MapPoint(point.x + (delta_x * i), point.y + (delta_y * i)).x + y = MapPoint(point.x + (delta_x * i), point.y + (delta_y * i)).y if city_map[x][y] == '': city_map[x][y] = building.name - city_image[x, y] = (90, 90, 90) + city_image[x, y] = (100, 0, 0) elif city_map[x][y] != building.name: neighbour = city.city_object(city_map[x][y]) if building.neighbours is None: @@ -194,6 +212,6 @@ class GeometryHelper: """ power = 0 for dimension in range(0, len(vertex1)): - power += math.pow(vertex2[dimension]-vertex1[dimension], 2) + power += math.pow(vertex2[dimension] - vertex1[dimension], 2) distance = math.sqrt(power) return distance