In: Computer Science
in Python,
Define the class called Line which represents a line with equation ?=??+? with input slope ? and intercept ? to initialize the instances. It should include:
attributes named ? and ? to represent slope and intercept.
method named intersect to return the list, containing coordinates
of the intersection point of two lines.
support for + operator to compute the addition of two equations.
The sum of two Line objects ?=?1?+?1 and ?=?2?+?2 is defined as the
line ?=(?1+?2)?+?1+?2 .
printable representation for the equation of line, which we have
already defined in __repr__ speical method below.
class Line:
    # initializing constructor with slope and y-intercept
    def __init__(self, k, b):
        self.k = k
        self.b = b
    # return intersection point of two lines
    def intersect(self, line):
        # formula of point of intersection (x,y)
        x_ = (line.b - self.b)/(self.k - line.k)
        y_ = self.k * x_ + self.b
        # returns intersection point
        return [x_, y_]
    # adds two lines and returns the new line
    def __add__(self, line):
        k_ = self.k + line.k
        b_ = self.b + line.b
        return Line(k_, b_)
    # prints the line
    def __repr__(self):
        return "y = {}x + {}".format(self.k, self.b)
line1 = Line(2,3)
line2 = Line(3,4)
# prints the intersection of two lines
print(line1.intersect(line2))
# adds two lines
print(line1 + line2)
Code
Output