untested neighbours detection implemented.

This commit is contained in:
Guille Gutierrez 2023-02-23 15:35:13 -05:00
parent 143e48bb7b
commit c5110585c7

View File

@ -16,14 +16,35 @@ from hub.helpers.location import Location
from PIL import Image from PIL import Image
class MapPoint: class MapPoint:
def __init__(self, x, y): def __init__(self, x, y):
self.x = int(x) self._x = int(x)
self.y = int(y) self._y = int(y)
@property
def x(self):
return self._x
@property
def y(self):
return self._y
def __str__(self): def __str__(self):
return f'({self.x}, {self.y})' 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: class GeometryHelper:
""" """
Geometry helper class Geometry helper class
@ -38,41 +59,38 @@ class GeometryHelper:
@staticmethod @staticmethod
def coordinate_to_map_point(coordinate, city): def coordinate_to_map_point(coordinate, city):
return MapPoint((city.upper_corner[0] - coordinate[0])*2, (city.upper_corner[1] - coordinate[1])*2) return MapPoint((city.upper_corner[0] - coordinate[0] / 1), (city.upper_corner[1] - coordinate[1] / 1))
@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)
@staticmethod @staticmethod
def city_mapping(city, building_names=None): def city_mapping(city, building_names=None):
if building_names is None: if building_names is None:
building_names = [b.name for b in city.buildings] building_names = [b.name for b in city.buildings]
x = int((city.upper_corner[0] - city.lower_corner[0]) * 2) x = int((city.upper_corner[0] - city.lower_corner[0]) / 1)
y = int((city.upper_corner[1] - city.lower_corner[1]) * 2) y = int((city.upper_corner[1] - city.lower_corner[1]) / 1)
city_map = [['' for _ in range(y+1)] for _ in range(x+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 img = Image.new('RGB', (x + 1, y + 1), "black") # create a new black image
city_image = img.load() # create the pixel map city_image = img.load() # create the pixel map
for building_name in building_names: for building_name in building_names:
building = city.city_object(building_name) building = city.city_object(building_name)
for ground in building.grounds: for ground in building.grounds:
length = len(ground.perimeter_polygon.coordinates) - 1 length = len(ground.perimeter_polygon.coordinates) - 1
for i, coordinate in enumerate(ground.perimeter_polygon.coordinates): for i, coordinate in enumerate(ground.perimeter_polygon.coordinates):
j = i+1 j = i + 1
if i == length: if i == length:
j = 0 j = 0
next_coordinate = ground.perimeter_polygon.coordinates[j] next_coordinate = ground.perimeter_polygon.coordinates[j]
point_1 = GeometryHelper.coordinate_to_map_point(coordinate, city) point = GeometryHelper.coordinate_to_map_point(coordinate, city)
point_2 = GeometryHelper.coordinate_to_map_point(next_coordinate, city) distance = GeometryHelper.distance_between_points(coordinate, next_coordinate)
for x in range(point_1.x, point_2.x): if distance == 0:
y = GeometryHelper.point_between_point(point_1, point_2, x).y 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] == '': if city_map[x][y] == '':
city_map[x][y] = building.name 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: elif city_map[x][y] != building.name:
neighbour = city.city_object(city_map[x][y]) neighbour = city.city_object(city_map[x][y])
if building.neighbours is None: if building.neighbours is None:
@ -194,6 +212,6 @@ class GeometryHelper:
""" """
power = 0 power = 0
for dimension in range(0, len(vertex1)): 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) distance = math.sqrt(power)
return distance return distance