#!/usr/bin/env python # coding: utf-8 # In[54]: import turtle #using turtle to draw the polygons class Polygon: def __init__(self, sides, name, size=100, color='black', line_thickness=3): self.sides = sides self.name = name self.size = size self.color = color self.line_thickness = line_thickness self.interior_angles = (self.sides - 2) * 180 self.angle = self.interior_angles / self.sides def draw(self): turtle.color(self.color) turtle.pensize(self.line_thickness) for i in range(self.sides): turtle.forward(100) turtle.right(180 - self.angle) # In[11]: square = Polygon(4, "Square", color='green', line_thickness=3) penatgon = Polygon(5, "Pentagon", color='red', line_thickness=18) hexagon = Polygon(6, "Hexagon", color='blue', line_thickness=11) # In[12]: print(square.sides) print(square.name) print(square.interior_angles) print(square.angle) # In[13]: print(penatgon.sides) print(penatgon.name) print(penatgon.interior_angles) print(penatgon.angle) # In[14]: print(hexagon.sides) print(hexagon.name) print(hexagon.interior_angles) print(hexagon.angle) # In[17]: hexagon.draw() # In[19]: penatgon.draw() # In[21]: square.draw() # In[62]: #Inheritance of properties froma parent class using super() class Square(Polygon): def __init__(self, size=100, color='black', line_thickness=3): super().__init__(4, 'Square', size, color, line_thickness) def draw(self): turtle.begin_fill() super().draw() turtle.end_fill() square = Square(color='#666666', size=200) square.draw() turtle.done() # In[75]: #Plotting Points with Classes import matplotlib.pyplot as plt class Point: def __init__(self, x, y, color="red", size=10): self.x = x self.y = y self.color = color self.size = size def __add__(self, other): if isinstance(other, Point): x = self.x + other.x y = self.y + other.y return Point(x,y) else: x = self.x + other y = self.y + other return Point(x,y) def plot(self): plt.scatter(self.x, self.y, c=self.color, s=self.size) if __name__ == "__main__": a = Point(1,3) b = Point(2,4) c = a + b c.plot() plt.show()