# # point_extra.py # # A class for Point objects with methods from the extra-practice # exercises in Lab 10 # # 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 def quadrant(self): """ determines and returns the number of the quadrant (if any) in which the called Point object (self) falls, and 0 if it falls on an axis """ if self.x > 0 and self.y > 0: return 1 elif self.x < 0 and self.y > 0: return 2 elif self.x < 0 and self.y < 0: return 3 elif self.x > 0 and self.y < 0: return 4 else: return 0 def in_same_quadrant(self, other): """ returns True if the called Point object (self) and the Point object other are in the same quadrant, and False otherwise. If one or both of the Point objects are on an axis, the method returns False. """ self_quad = self.quadrant() other_quad = other.quadrant() # Check first whether either Point is on an axis. if self_quad == 0 or other_quad == 0: return False if self_quad == other_quad: return True else: return False