0

Is me again. Did I mention how much I love you guys? My prof talked me into trying python, so far I hate it, but I decided to give it a try. I have made a simple program, using pygame, that moves few circles around the screen. I have issues with some math problems in it. I gave each circle (x, y) coordinates (the center of circle) and I calculated their shifts(Dx, Dy) on the screen, based on the speed (distance per move) I want them to move. This is what I have done for the move method:

def Move(self, speed):
    Dx = self.qx * (speed * math.sin(math.degrees(90 - Alp)))
    Dy = self.qy * (speed * math.sin(math.degrees(Alp)))
    self.x += Dx
    self.y += Dy
    print "D = ", math.sqrt(Dx * Dx + Dy * Dy)

the problem: I calculate Dx and Dy based on speed using Pythagorahs theorem, and then, calculating D (actaully speed) in print statement using the same theorem, I should Have a result equal to speed that is inputed. But, the result I get is:

    D =  9.15180313227 (speed  = 10)

The result varies and is not always the same (I have test method with random values), but it's always close and wrong. What am I missing?

NOTE: Ignore self.qy and self.qx, they are used to properly determine the direction, their value is either 1 or -1

Antonio Teh Sumtin
  • 498
  • 4
  • 12
  • 26
  • 2
    Dono if this would solve the problem. But you should use `math.radians` to convert from degrees to radians. `math.sin` expects the angle to be in radians. – M4rtini Mar 12 '14 at 17:04
  • This doesn't answer your question, but note there's also a `math.hypot()` function. – martineau Mar 12 '14 at 17:06
  • @M4rtini, that was the problem, thank yoiu for answering as well :) martineau, I'm not sure where you wanted to go with this? – Antonio Teh Sumtin Mar 12 '14 at 17:07
  • 1
    Instead of using `math.sin(math.radians(90 - Alp))` you can as well use the more readable `math.cos(math.radians(Alp))`. – Lutz Lehmann Mar 13 '14 at 08:31
  • @Lutzl Thank you, it did not occur to me at that time, that this is the same, I was too focused on basic problem to see this :D – Antonio Teh Sumtin Mar 17 '14 at 08:33

1 Answers1

3

Looking to your code, and precisely where you call math.degrees(90-Alp), it seems to me that you understood math.degrees in the wrong way:

>>> math.degrees(2)
114.59 (...)

This function converts radians to degrees, not the opposite. How does it perform if you use math.radians instead?

Joël
  • 2,723
  • 18
  • 36