[pygame] Re: [Tutor] Fastest (x,y) distance calculation

Zak Arntson zak at harlekin-maus.com
Fri Sep 12 16:43:54 EDT 2003


>> I don't know the concept behind using complex numbers and abs to
>> determine
>> distance. How does that work?
>
> You can think of complex numbers as a 2D plane in a lot of respects.
> The imaginary component is the Y axis, and the real component is the X
> axis.  abs(c) is defined as the distance from the origin complex(0, 0j)
> or math.sqrt(c.real**2 + c.imag**2).

I get it. Unfortunately, this still causes a sqrt to be performed (see
below).

> In any case, you're doing more computational work with abs or hypot
> because they are the actual distance, not the distance**2.
>
> -bob

Exactly. I don't need to determine absolute distances, just values good
enough for comparison. Which is why I don't take a sqrt. I tried
math.hypot, but it's slower than the straight multiplication & addition.

---
Zak Arntson
www.harlekin-maus.com - Games - Lots of 'em



More information about the Tutor mailing list