Next / Previous / Contents / Shipman's homepage

7. class Pt: One point in Cartesian space

An instance of this class represents one homogeneous coordinate. Here is the class interface.

homcoord.py
# - - - - -   c l a s s   P t

class Pt(object):
    '''Represents a homogeneous coordinate in 2-space.

      Exports:
        Pt(*coords):
          [ coords is a 2-sequence or a 1-sequence containing a
            2-sequence ->
              return a new Pt instance representing those two
              values as x and y, respectively
            coords is a 1-sequence containing a 3-sequence ->
              return a new Pt instance representing those values
              as x, y, and w, respectively ]
        .xy():
          [ return a 2-tuple with the homogenized x and y values ]
        .x():    [ return the homogenized x coordinate ]
        .y():    [ return the homogenized y coordinate ]
        .dist(other):
          [ other is a Pt instance ->
              return the distance between self and other ]
        .bearing(p):
          [ p is a Pt instance ->
              return the Cartesian angle in radians from self to p ]
        .radial(d, bearing):
          [ (d is a distance) and (bearing is an angle in radians) ->
              return the location at that distance and bearing as
              a Pt instance ]
        .toPolar():
          [ return self in polar coordinates as a Polar instance ]
        .__str__():  [ return self as a string ]
        .__add__(self, other):
          [ other is a Pt instance ->
              return a new Pt instance whose coordinates are the
              sum of self's and other's ]
        .__sub__(self, other):
          [ other is a Pt instance ->
              return a new Pt instance whose coordinates are the
              self's minus other's ]
        .__cmp__(self, other):
          [ if self and other are the same point ->
              return 0
            else -> return a nonzero value ]

So much for the externals. Internally we store the point as a 3-element numpy vector in which the W component is third.

homcoord.py
      State/Invariants:
        .v     [ a numpy 3-element vector [x, y, W] ]
    '''