a more precise distance algorithm

ravas ravas at outlook.com
Tue May 26 00:59:01 EDT 2015


Oh ya... true >_<

Thanks :D

On Monday, May 25, 2015 at 9:43:47 PM UTC-7, Ian wrote:
> > def distance(A, B):
> >     """
> >     A & B are objects with x and y attributes
> >     :return: the distance between A and B
> >     """
> >     dx = B.x - A.x
> >     dy = B.y - A.y
> >     a = min(dx, dy)
> >     b = max(dx, dy)
> >     if a == 0:
> >         return b
> >     elif b == 0:
> >         return a
> 
> This branch is incorrect because a could be negative.
> 
> You don't need this anyway; the a == 0 branch is only there because of
> the division by a in the else branch.
> 
> >     else:
> >         return a * sqrt(1 + (b / a)**2)
> 
> Same issue; if a is negative then the result will have the wrong sign.




More information about the Python-list mailing list