Python sin 和 cosine 给出了错误的值

Python sin and cosine giving incorrect values

我有从 2 个点画一条线的代码;一个在屏幕的中间底部,另一个指向鼠标指针。我试图通过不超过我设置的 length 参数来限制该点。代码如下:

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_)

当我 运行 这与我期望的结果不同时,它与我的鼠标不对齐,并且经常在鼠标移动时来回摆动。

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

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

编辑: 正如 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()

红线的长度总是相同的,它表示光标的方向。