# # point.py # # A class for Point objects # # CS 111 # import math class Point: """ A class for objects that represent points in the Cartesian plane. """ def __init__(self, init_x, init_y): """ constructor for a Point object that represents a point with coordinates (init_x, init_y) """ self.x = init_x self.y = init_y def __repr__(self): """ returns a string representation for the called Point object (self) """ s = '(' + str(self.x) + ', ' + str(self.y) + ')' return s def distance_from_origin(self): """ computes and returns the distance of the called Point object (self) from the origin (i.e., from (0, 0)) """ dist = math.sqrt(self.x**2 + self.y**2) return dist def move_down(self, amount): """ moves the called Point object (self) in a downwards direction by the specified amount """ self.y -= amount def __eq__(self, other): """ returns True if the called Point object (self) has the same coordinates as the Point object other, and False otherwise """ if self.x != other.x: return False elif self.y != other.y: return False else: return True def flip(self): """ negates and swaps the coordinates of the called Point object (self) """ # first negate the coordinates self.x *= -1 self.y *= -1 # then swap them temp = self.x self.x = self.y self.y = temp # no return value is needed, because the method modifies # the internals of the called object