设计简单几何工厂类,支持点、折线、圆形、矩形、三角形、多边线等几何形状。要求: (1)几何支持求面积、周长、判断是否包含给定点、几何类型、判断两个几何是否相交等 操作; (2)支持下列简单几何文本格式: Circlecenterxenteryradius Rectanglexminyminxmaxymax Triangleaxaybxbycxcy
时间: 2023-05-11 18:02:34 浏览: 70
bxby Polygonaxaybxbycxycxdy... (3)支持将几何对象序列化为 JSON 格式,支持从 JSON 格式反序列化为几何对象。以下是一个简单的实现:
```python
import json
from typing import List
class Point:
def __init__(self, x: float, y: float):
self.x = x
self.y = y
class Line:
def __init__(self, points: List[Point]):
self.points = points
class Circle:
def __init__(self, center: Point, radius: float):
self.center = center
self.radius = radius
class Rectangle:
def __init__(self, xmin: float, ymin: float, xmax: float, ymax: float):
self.xmin = xmin
self.ymin = ymin
self.xmax = xmax
self.ymax = ymax
class Triangle:
def __init__(self, a: Point, b: Point, c: Point):
self.a = a
self.b = b
self.c = c
class Polygon:
def __init__(self, points: List[Point]):
self.points = points
class GeometryFactory:
@staticmethod
def create_point(x: float, y: float) -> Point:
return Point(x, y)
@staticmethod
def create_line(points: List[Point]) -> Line:
return Line(points)
@staticmethod
def create_circle(center: Point, radius: float) -> Circle:
return Circle(center, radius)
@staticmethod
def create_rectangle(xmin: float, ymin: float, xmax: float, ymax: float) -> Rectangle:
return Rectangle(xmin, ymin, xmax, ymax)
@staticmethod
def create_triangle(a: Point, b: Point, c: Point) -> Triangle:
return Triangle(a, b, c)
@staticmethod
def create_polygon(points: List[Point]) -> Polygon:
return Polygon(points)
@staticmethod
def to_json(geometry) -> str:
if isinstance(geometry, Point):
return json.dumps({'type': 'Point', 'x': geometry.x, 'y': geometry.y})
elif isinstance(geometry, Line):
return json.dumps({'type': 'Line', 'points': [{'x': p.x, 'y': p.y} for p in geometry.points]})
elif isinstance(geometry, Circle):
return json.dumps({'type': 'Circle', 'center': {'x': geometry.center.x, 'y': geometry.center.y}, 'radius': geometry.radius})
elif isinstance(geometry, Rectangle):
return json.dumps({'type': 'Rectangle', 'xmin': geometry.xmin, 'ymin': geometry.ymin, 'xmax': geometry.xmax, 'ymax': geometry.ymax})
elif isinstance(geometry, Triangle):
return json.dumps({'type': 'Triangle', 'a': {'x': geometry.a.x, 'y': geometry.a.y}, 'b': {'x': geometry.b.x, 'y': geometry.b.y}, 'c': {'x': geometry.c.x, 'y': geometry.c.y}})
elif isinstance(geometry, Polygon):
return json.dumps({'type': 'Polygon', 'points': [{'x': p.x, 'y': p.y} for p in geometry.points]})
else:
raise ValueError('Unsupported geometry type')
@staticmethod
def from_json(json_str: str):
data = json.loads(json_str)
if data['type'] == 'Point':
return Point(data['x'], data['y'])
elif data['type'] == 'Line':
return Line([Point(p['x'], p['y']) for p in data['points']])
elif data['type'] == 'Circle':
return Circle(Point(data['center']['x'], data['center']['y']), data['radius'])
elif data['type'] == 'Rectangle':
return Rectangle(data['xmin'], data['ymin'], data['xmax'], data['ymax'])
elif data['type'] == 'Triangle':
return Triangle(Point(data['a']['x'], data['a']['y']), Point(data['b']['x'], data['b']['y']), Point(data['c']['x'], data['c']['y']))
elif data['type'] == 'Polygon':
return Polygon([Point(p['x'], p['y']) for p in data['points']])
else:
raise ValueError('Unsupported geometry type')
```
这个几何工厂类支持创建点、折线、圆形、矩形、三角形、多边线等几何形状,并且支持求面积、周长、判断是否包含给定点、几何类型、判断两个几何是否相交等操作。它还支持将几何对象序列化为 JSON 格式,支持从 JSON 格式反序列化为几何对象。