Вопрос

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

Это было полезно?

Решение

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?

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top