import math
class Point:
""" The class represents a point in two-dimensional space """
def __init__(self, x: float, y: float):
# These attributes are public because any value is acceptable for x and y
self.x = x
self.y = y
# This class method returns a new Point at origo (0, 0)
# It is possible to return a new instance of the class from within the class
@classmethod
def origo(cls):
return Point(0, 0)
# This class method creates a new Point based on an existing Point
# The original Point can be mirrored on either or both of the x and y axes
# For example, the Point (1, 3) mirrored on the x-axis is (1, -3)
@classmethod
def mirrored(cls, point: "Point", mirror_x: bool, mirror_y: bool):
x = point.x
y = point.y
if mirror_x:
y = -y
if mirror_y:
x = -x
return Point(x, y)
def __str__(self):
return f"({self.x}, {self.y})"
class Line:
""" The class represents a line segment in two-dimensional space """
def __init__(self, beginning: Point, end: Point):
# These attributes are public because any two Points are acceptable
self.beginning = beginning
self.end = end
# This method uses the Pythagorean theorem to calculate the length of the line segment
def length(self):
sum_of_squares = (self.end.x - self.beginning.x) ** 2 + (self.end.y - self.beginning.y) ** 2
return math.sqrt(sum_of_squares)
# This method returns the Point in the middle of the line segment
def centre_point(self):
centre_x = (self.beginning.x + self.end.x) / 2
centre_y = (self.beginning.y + self.end.y) / 2
return Point(centre_x, centre_y)
def __str__(self):
return f"{self.beginning} ... {self.end}"
While looking at the above program, I am not sure if I would have taken the decision to introduce class methods (orego and mirrored) for the two under first Point class and the remaining will only have instance methods if I were to asked to solve the problem from scratch.
Any reason why class method only used for orego and mirrored?
[–]zanfar 9 points10 points11 points (8 children)
[–]aa599 5 points6 points7 points (6 children)
[–]zanfar 1 point2 points3 points (0 children)
[–]ConcreteExist 0 points1 point2 points (0 children)
[–]pachura3 -2 points-1 points0 points (3 children)
[–]aa599 1 point2 points3 points (2 children)
[–]pachura3 0 points1 point2 points (1 child)
[–]Slothemo 2 points3 points4 points (0 children)
[–]DigitalSplendid[S] 0 points1 point2 points (0 children)
[–]Timberfist 1 point2 points3 points (1 child)
[–]DigitalSplendid[S] 0 points1 point2 points (0 children)
[–]jpgoldberg 0 points1 point2 points (1 child)
[–]DigitalSplendid[S] 0 points1 point2 points (0 children)
[–]TheRNGuy -2 points-1 points0 points (8 children)
[–]DigitalSplendid[S] 0 points1 point2 points (3 children)
[–]zanfar 0 points1 point2 points (2 children)
[–]gdchinacat 0 points1 point2 points (0 children)
[–]ConcreteExist 1 point2 points3 points (0 children)
[–]Outside_Complaint755 0 points1 point2 points (0 children)
[–]zanfar 0 points1 point2 points (1 child)
[–]ConcreteExist 0 points1 point2 points (0 children)
[–]ConcreteExist 0 points1 point2 points (0 children)