# a2.py # Steve Marschner (SRM2) and Lillian Lee (LJL2) # Feb 24, 2014 class Point2(object): """A class representing points in a 2D plane.""" def __init__(self,x,y): self.x = float(x) self.y = float(y) def midpoint(p,q): """Return: a new point at the midpoint of the points p and q.""" mx = (p.x + q.x) / 2 my = (p.y + q.y) / 2 mp = Point2(mx, my) return mp def move_to_mid(p, q): """Move p to the midpoint of p and q.""" p = midpoint(p,q) def min_max(p, q): """Move p to bottom left and q to upper right of the rectangle they define.""" if q.x < p.x: t = p.x p.x = q.x q.x = t if q.y < p.y: t = p.y p.y = q.y q.y = t def dist_sqr(p,q): """The squared distance between the points p and q.""" dx = p.x - q.x dy = p.y - q.y return dx**2 + dy**2 def half_dist_sqr(p,q): """The square of half the distance from p to q.""" mp = midpoint(p,q) return dist_sqr(mp, p) a = Point2(1, 2) b = Point2(5, 0) c = midpoint(a, b) min_max(c, a) h1 = half_dist_sqr(a, b) move_to_mid(a, b) h2 = dist_sqr(a, b)