[Tutor] line class
Marc Tompkins
marc.tompkins at gmail.com
Wed Jul 9 01:00:31 CEST 2008
On Tue, Jul 8, 2008 at 3:29 PM, Christopher Spears <cspears2002 at yahoo.com>
wrote:
> I have been reading everyone's comments on my line class. I have decided
> to implement some of the suggestions. Someone suggested that I create a
> Point.__cmp__ method. Here is what I have so far:
>
> def __cmp__(self, other):
> if self.x == other.x and self.y == other.y:
> return 0
> elif self.x < other.x and self.y < other.y:
> return -1
> elif self.x > other.x and self.y > other.y:
> return 1
>
> Figuring out the results for the above situations was easy. However, what
> should I do with the following situations:
> self.x > other.x and self.y < other.y
> self.x < other.x and self.y > other.y
>
Sorry to jump in late - I missed the first part of the discussion - but what
do you want to achieve with a Point.__cmp__ method?
Is it just to determine whether two points are identical - in which case I'd
write it like so:
def __cmp__(self, other):
if (self.x == other.x) and (self.y == other.y):
return True
else:
return False
Or are you trying to determine the slope of the line between two points, in
which case I'd write it like this:
def __cmp__(self, other):
if self.x == other.x:
return False # points are identical, or one above the other - slope
is undefined
else:
return (self.y - other.y) / (self.x - other.x) # rise over run
or... or... or... First decide what you want out of it, then write the
function/method to give you the result you want.
--
www.fsrtechnologies.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20080708/b0eafb16/attachment.htm>
More information about the Tutor
mailing list