Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Finding distance between two points in OOP

The program creates a class for points and has three functions: one that shows the coordinates of the point, another that moves the coordinates, and the last one that calculates the distance between them. I’m stuck with the last one I don’t know how to do that.

from math import sqrt


class Points:
    def __init__(self, x1, y1):
        self.x1 = x1
        self.y1 = y1

    def show(self):
        return (self.x1, self.y1)

    def move(self, x2, y2):
        self.x1 += x2
        self.y1 += y2

    def dist(self, point):
        return sqrt(((point[0] - self.x1) ** 2) + ((point[1] - self.y1) ** 2))


p1 = Points(2, 3)
p2 = Points(3, 3)
print(p1.show())

print(p2.show())

p1.move(10, -10)
print(p1.show())

print(p2.show())

print(p1.dist(p2))

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Access point members in dist like this:

return sqrt(((point.x1 - self.x1) ** 2) + ((point.y1 - self.y1) ** 2))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading