#!/usr/bin/env python # # a simple point class example # # written by e.bonakdarian december 2010 # from math import sqrt class Point(): # initialize point with x and y val def __init__(self, xval, yval): self.x = xval self.y = yval def get_x(self): # return x coordinate return self.x def get_y(self): # return y coordinate return self.y def set_x(self, x): # set x coordinate self.x = x def set_y(self, y): # set y coordinate self.y = y def set_xy(self, x, y): # set y coordinate self.x = x self.y = y def distance(self, other_x, other_y): # compute the distance between this point # and the other point on the plane x_diff = other_x - self.x y_diff = other_y - self.y dist = sqrt((x_diff**2) + (y_diff**2)) return dist def p_distance(self, other_point): # compute the distance between this point # and the other point on the plane x_diff = other_point.get_x() - self.x y_diff = other_point.get_y() - self.y dist = sqrt((x_diff**2) + (y_diff**2)) return dist def show(self): print self.x, self.y, if __name__ == '__main__': pointA = Point(0, 0) pointB = Point(1, 1) print 'Point A: ', pointA.show() print print 'Point B: ', pointB.show() print distance = pointA.distance(pointB.get_x(), pointB.get_y()) print 'distance between point A and point B is %.2f' % distance distance = pointB.distance(pointA.get_x(), pointA.get_y()) print 'distance between point B and point A is %.2f' % distance print pointA.set_xy(10, 10) print 'Point A: ', pointA.show() print print 'Point B: ', pointB.show() print print 'distance between point B and point A is %.2f' % pointB.p_distance(pointA)