class Point(): """Holds on a point (x,y) in the plane""" def __init__(self, x=0, y=0): assert isinstance(x, (int, float)) and isinstance(y, (int, float)) self.x = float(x) self.y = float(y) p = Point(1,2) print("point", p.x, p.y) origin = Point() print("origin", origin.x, origin.y) p class Point(): """Holds on a point (x,y) in the plane""" def __init__(self, x=0, y=0): assert isinstance(x, (int, float)) and isinstance(y, (int, float)) self.x = float(x) self.y = float(y) def __repr__(self): return "Point(" + str(self.x) + ", " + str(self.y) + ")" Point(1,2) class Point(): """Holds on a point (x,y) in the plane""" def __init__(self, x=0, y=0): assert isinstance(x, (int, float)) and isinstance(y, (int, float)) self.x = float(x) self.y = float(y) def __repr__(self): return "Point(" + str(self.x) + ", " + str(self.y) + ")" def add(self, other): assert isinstance(other, (int, Point)) if isinstance(other, Point): return Point(self.x + other.x , self.y + other.y) else: # other is int, taken as (int, int) return Point(self.x + other , self.y + other) Point(1,1).add(Point(2,2)) Point(1,1).add(2) class Point(): """Holds on a point (x,y) in the plane""" def __init__(self, x=0, y=0): assert isinstance(x, (int, float)) and isinstance(y, (int, float)) self.x = float(x) self.y = float(y) def __repr__(self): return "Point(" + str(self.x) + ", " + str(self.y) + ")" def __add__(self, other): assert isinstance(other, (int, Point)) if isinstance(other, Point): return Point(self.x + other.x , self.y + other.y) else: # other is int, taken as (int, int) return Point(self.x + other , self.y + other) Point(1,1) + Point(2,2) Point(1,1) + 2 Point(1,2) == Point(2,1) Point(1,2) == Point(1,2) p = Point() p == p Point(1,2) > Point(2,1) class Point(): """Holds on a point (x,y) in the plane""" def __init__(self, x=0, y=0): assert isinstance(x, (int, float)) and isinstance(y, (int, float)) self.x = float(x) self.y = float(y) def __repr__(self): return "Point(" + str(self.x) + ", " + str(self.y) + ")" def __add__(self, other): assert isinstance(other, (int, Point)) if isinstance(other, Point): return Point(self.x + other.x , self.y + other.y) else: # other is int, taken as (int, int) return Point(self.x + other , self.y + other) def __eq__(self, other): return (self.x, self.y) == (other.x, other.y) def __gt__(self, other): return (self.x > other.x and self.y > other.y) Point(1,0) == Point(1,2) Point(1,0) == Point(1,0) Point(1,0) > Point(1,2) Point(5,6) > Point(1,2) class Point(): """Holds on a point (x,y) in the plane""" def __init__(self, x=0, y=0): assert isinstance(x, (int, float)) and isinstance(y, (int, float)) self.x = float(x) self.y = float(y) def __repr__(self): return "Point(" + str(self.x) + ", " + str(self.y) + ")" def __eq__(self, other): return (self.x, self.y) == (other.x, other.y) def __gt__(self, other): return (self.x > other.x and self.y > other.y) def __add__(self, other): assert isinstance(other, (int, Point)) if isinstance(other, Point): return Point(self.x + other.x , self.y + other.y) else: # other is int, taken as (int, int) return Point(self.x + other , self.y + other) def increment(self, other): '''this method changes self (add "inplace")''' assert isinstance(other,Point) self.x += other.x self.y += other.y p = Point(6.5, 7) p + Point(1,2) print(p) p.increment(Point(1,2)) print(p) class Point(): """Holds on a point (x,y) in the plane""" def __init__(self, x=0, y=0): assert isinstance(x, (int, float)) and isinstance(y, (int, float)) self.x = float(x) self.y = float(y) def __repr__(self): return "Point(" + str(self.x) + ", " + str(self.y) + ")" def __eq__(self, other): return (self.x, self.y) == (other.x, other.y) def __lt__(self, other): return (self.x < other.x and self.y < other.y) def __add__(self, other): assert isinstance(other, (int, Point)) if isinstance(other, Point): return Point(self.x + other.x , self.y + other.y) else: # other is int, taken as (int, int) return Point(self.x + other , self.y + other) def increment(self, other): '''this method changes self (add "inplace")''' assert isinstance(other,Point) self.x += other.x self.y += other.y def is_extreme(self, *points): for point in points: if not self > point: return False return True p = Point(5, 6) p.is_extreme(Point(1,1)) p.is_extreme(Point(1,1), Point(2,5), Point(6,2)) Point.is_extreme(Point(7,8), Point(1,1), Point(4,5), Point(2,3)) class Rectangle1(): """ Holds a parallel-axes rectangle by storing two points lower left vertex - llv upper right vertex - urv """ def __init__(self, lower_left_vertex, upper_right_vertex): assert isinstance(lower_left_vertex, Point) assert isinstance(upper_right_vertex, Point) assert lower_left_vertex < upper_right_vertex self.llv = lower_left_vertex self.urv = upper_right_vertex def __repr__(self): representation = "Rectangle with lower left {0} and upper right {1}" return representation.format(self.llv, self.urv) def dimensions(self): height = self.urv.y - self.llv.y width = self.urv.x - self.llv.x return height, width def area(self): height, width = self.dimensions() area = height * width return area def transpose(self): """ Reflection with regard to the line passing through lower left vertex with angle 315 (-45) degrees """ height, width = self.dimensions() self.urv = self.llv self.llv = Point(self.urv.x - height, self.urv.y - width) rec = Rectangle1(Point(), Point(2,1)) print(rec) print("Area:", rec.area()) print("Dimensions:", rec.dimensions()) rec.transpose() print("Transposed:", rec) class Rectangle2(): """ Holds a parallel-axes rectangle by storing lower left point, height and width """ def __init__(self, point, height, width): assert isinstance(point, Point) assert isinstance(height, (int,float)) assert isinstance(width, (int,float)) assert height > 0 assert width > 0 self.point = point self.height = float(height) self.width = float(width) def __repr__(self): representation = "Rectangle with lower left {0} and upper right {1}" return representation.format(self.point, Point(self.point.x + self.width, self.point.y + self.height)) def dimensions(self): return self.height, self.width def area(self): area = self.height * self.width return area def transpose(self): self.point = Point(self.point.x - self.height , self.point.y - self.width) self.height, self.width = self.width, self.height rec = Rectangle2(Point(), 1, 2) print(rec) print("Area:", rec.area()) print("Dimensions:", rec.dimensions()) rec.transpose() print("Transposed:", rec)