Point - &quot&quot"Scales the...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
from math import sqrt # needed for computing distances class Point: """A coordinate system with (x, y) standards""" def __init__(self, initialX=0, initialY=0): """Let's the user input values for x and y; will default to 0 otherwise""" self._x = initialX self._y = initialY def getX(self): """Returns X value of Point""" return self._x def setX(self, val): """Sets the X value of Point""" self._x = val def getY(self): """Returns Y value of Point""" return self._y def setY(self, val): """Sets the Y value of Point""" self._y = val def scale(self, factor): """Multiplies the X and Y value by the inputted factor""" self._x *= factor self._y *= factor def distance(self, other): """Uses the distance formula to find the distance between current Point and another Point""" dx = self._x - other._x dy = self._y - other._y return sqrt(dx * dx + dy * dy) # imported from math module def normalize(self):
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: """Scales the Point from the Origin""" mag = self.distance( Point() ) if mag > 0: self.scale(1/mag) def __str__(self): """Allows the user to see the coordinates of Point as <x,y>""" return '<' + str(self._x) + ',' + str(self._y) + '>' def __add__(self, other): """Creates a new point as a result from adding X values and Y values together""" return Point(self._x + other._x, self._y + other._y) def __mul__(self, operand): """Multiplies the coordinates if operand is constant; will use dot product if operand is another Point""" if isinstance(operand, (int,float)): # multiply by constant return Point(self._x * operand, self._y * operand) elif isinstance(operand, Point): # dot product return self._x * operand._x + self._y * operand._y def __rmul__(self, operand): """Multiples the coordinates by operand""" return self * operand...
View Full Document

This note was uploaded on 05/03/2011 for the course CSCI 150 taught by Professor Staff during the Spring '08 term at Saint Louis.

Page1 / 2

Point - &quot&quot"Scales the...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online