[Tutor] line class
Paul McGuire
ptmcg at austin.rr.com
Tue Jul 8 17:15:09 CEST 2008
>
> > def __init__(self,p1,p2):
> > self.p1 = p1
> > self.p2 = p2
> >
> > And since a line should not have zero length (although you might argue
> > with that!) you could also check if
> > p1==p2
>
> In this case he should define Point.__cmp__() so the comparison is by
value rather than identity.
>
> Kent
>
I'd also suggest that in defining __cmp__ that the OP's test for equality
should comprehend some kind of epsilon value, if the x-y coordinates are
going to be floating point numbers. It is very easy in graphical apps to
get tripped up by floating point round-off. Here is a contrived but simple
demonstration:
>>> x = 1
>>> for i in range(10): x -= 0.1
...
>>> x
1.3877787807814457e-016
>>> x == 0
False
So instead of "if p1.x == p2.x" you should use "if abs(p1.x-p2.x) <
EPSILON", and define EPSILON to something in the range of 1e-13 or so.
-- Paul
More information about the Tutor
mailing list