You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
62 lines
1.9 KiB
62 lines
1.9 KiB
import json |
|
|
|
from voronoiview.colors import Color |
|
from voronoiview.points import PointSet |
|
|
|
|
|
class PointManager(): |
|
""" |
|
A state class that represents the absolute state of the |
|
world in regards to points. |
|
""" |
|
|
|
point_set = None |
|
|
|
# Stores the direct results of running scipy's voronoi function. |
|
voronoi_results = None |
|
|
|
@staticmethod |
|
def load(location): |
|
""" |
|
Loads the JSON file from the location and populates point_set |
|
with it's contents. |
|
|
|
@param location The location of the JSON file. |
|
""" |
|
with open(location) as json_file: |
|
data = json.load(json_file) |
|
|
|
PointManager.point_set = PointSet(data['point_size'], |
|
data['viewport_width'], |
|
data['viewport_height']) |
|
|
|
for point in data['points']: |
|
# We will need to cast the string representation of color |
|
# back into a Color enum. |
|
PointManager.point_set.add_point(point['x'], point['y'], |
|
Color(point['color']), point['weight']) |
|
|
|
@staticmethod |
|
def save(location): |
|
""" |
|
Persists the point_set as a JSON file at location. |
|
|
|
@param location The persistence location. |
|
""" |
|
|
|
data = {} |
|
data['point_size'] = PointManager.point_set.point_size |
|
data['viewport_width'] = PointManager.point_set.viewport_width |
|
data['viewport_height'] = PointManager.point_set.viewport_height |
|
data['points'] = [] |
|
|
|
for p in PointManager.point_set.points: |
|
data['points'].append({ |
|
'x': p.x, |
|
'y': p.y, |
|
'color': p.color, |
|
'weight': p.weight |
|
}) |
|
|
|
with open(location, 'w') as out_file: |
|
json.dump(data, out_file)
|
|
|