[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

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



>> 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