18.6. Solutions

Solution 18.1. A Point class

Exercise 18.1

class Point:

    def __init__(self, x, y):
        self.x = x
        self.y = y

    def move(self, dx, dy):
        self.x = self.x + dx
        self.y = self.y + dy

    def show(self):
        return self.x, self.y
      

Solution 18.2. A Point class (continued)

Exercise 18.2

class Point:

    def __init__(self, x, y):
        self.x = x
        self.y = y

    def move(self, dx, dy):
        self.x = self.x + dx
        self.y = self.y + dy

    def show(self):
        return self.x, self.y

    def distance(self, p):
        dx = self.x - other.x
        dy = self.y - other.y
        return sqrt(dx**2 + dy**2)