#!/usr/bin/python class Shape: W=50 H=40 def __init__(self): self.inside=[] def isIn(self,point): return self.inside.__contains__(point) def __and__(self,other): return self.and_(other) def __or__(self,other): return self.or_(other) def __sub__(self,other): return self.andnot_(other) def __div__(self,other): return self.xor_(other) def and_(self,shape): out=Shape() for x in range(Shape.W): for y in range(Shape.H): if self.isIn((x,y)) and shape.isIn((x,y)): out.inside.append((x,y)) return out def or_(self,shape): out=Shape() for x in range(Shape.W): for y in range(Shape.H): if self.isIn((x,y)) or shape.isIn((x,y)): out.inside.append((x,y)) return out def xor_(self,shape): out=Shape() for x in range(Shape.W): for y in range(Shape.H): if (self.isIn((x,y)) or shape.isIn((x,y))) and not (self.isIn((x,y)) and shape.isIn((x,y))): out.inside.append((x,y)) return out def andnot_(self,shape): out=Shape() for x in range(Shape.W): for y in range(Shape.H): if self.isIn((x,y)) and not shape.isIn((x,y)): out.inside.append((x,y)) return out def draw(self): out="" for y in range(Shape.H): for x in range(Shape.W): if self.isIn((x,y)): out+="#" else: out+=" " out+="\n" return out def __repr__(self): return self.draw() class Circle(Shape): def __init__(self, position, radius): Shape.__init__(self) x1=position[0] y1=position[1] for x in range(position[0]-radius,position[0]+radius): for y in range(position[1]-radius,position[1]+radius): x2=x y2=y if ((x1-x2)**2 + (y1-y2)**2)**0.5 <=radius: self.inside.append((x,y)) class Square(Shape): def __init__(self, position, size): Shape.__init__(self) for x in range(position[0],position[0]+size): for y in range(position[1],position[1]+size): self.inside.append((x,y)) if __name__ == '__main__': c=Circle((19,19),5) s=Square((18,15),10) c2=Circle((23,20),9) print (s - c)/c2