如何在pygame中将三角形旋转到某个角度? [英] How to rotate a triangle to a certain angle in pygame?
问题描述
我需要围绕屏幕中心旋转一个三角形(不是图像).我看到其他人回答了这个问题,但三角形不能向上.
我曾尝试使用其他人的功能,但他们认为只能部分工作,就像我上面提到的功能一样.
导入pygamedisp=pygame.display.set_mode((200,200))导入数学defrotate_triange(mouse_pos,triangle_pos):#代码在这里导入时间为真:时间.sleep(1)pygame.Surface.fill(disp,(255,255,255))中心 = (100,100)半径 = 10mouse_position = pygame.mouse.get_pos()对于 pygame.event.get() 中的事件:经过点 = rotate_triangle((100,100),mouse_position)pygame.draw.polygon(disp,(0,0,0),points)pygame.display.update()
在 pygame 中,二维向量算法在
导入pygame导入数学defrotate_triangle(center, scale, mouse_pos):vMouse = pygame.math.Vector2(mouse_pos)vCenter = pygame.math.Vector2(中心)角度 = pygame.math.Vector2().angle_to(vMouse - vCenter)点数 = [(-0.5, -0.866), (-0.5, 0.866), (2.0, 0.0)]旋转点 = [pygame.math.Vector2(p).rotate(angle) for p in points]triangle_points = [(vCenter + p*scale) for p in rotation_point]返回三角形点disp=pygame.display.set_mode((200,200))运行 = 真运行时:对于 pygame.event.get() 中的事件:如果 event.type == pygame.QUIT:运行 = 错误mouse_position = pygame.mouse.get_pos()点 = rotate_triangle((100, 100), 10, mouse_position)pygame.Surface.fill(disp, (255,255,255))pygame.draw.polygon(显示,(0,0,0),点)pygame.display.update()
<小时>
算法的一个版本,不使用pygame.math.Vector2
,如下所示:
defrotate_triangle(center, scale, mouse_pos):dx = mouse_pos[0] - 中心 [0]dy = mouse_pos[1] - 中心 [1]len = math.sqrt(dx*dx + dy*dy)dx, dy = (dx*scale/len, dy*scale/len) 如果 len >0 其他 (1, 0)分 = [(-0.5, -0.866), (-0.5, 0.866), (2.0, 0.0)]pts = [(center[0] + p[0]*dx + p[1]*dy, center[1] + p[0]*dy - p[1]*dx) for p in pts]返回点数
注意这个版本可能更快.与 .angle_to()
和 math 可能使用的
分别是 math.atan2
相比,它需要一个 math.sqrt
操作.sinmath.cos
可能被前一种算法的 .rotate()
使用.结果坐标相同.
I need to rotate a triangle (Not an image) around at the center of the screen. I have seen other people answer this question, but the triangle could not point upwards.
I have tried to use other peoples functions, but they see to only work partly, like the function I mentioned above.
import pygame
disp=pygame.display.set_mode((200,200))
import math
def rotate_triange(mouse_pos,triangle_pos):
#The code here
import time
while True:
time.sleep(1)
pygame.Surface.fill(disp,(255,255,255))
center = (100,100)
radius = 10
mouse_position = pygame.mouse.get_pos()
for event in pygame.event.get():
pass
points = rotate_triangle((100,100),mouse_position)
pygame.draw.polygon(disp,(0,0,0),points)
pygame.display.update()
In pygame 2 dimensional vector arithmetic is implemented in pygame.math.Vector2
.
Define a Vector2
object for the mouse position and the center of the triangle. Calculate the angle of vector form the center point to the mouse position (.angle_to()
):
vMouse = pygame.math.Vector2(mouse_pos)
vCenter = pygame.math.Vector2(center)
angle = pygame.math.Vector2().angle_to(vMouse - vCenter)
Define the 3 points of the triangle around the (0, 0) and rotate them by the angle (.rotate()
)
points = [(-0.5, -0.866), (-0.5, 0.866), (2.0, 0.0)]
rotated_point = [pygame.math.Vector2(p).rotate(angle) for p in points]
To calculate the final points, the points have to b scaled and translated by the center of the triangle:
triangle_points = [(vCenter + p*scale) for p in rotated_point]
See the example:
import pygame
import math
def rotate_triangle(center, scale, mouse_pos):
vMouse = pygame.math.Vector2(mouse_pos)
vCenter = pygame.math.Vector2(center)
angle = pygame.math.Vector2().angle_to(vMouse - vCenter)
points = [(-0.5, -0.866), (-0.5, 0.866), (2.0, 0.0)]
rotated_point = [pygame.math.Vector2(p).rotate(angle) for p in points]
triangle_points = [(vCenter + p*scale) for p in rotated_point]
return triangle_points
disp=pygame.display.set_mode((200,200))
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
mouse_position = pygame.mouse.get_pos()
points = rotate_triangle((100, 100), 10, mouse_position)
pygame.Surface.fill(disp, (255,255,255))
pygame.draw.polygon(disp, (0,0,0), points)
pygame.display.update()
A version of the algorithm, without the use of pygame.math.Vector2
, looks as follows:
def rotate_triangle(center, scale, mouse_pos):
dx = mouse_pos[0] - center[0]
dy = mouse_pos[1] - center[1]
len = math.sqrt(dx*dx + dy*dy)
dx, dy = (dx*scale/len, dy*scale/len) if len > 0 else (1, 0)
pts = [(-0.5, -0.866), (-0.5, 0.866), (2.0, 0.0)]
pts = [(center[0] + p[0]*dx + p[1]*dy, center[1] + p[0]*dy - p[1]*dx) for p in pts]
return pts
Note this version is probably faster. It needs a math.sqrt
operation, in compare to math.atan2
which is probably used by .angle_to()
and math.sin
respectively math.cos
which is probably used by .rotate()
, of the former algorithm.
The result coordinates are the same.
这篇关于如何在pygame中将三角形旋转到某个角度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!