在Python中正确使用cos()和sin()

时间:2017-03-14 23:54:07

标签: python-3.x

我试图将一种名为Seed7的语言编写的代码列表转换为Python。除了一部分,我已经准备好了β阶段。这是Seed7列表的摘录:

x1 := flt(column) + 0.5;
y1 := flt(row) + 0.5;
angle := (course - 1.0) * 0.785398;
delta_x := cos(angle);
delta_y := -sin(angle);
inquad := TRUE;
blocked := FALSE;
number := 1;
while number <= distance do
  y1 := y1 + delta_y;
  x1 := x1 + delta_x;
  row := trunc(y1);
  column := trunc(x1);
  if column < 1 or column > 8 or row < 1 or row > 8 then
     inquad := FALSE;
     number := distance;
  else
    if sect[row][column] <> 1 then  (* Object blocking move *)
       blocked := TRUE;
       number := distance;
    end if;
  end if;
  incr(number);
end while;

除了我不理解函数cos()和sin()在Seed7中如何工作之外,这一切都是有道理的。

手册说:

const func float:sin(在float:x中)

Compute the sine of x, where x is given in radians.

Returns:
    the trigonometric sine of an angle.

但我不能在Python中使用等效的东西。

这个问题纯粹是由于我没有正确理解Python而导致的问题(并且数学上并没有那么好),所以我来这里问一个了解这些事情的人。

使上述代码在Python中运行需要什么代码?救命!!! : - )

非常感谢,

约瑟夫。

编辑:我认为问题是incr()函数。基本上,在这个游戏中可能会小于1.来自帮助文件:

writeln("Warp - One warp moves you the width of a quadrant.  A warp of .5 will move you");
    writeln("halfway through a quadrant.  Moving diagonally across a quadrant to the next");
    writeln("will require 1.414 warps.  Warp 3 will move you 3 quadrants providing nothing");
    writeln("in your present quadrant blocks your exit.  Once you leave the quadrant that");
    writeln("you were in, you will enter hyperspace; coming out of hyperspace will place you");
    writeln("randomly in the new quadrant.  Klingons in a given quadrant will fire at you");
    writeln("whenever you leave, enter, or move within the quadrant.  Entering a course or");
    writeln("warp of zero can be used to return to the command mode.")

我的代码如下所示:

x1 = float(column) + 0.5
    y1 = float(row) + 0.5
    angle = (course - 1.0) * 0.785398
    deltaX = math.cos(angle)
    deltaY = -math.sin(angle)

    inQuad = True
    blocked = False
    num = 1
    while num <= distance:
        y1 += deltaY
        x1 += deltaX
        row = int(round(y1))
        column = int(round(x1))
        if column < 0 or column > 7 or row < 0 or row > 7:
            inQuad = False
            num = distance
        else:
            if sect[row][column] != 1:
                blocked = True
                num = distance

        num += 1

事情是我在那里使用num + = 1,而不是incr,但我不理解incr。正如我所说的那样,我已经离开游戏有点长了,而且某些事情真的让我感到困惑。

任何帮助照亮光线的人都会受到赞赏。

约瑟夫。

1 个答案:

答案 0 :(得分:0)

猜测:

如果Seed7需要弧度,则在应用sin或cos之前,您可能需要将度数转换为弧度:

import math
math.sin(math.radians(1))