{ "metadata": { "name": "" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Classes\n", "\n", "Write a class called `Point` which stores a point object in $\\mathbb{R}^2$. It should have a method `distance` such that for two points `P` and `Q`, `P.distance(Q)` returns the distance from `P` to `Q`.\n", "\n", "Write another class `Polygon` which stores an *ordered* list of point objects to represent a polygon. It should have methods `perimeter` and `area` to which returns the perimeter and the area of the polygon. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Sample code" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Imports\n", "import math" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 1 }, { "cell_type": "code", "collapsed": false, "input": [ "class Point :\n", " \"\"\"Imitates a point on R^2\n", " Attributes : x, y\n", " Methods : distance(another point)\n", " \"\"\"\n", " \n", " def __init__(self, x, y) :\n", " self.x = x\n", " self.y = y\n", " \n", " def distance(self, p) :\n", " x1 = self.x\n", " y1 = self.y\n", " x2 = p.x\n", " y2 = p.y\n", " \n", " d = math.sqrt((x1 - x2)*(x1 - x2) + (y1 - y2)*(y1 - y2))\n", " return d\n", " \n", " def __str__(self) :\n", " return \"(%g, %g)\" % (self.x, self.y)\n", " \n", " def __repr__(self) :\n", " return \"Point(%g, %g)\" % (self.x, self.y)" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 2 }, { "cell_type": "code", "collapsed": false, "input": [ "if __name__ == '__main__' :\n", " p = Point(1, 0)\n", " q = Point(0, 1)\n", " print \"Distance between %s and %s is %g.\" % (p, q, p.distance(q))" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Distance between (1, 0) and (0, 1) is 1.41421.\n" ] } ], "prompt_number": 3 }, { "cell_type": "code", "collapsed": false, "input": [ "class Polygon :\n", " \"\"\"Implements a polygon as a sequence of points.\n", " Attributes : lop : list of points\n", " Methods :\n", " add_point(p) : Adds p to lop.\n", " permimeter : returns the perimeter.\n", " \"\"\"\n", " \n", " def __init__(self, lop) :\n", " self.lop = lop\n", " \n", " def add_point(self, p) :\n", " self.lop.append(p)\n", " \n", " def perimeter(self) :\n", " l = self.lop\n", " per = 0\n", " for i in range(len(l)) :\n", " if i == 0 :\n", " per += l[i].distance(l[-1])\n", " else :\n", " per += l[i].distance(l[i-1])\n", " return per\n", " \n", " def shoelaceterm(self, i) :\n", " l = self.lop\n", " n = len(l)\n", " if i == 0 : \n", " l1x = l[n - 1].x\n", " l1y = l[n - 1].y\n", " l2x = l[0].x\n", " l2y = l[0].y\n", " else :\n", " l1x = l[i - 1].x\n", " l1y = l[i - 1].y\n", " l2x = l[i].x\n", " l2y = l[i].y\n", " return l1x * l2y - l1y * l2x\n", " \n", " def area(self) :\n", " l = self.lop\n", " shoelacesum = 0\n", " for i in range(len(l)) :\n", " shoelacesum += self.shoelaceterm(i)\n", " return 0.5 * shoelacesum\n", " \n", " def __call__(self) :\n", " return (self.perimeter(), self.area())\n", " \n", " def __str__(self) :\n", " lop = self.lop\n", " retstr = \"\"\n", " for p in lop :\n", " retstr += p.__str__() + \" - \"\n", " return retstr\n", " \n", " def __repr__(self) :\n", " lop = self.lop\n", " lstofstr = []\n", " for p in lop :\n", " lstofstr.append(p.__repr__())\n", " retstr = ', '.join(lstofstr)\n", " retstr = \"Polygon(\" + retstr + \")\"\n", " return retstr\n", " " ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 4 }, { "cell_type": "code", "collapsed": false, "input": [ "if __name__ == '__main__' :\n", " p = Point(0, 0)\n", " q = Point(1, 0)\n", " r = Point(1, 1)\n", " pol = Polygon([p, q, r])\n", " print pol\n", " print pol.perimeter()\n", " print pol.area()\n", " s = Point(0, 1)\n", " pol.add_point(s)\n", " print pol\n", " print pol.perimeter()\n", " print pol.area()\n", " print pol()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "(0, 0) - (1, 0) - (1, 1) - \n", "3.41421356237\n", "0.5\n", "(0, 0) - (1, 0) - (1, 1) - (0, 1) - \n", "4.0\n", "1.0\n", "(4.0, 1.0)\n" ] } ], "prompt_number": 5 } ], "metadata": {} } ] }