Python的正弦和余弦给出不正确的值 [英] Python sin and cosine giving incorrect values
本文介绍了Python的正弦和余弦给出不正确的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有从2点画一条线的代码;一个位于屏幕中间底部,另一个指向鼠标指针.我试图通过不超过我设置的 length 参数来约束该点.这是代码:
I have code that draws a line from 2 points; One on the middle bottom of the screen, and the other to the mouse pointer. I am trying to constrain the point by not exceeding a length parameter that I set. Heres the code:
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_)
运行该程序时,我看到的结果与预期的非常不同,它与鼠标不对齐,并且通常在移动鼠标时来回摆动.
I am seeing very different results when I run this that I would expect, it doesnt line up with my mouse and often just oscillates back and forth when the mouse is moved.
推荐答案
self.b
是根据原点0、0而不是self.start_pos
计算的.
self.b
is calculated based on origin 0, 0, not self.start_pos
.
将self.a
中的坐标添加到self.b
.
这篇关于Python的正弦和余弦给出不正确的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文