Re: [pygame] Pygame 2D vector

2011-07-23 Thread Greg Ewing

DR0ID wrote:

get_length_sqrd just returns the length of a 
vector which does not depend on the quadrant. Using atan2 might be 
simpler


The get_angle() method *does* use atan2. The length check seems
to be just to avoid an exception if the vector happens to be (0, 0).

--
Greg


Re: [pygame] Pygame 2D vector

2011-07-23 Thread Greg Ewing

sam.hack...@sent.com wrote:

It has a get_angle() function, the first line of this checks if 
length_sqrd() == 0, and then returns the angle as 0. This seems wrong to 
me, if the vector is (1,-1), then length_sqrd (1**2 + -1**2) returns 0,


You're fooling yourself with operator precedence. If you're
typing that into the interactive interpreter, you need to
write it as

1**2 + (-1)**2

which has the value 2.

The code given for get_length_sqrd() is

return self.x**2 + self.y**2

which won't suffer from that problem.

--
Greg


Re: [pygame] Pygame 2D vector

2011-07-23 Thread DR0ID

On 23.07.2011 14:23, sam.hack...@sent.com wrote:
Well, when I type "-1**2" into Python, it returns -1. If I run "1**2 + 
-1**2" it returns 0. So, is there a bug in Python then? Running 2.6.6 
on Ubuntu 10.10.
Removing the if statement from get_angle fixed the angle problem for 
me, because of this.

On Sat, 23 Jul 2011 13:56 +0200, "DR0ID"  wrote:


Your mistake basically was that you assumed that  -1 ** 2 == -1 which 
is not.



Hi again


def  get_angle(self):
if  (self.get_length_sqrd()  ==0):
return  0
return  math.degrees(math.atan2(self.y,self.x))



Well the 0 length check is ok, since a 0 length vector does not really 
have an angle. But I guess you see it now.


~DR0ID


Re: [pygame] Pygame 2D vector

2011-07-23 Thread DR0ID

On 23.07.2011 14:26, sam.hack...@sent.com wrote:
Ah, I see the problem, it's doing -(1**2), you need to put "(-1)**2". 
And, that's not a problem for the vector class, it's only because I 
modified my copy a little. OK, nevermind then.

On Sat, 23 Jul 2011 13:23 +0100, sam.hack...@sent.com wrote:
Well, when I type "-1**2" into Python, it returns -1. If I run "1**2 
+ -1**2" it returns 0. So, is there a bug in Python then? Running 
2.6.6 on Ubuntu 10.10.
Removing the if statement from get_angle fixed the angle problem for 
me, because of this.

On Sat, 23 Jul 2011 13:56 +0200, "DR0ID"  wrote:


Your mistake basically was that you assumed that  -1 ** 2 == -1 
which is not.



Hi again

yes, careful with -1**2 == -(1**2) and (-1)**2 , its not the same (and 
the ** operator precedes the - operator hence the -(1**2) if you type in 
-1**2).


To avoid that, you could use a variable:

a = -1

a**2 == 1

Glad you found out. :)

~DR0ID


Re: [pygame] Pygame 2D vector

2011-07-23 Thread sam . hacking
Ah, I see the problem, it's doing -(1**2), you need to put
"(-1)**2". And, that's not a problem for the vector class, it's
only because I modified my copy a little. OK, nevermind then.

On Sat, 23 Jul 2011 13:23 +0100, sam.hack...@sent.com wrote:

Well, when I type "-1**2" into Python, it returns -1. If I run
"1**2 + -1**2" it returns 0. So, is there a bug in Python then?
Running 2.6.6 on Ubuntu 10.10.

Removing the if statement from get_angle fixed the angle problem
for me, because of this.

On Sat, 23 Jul 2011 13:56 +0200, "DR0ID" 
wrote:

Your mistake basically was that you assumed that  -1 ** 2 == -1
which is not.


Re: [pygame] Pygame 2D vector

2011-07-23 Thread sam . hacking
Well, when I type "-1**2" into Python, it returns -1. If I run
"1**2 + -1**2" it returns 0. So, is there a bug in Python then?
Running 2.6.6 on Ubuntu 10.10.

Removing the if statement from get_angle fixed the angle problem
for me, because of this.

On Sat, 23 Jul 2011 13:56 +0200, "DR0ID" 
wrote:

Your mistake basically was that you assumed that  -1 ** 2 == -1
which is not.


Re: [pygame] Pygame 2D vector

2011-07-23 Thread DR0ID

On 23.07.2011 13:03, sam.hack...@sent.com wrote:

On the 2D vector page, http://pygame.org/wiki/2DVectorClass
It has a get_angle() function, the first line of this checks if 
length_sqrd() == 0, and then returns the angle as 0. This seems wrong 
to me, if the vector is (1,-1), then length_sqrd (1**2 + -1**2) 
returns 0, and so the angle is returned as 0. The angle for the vector 
should return -0.78... (in radians) not 0.

I believe the problem is get_length_sqrd(), I think the lines:
x = -self.x if self.x < 0 else self.x
y = -self.y if self.y < 0 else self.y
should be added (and the same to get_length()). I just wanted to check 
if people agree, before I change the page.
I assume this function call is supposed to be faster that math.atan2, 
which is why it is checked first. With these extra lines of code, is 
it still beneficial to have this call in, or just to remove the call 
from get_angle()?

Thanks,
Sam Bull


Hi Sam

I think your math is wrong:

length_sqrd (1**2 + -1**2) returns 0 <--btw this line does not make much 
sense anyway, it should be length_sqrd(1, -1)



this is not true, calculated correctly:

1** 2 + -1 ** 2  ==  1 * 1 + -1 * -1 == 1 + 1 == 2


Your mistake basically was that you assumed that  -1 ** 2 == -1 which is 
not.


And I think the check in which quadrant the point is should be done in 
the get_angle method since get_length_sqrd just returns the length of a 
vector which does not depend on the quadrant. Using atan2 might be 
simpler but could not match with the angles pygame uses (besides of the 
potentially needed degree <-> radians translations).


Hope this helps.

~DR0ID