Question
Complete the codebase for TwoDPoint, Quadrilateral, Rectangle, and Square. The incomplete portions of the code are marked with TODO and the methods are provided with
Complete the codebase for TwoDPoint, Quadrilateral, Rectangle, and Square. The incomplete portions of the code are marked with TODO and the methods are provided with documentation explaining what each incomplete method should do. There may also be a few minor bugs in the provided code. It is a part of this assignment to fix these (you are encouraged to use the debugger and discover these bugs by use of unit tests, if necessary).
TwoDPoint
from typing import List
class TwoDPoint:
def __init__(self, x, y) -> None:
self.__x = x
self.__y = y
@property
def x(self):
return self.__x
@property
def y(self):
return self.__y
def __eq__(self, other: object) -> bool:
return False # TODO
def __ne__(self, other: object) -> bool:
return not self.__eq__(other)
def __str__(self) -> str:
return '(%g, %g)' % (self.__x, self.__y)
# TODO: add magic methods such that two TwoDPoint objects can be added and subtracted coordinate-wise just by using
# syntax of the form p + q or p - q
@staticmethod
def from_coordinates(coordinates: List[float]):
if len(coordinates) % 2 != 0:
raise Exception("Odd number of floats given to build a list of 2-d points")
points = []
it = iter(coordinates)
for x in it:
points.append(TwoDPoint(x, next(it)))
return points
Quadrilateral
from .two_d_point import TwoDPoint
class Quadrilateral:
def __init__(self, *floats):
points = TwoDPoint.from_coordinates(list(floats))
self.__vertices = tuple(points[0:3])
@property
def vertices(self):
return self.__vertices
def side_lengths(self):
"""Returns a tuple of four floats, each denoting the length of a side of this quadrilateral. The value must be
ordered clockwise, starting from the top left corner."""
return 0, 0, 0, 0 # TODO
def smallest_x(self):
"""Returns the x-coordinate of the vertex with the smallest x-value of the four vertices of this
quadrilateral."""
return 0 # TODO
Rectangle
from .quadrilateral import Quadrilateral
from .two_d_point import TwoDPoint
class Rectangle(Quadrilateral):
def __init__(self, *floats):
super().__init__(*floats)
if not self.__is_member():
raise TypeError("A rectangle cannot be formed by the given coordinates.")
def __is_member(self):
"""Returns True if the given coordinates form a valid rectangle, and False otherwise."""
return False # TODO
def center(self):
"""Returns the center of this rectangle, calculated to be the point of intersection of its diagonals."""
return TwoDPoint(0, 0) # TODO
def area(self):
"""Returns the area of this rectangle. The implementation invokes the side_lengths() method from the superclass,
and computes the product of this rectangle's length and width."""
return 0 # TODO
Square
from .rectangle import Rectangle
from .quadrilateral import Quadrilateral
class Square(Rectangle):
def __init__(self, *floats):
super().__init__(*floats)
if not self.__is_member():
raise TypeError("A square cannot be formed by the given coordinates.")
def snap(self):
"""Snaps the sides of the square such that each corner (x,y) is modified to be a corner (x',y') where x' is the
integer value closest to x and y' is the integer value closest to y. This, of course, may change the shape to a
general quadrilateral, hence the return type. The only exception is when the square is positioned in a way where
this approximation will lead it to vanish into a single point. In that case, a call to snap() will not modify
this square in any way."""
return Quadrilateral() # TODO
For this assignment, you may assume that all shapes have sides that are axis-aligned. For example, rectangles and squares will have sides that are parallel to the x and y axes. This does not, of course, make sense for quadrilaterals in general.
Python version3.x
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started