Python sin和cosine给出不正确的值

时间:2017-12-05 00:38:15

标签: python math pygame trigonometry

我的代码从2分中划出一条线;一个在屏幕的中间底部,另一个在鼠标指针上。我试图通过不超过我设置的长度参数来约束该点。下面是代码:

import pygame
import Resources as r
import math as m

pygame.init()

class Segment():



    def __init__(self, _screen, _id, _start_pos, _length):
        self.screen = _screen
        self.id = _id
        self.start_pos = _start_pos
        self.length = _length

    def update(self):
        if self.id == 1:
            mouse_pos = pygame.mouse.get_pos()


            self.angle = m.atan2(mouse_pos[1]-self.start_pos[1],mouse_pos[0]-self.start_pos[0])
            self.a = self.start_pos
            self.b = (m.cos(self.angle)*self.length, m.sin(self.angle)*self.length)


            self.draw_line(self.a, self.b, r.black, 4)




    def draw_line(self, start, end, color, width):
        if self.id == 1:
            pygame.draw.line(self.screen, color, start, end, width)

    def get_data(self):
        return (self.start_pos, self.end_)

当我按照我的预期运行时,我看到了非常不同的结果,它与我的鼠标不对齐,并且通常只是在鼠标移动时来回摆动。

2 个答案:

答案 0 :(得分:2)

self.b根据原点0,0计算,而不是self.start_pos

self.a中的坐标添加到self.b

答案 1 :(得分:1)

编辑:正如skrx在评论中指出的那样:鼠标位置无需转换为Vector2,因为tuple-Vector2提供了Vector2

您可以对python.math.Vector2

执行相同的操作

屏幕中间底部的start

screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
screen_rect = screen.get_rect()

start = pygame.math.Vector2(screen_rect.centerx, screen_rect.bottom)

end点使用鼠标位置和长度

#mouse = pygame.math.Vector2(pygame.mouse.get_pos()) # edited
mouse = pygame.mouse.get_pos()
end = start + (mouse - start).normalize() * length

现在你可以画

pygame.draw.line(screen, (255,0,0), start, end)

工作示例

import pygame

# === CONSTANS === (UPPER_CASE names)

BLACK = (  0,   0,   0)
WHITE = (255, 255, 255)

RED   = (255,   0,   0)
GREEN = (  0, 255,   0)
BLUE  = (  0,   0, 255)

SCREEN_WIDTH  = 600
SCREEN_HEIGHT = 400

# === MAIN === (lower_case names)

# --- init ---

pygame.init()

screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
screen_rect = screen.get_rect()

# --- objects ---

start = pygame.math.Vector2(screen_rect.centerx, screen_rect.bottom)
end = start
length = 150

# --- mainloop ---

clock = pygame.time.Clock()
is_running = True

while is_running:

    # --- events ---

    for event in pygame.event.get():

        # --- global events ---

        if event.type == pygame.QUIT:
            is_running = False
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE:
                is_running = False
        elif event.type == pygame.MOUSEMOTION:
            #mouse = pygame.math.Vector2(pygame.mouse.get_pos()) # edited
            mouse = pygame.mouse.get_pos()
            end = start + (mouse - start).normalize() * length

        # --- objects events ---

            # empty

    # --- updates ---

        # empty

    # --- draws ---

    screen.fill(BLACK)

    pygame.draw.line(screen, RED, start, end)

    pygame.display.update()

    # --- FPS ---

    clock.tick(25)

# --- the end ---

pygame.quit()

红线始终具有相同的长度,并显示光标的方向。 enter image description here