Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from math import atan2, cos, sin, pi
- class Point2D:
- """
- Point 2D Class
- Each object is point in the x-y plane.
- Coordinates are 2 floating point numbers x and y.
- """
- __precision = 10
- def __init__(self, x=0, y=0):
- """
- Point2d Constructor
- Default value is (0, 0)
- """
- self.__r = (x**2 + y**2)**0.5
- self.__th = atan2(y, x)
- def getX(self):
- """
- Get radius of equivalent polar coordinate.
- """
- return round(self.__r * cos(self.__th), Point2D.__precision)
- def getY(self):
- """
- Get radius of equivalent polar coordinate.
- """
- return round(self.__r * sin(self.__th), Point2D.__precision)
- def translateX(self, x):
- """
- Translate in the x direction
- """
- nx = x + self.getX()
- ny = self.getY()
- self.__r = (nx**2 + ny**2)**0.5
- self.__th = atan2(ny, nx)
- def translateY(self, y):
- """
- Translate in the y direction
- """
- nx = self.getX()
- ny = y + self.getY()
- self.__r = (nx**2 + ny**2)**0.5
- self.__th = atan2(ny, nx)
- def getRadius(self):
- """
- Get radius of equivalent polar coordinate.
- """
- return round(self.__r, Point2D.__precision)
- def getTheta(self):
- """
- Get angle of equivalent polar coordinate.
- """
- return round(self.__th, Point2D.__precision)
- def extend(self, r):
- """
- Increase radius by a factor of r.
- """
- self.__r *= r
- def rotate(self, th):
- """
- Increase angle by th radians.
- """
- self.__th += th
- def getDistance(self, other):
- """
- Return distance between two points.
- """
- dx = self.getX() - other.getX()
- dy = self.getY() - other.getY()
- return (dx**2 + dy**2)**0.5
- def getSlope(self, other):
- """
- Return the slope of the line containing both points.
- """
- dx = self.getX() - other.getX()
- dy = self.getY() - other.getY()
- return dy/dx
- def getMidpoint(self, other):
- """
- Return a Point2D object with coordinates
- equal to the midpoint of the line segment
- connecting self and other.
- """
- dx = self.getX() + other.getX()
- dy = self.getY() + other.getY()
- return Point2D(dx/2.0, dy/2.0)
- from copy import deepcopy
- class Shape2D:
- """
- Shape 2D Class
- Each object is shape in the x-y plane.
- A shape in this context is defined as a set of points
- that are connected one after the other, where the
- last point is connected to the first point as well.
- """
- def __init__(self, points=[]):
- """
- Shape2D Constructor
- Defaults to no points yet.
- """
- self._points = points
- self._nsides = len(self._points)
- def set_points(self, new_points):
- self._points = new_points
- self._nsides = len(self._points)
- def clear_points(self):
- self._points = []
- self._nsides = 0
- def get_points(self):
- # Uses deep copy to ensure returned copy
- # does not modify original list.
- return deepcopy(self._points)
- def print_points(self):
- # Prints each point as (x, y)
- for p in self.get_points():
- print(f"({p.getX()}, {p.getY()}), ", end="")
- print()
- return deepcopy(self._points)
- def copy(self):
- # Returns a copy of the shape
- return Shape2D(self.get_points())
- def add_point(self, new_point):
- self._points.append(new_point)
- self._nsides = len(self._points)
- def get_allX(self):
- return [p.getX() for p in self.get_points()]
- def get_allY(self):
- return [p.getY() for p in self.get_points()]
- def translateX(self, x):
- """
- Translate all points to the x direction
- """
- for i in range(self._nsides):
- self._points[i].translateX(x)
- def translateY(self, y):
- """
- Translate all points to the y direction
- """
- for i in range(self._nsides):
- self._points[i].translateY(y)
- def rotate(self, th):
- """
- Rotates all points about the origin
- """
- for i in range(self._nsides):
- self._points[i].rotate(th)
- return
- def getPerimeter(self):
- """
- Returns the perimeter of the shape
- """
- perim = 0
- for i in range(self._nsides):
- perim += self._points[i-1].getdistance(self._points[i])
- return perim
- class Triangle(Shape2D):
- def __init__(self, points=[]):
- """
- Triangle Constructor
- Defaults to [(0,0), (1,0), (0,1)].
- """
- self.set_points(points)
- self._nsides = 3
- def set_points(self, points):
- if len(points) == 3:
- self._points = points
- else:
- print("Only Three Points Allowed, using default values")
- self._points = [Point2D(0,0), Point2D(1,0), Point2D(0,1)]
- class Square(Shape2D):
- def __init__(self, side=1, center=(0,0)):
- """
- Square Constructor
- Defaults to Unit Square
- centered at the origin.
- """
- self._points = [Point2D(0.5,0.5), Point2D(-0.5,0.5), \
- Point2D(-0.5,-0.5), Point2D(0.5,-0.5)]
- self._nsides = 4
- def scale(self, r):
- """
- Increases square side length by a factor of r.
- Center of the square is maintained.
- """
- for i in range(4):
- self._points[i].extend(r)
- def get_center(self):
- """
- Returns the center of the square.
- """
- return (0, 0)
- def set_center(self, c):
- """
- Sets the center of the square.
- """
- return
- import matplotlib.pyplot as plt
- # May require `python -m pip install -U matplotlib`
- class Drawing:
- def __init__(self, shape_list=[]):
- temp = [issubclass(type(x), Shape2D) for x in shape_list]
- if False in temp:
- # Check if any element of shape_list is not a Shape2D
- print("Invalid list of shapes, creating empty list instead.")
- self.__shape_list=[]
- # Create a list member that would store the given shapes
- self.__shape_list=[]
- def show(self, a=1):
- fig, ax = plt.subplots()
- # Plot each Shape2D in a figure
- for shape in self.__shape_list:
- x_list = shape.get_allX()
- y_list = shape.get_allY()
- ax.plot(x_list + [x_list[0]], y_list + [y_list[0]], alpha=a)
- plt.show()
- def add_shape(self, shape):
- if not (issubclass(type(shape), Shape2D)):
- print("Invalid Shape2D")
- return
- # Insert shape to list of Shape2D
- self.__shape_list.append(shape)
- def clear(self):
- # Remove all the Shape2D
- self.__shape_list=[]
Advertisement
Add Comment
Please, Sign In to add comment