From 778240a1d38b9d11133c9f72dcfd0a507ffc5738 Mon Sep 17 00:00:00 2001 From: Pascal Lais Date: Sun, 8 Mar 2020 11:00:14 +0100 Subject: [PATCH] Add linesegment module The module defines a LineSegment class that displays a line segment in two-dimensional coordinate system. It is defined by a start- and an endpoint. It also contains a method to calculate the length. --- linesegment.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 linesegment.py diff --git a/linesegment.py b/linesegment.py new file mode 100644 index 0000000..e81448c --- /dev/null +++ b/linesegment.py @@ -0,0 +1,33 @@ +#!/usr/bin/python3 +"""The linesegment module defines the class line_segment""" + +import point + +class LineSegment: + """The class LineSegment defines a two-dimensional line segment in the coordinate system + starting at a startpoint and ending at a endpoint.""" + def __init__(self, startpoint=point.Point(), endpoint=point.Point()): + """Initialize the line segment.""" + self.set_startpoint(startpoint) + self.set_endpoint(endpoint) + def set_startpoint(self, new_startpoint): + """Set or change the startpoint of the line segment. If not a Point object is given + the startpoint gets set to a default value of Point(0,0)""" + self.__startpoint = new_startpoint + def get_startpoint(self): + """Returns the current startpoint""" + return self.__startpoint + def set_endpoint(self, new_endpoint): + """Set or change the endpoint of the line segment. If not a Point object is given + the endpoint gets set to a default value of Point(0,0)""" + self.__endpoint = new_endpoint + def get_endpoint(self): + """Returns the current endpoint""" + return self.__endpoint + def get_length(self): + """Returns the length of the line segment.""" + diff_x = abs(self.__startpoint.get_x() - self.__endpoint.get_x()) + diff_y = abs(self.__startpoint.get_y() - self.__endpoint.get_y()) + return ((diff_x ** 2) + (diff_y ** 2)) ** (1/2.0) + def draw(self): + """TODO: Draw the line segment in a canvas"""