diff --git a/main.py b/main.py index 85e7ef2..a229945 100644 --- a/main.py +++ b/main.py @@ -306,15 +306,15 @@ random_points = [Point2D(random.randint(min_val, max_val), random.randint( # random_points = [Point2D(-40, -56), Point2D(-94, 92), Point2D(19, -47), Point2D( # 100, 59), Point2D(-85, -73), Point2D(-33, -9), Point2D(57, -25), Point2D(51, -34)] -# random_points = random_points[0].optimized_path(random_points) +random_points = random_points[0].optimized_path(random_points) -# print(random_points) +print(random_points) # random_points = [Point2D(94, 71), Point2D(-12, 54), Point2D(-28, 10), Point2D( # 0, -33), Point2D(80, -50), Point2D(73, -89), Point2D(-86, -3), Point2D(-82, 92)] -random_points = [Point2D(-59, -21), Point2D(-43, -19), Point2D(-61, 19), Point2D( - 45, 19), Point2D(80, -4), Point2D(99, 2), Point2D(47, 63), Point2D(100, -91)] +# random_points = [Point2D(-59, -21), Point2D(-43, -19), Point2D(-61, 19), Point2D( +# 45, 19), Point2D(80, -4), Point2D(99, 2), Point2D(47, 63), Point2D(100, -91)] p = Polyline(random_points) diff --git a/networks/geometry/Point2D.py b/networks/geometry/Point2D.py index 9848d5b..d748303 100644 --- a/networks/geometry/Point2D.py +++ b/networks/geometry/Point2D.py @@ -72,7 +72,7 @@ class Point2D: else: return (s_p <= 0) and (t_p <= 0) and (s_p + t_p) >= d - def nearest(self, points: List["Point2D"]) -> "Point2D": + def nearest(self, points: List["Point2D"], return_index: bool = False) -> Union["Point2D", List[Union["Point2D", int]]]: """Return the nearest point. If multiple nearest point, returns the first in the list. Args: @@ -81,6 +81,9 @@ class Point2D: Returns: Point2D: The nearest point, and if multiple, the first in the list. """ + if return_index: + return min( + enumerate(points), key=lambda pair: self.distance(pair[1])) return min(points, key=lambda point: self.distance(point)) def optimized_path(self, points: List["Point2D"]) -> List["Point2D"]: diff --git a/networks/geometry/Polyline.py b/networks/geometry/Polyline.py index e495466..9d067cb 100644 --- a/networks/geometry/Polyline.py +++ b/networks/geometry/Polyline.py @@ -61,7 +61,6 @@ class Polyline: self.get_arcs_intersections() self.get_arcs() self.get_segments() - print("\nlekj\n", self.segments, "\nklj\n") self.total_line_output = [] for i in range(1, self.length_polyline-1): diff --git a/networks/roads_2/Roads.py b/networks/roads_2/Roads.py index d9d8603..23cc062 100644 --- a/networks/roads_2/Roads.py +++ b/networks/roads_2/Roads.py @@ -52,9 +52,12 @@ class Road: def _projection(self): nearest_points_to_reference = [] for i in range(len(self.coordinates)): - nearest_points_to_reference.append(Point3D.insert_3d([Point3D.to_2d([self.coordinates[i]], 'y')[0].nearest( - self.polyline.total_line_output)], 'y', [self.coordinates[i].y])[0]) - print(nearest_points_to_reference) + # nearest_points_to_reference.append(Point3D.insert_3d([Point3D.to_2d([self.coordinates[i]], 'y')[0].nearest( + # self.polyline.total_line_output, return_index=True)], 'y', [self.coordinates[i].y])[0]) + index, point = Point3D.to_2d([self.coordinates[i]], 'y')[0].nearest( + self.polyline.total_line_output, return_index=True) + nearest_points_to_reference.append( + Point2D(index, self.coordinates[i].y)) def place(self): editor = Editor(buffering=True) diff --git a/output_image.png b/output_image.png index 9564ae9..a07f8a0 100644 Binary files a/output_image.png and b/output_image.png differ