A computational geometry learning and experimentation tool.
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.

59 lines
1.7 KiB

import json
from .colors import Color
from .points import PointSet
class PointManager():
"""
A state class that represents the absolute state of the
world in regards to points.
"""
point_set = None
centroids = []
@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']))
@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
})
with open(location, 'w') as out_file:
json.dump(data, out_file)