-
Notifications
You must be signed in to change notification settings - Fork 0
/
polygon.py
83 lines (67 loc) · 2.54 KB
/
polygon.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
from tkinter import filedialog
class Polygon:
def __init__(self):
"""Constructor
"""
self.points = []
self.border_color = 'black'
self.inner_polygons = []
def add_inner_polygons(self, polygon):
"""Adds an inner-polygon into the list of an outer-polygon.
Inner_polygons can be obstacles.
:param polygon: Polygon
:return: None
"""
self.inner_polygons.append(polygon)
def add_point(self, x, y):
"""Sets a node (=point, corner) and adds it to a list
:param x: int; x-coordinate
:param y: int; y-coordinate
:return: None
"""
try:
self.points.append((int(x), int(y)))
except ValueError as v:
print("Values must be numbers.\nCurrent x: {} \nCurrent y: {}".format(x, y))
raise v
@property
def __transform_pointlist_to_saving_string(self):
"""Converts a points into a string which is later saved in a file.
Origin method: pFlowGRID.main.create_save_to_file_string()
:return: str
"""
save = str(len(self.inner_polygons)) + "\n"
# if len(currently_loaded_floor_plan) > 0:
# save += currently_loaded_floor_plan + "\n"
# else:
save += "-\n"
# count of nodes, then nodes
save += str(len(self.points)) + "\n"
for point in self.points:
save += str(point[0]) + "\t" + str(point[1]) + "\n"
# holes or obstacles
for polygon in self.inner_polygons:
save += str(len(polygon.points)) + "\n"
for point in polygon.points:
save += str(point[0]) + "\t" + str(point[1]) + "\n"
return save
def save_to_file(self, file=None):
"""Writes and saves the saving_string into the file.
Origin method: pFlowGRID.main.save_to_file()
File ending .nsv is used by pFlow.
:param file: str; path of the file
:return: None
"""
# .nsv -> nik save
if file is None:
file = filedialog.asksaveasfile(mode="w",
defaultextension=".nsv",
initialdir="./",
title="Choose where to save the file",
initialfile="save.nsv")
else:
file = open(file=file, mode="w")
if file is None:
return
file.write(self.__transform_pointlist_to_saving_string)
file.close()