如何在 PyGame 中找到圆上点的坐标? [英] How do I find the coordinates of a point on a circle in PyGame?
问题描述
如果精灵位于 pygame 中点 250,250 处的圆的中心,那么在相对于原始点的任何方向上找到圆的边缘的等式是什么.方程中是否有角度(如 X)?
一般公式为 (x, y) = (cx + r * cos(a), cy + r * sin(a))代码>.
但是,在您的情况下,° 位于顶部,并且角度顺时针增加.因此公式为:
angle_rad = math.radians(angle)pt_x = cpt[0] + 半径 * math.sin(angle_rad)pt_y = cpt[1] - 半径 * math.cos(angle_rad)
或者,您可以使用
导入pygame导入数学pygame.init()窗口 = pygame.display.set_mode((500, 500))font = pygame.font.SysFont(None, 40)时钟 = pygame.time.Clock()cpt = window.get_rect().center角度 = 0半径 = 100运行 = 真运行时:时钟滴答(60)对于 pygame.event.get() 中的事件:如果 event.type == pygame.QUIT:运行 = 错误# 解决方案 1#angle_rad = math.radians(角度)#pt_x = cpt[0] + 半径 * math.sin(angle_rad)#pt_y = cpt[1] - 半径 * math.cos(angle_rad)# 解决方案 2vec = pygame.math.Vector2(0, -radius).rotate(angle)pt_x, pt_y = cpt[0] + vec.x, cpt[0] + vec.y角度 += 1如果角度 >= 360:角度 = 0window.fill((255, 255, 255))pygame.draw.circle(window, (0, 0, 0), cpt, radius, 2)pygame.draw.line(window, (0, 0, 255), cpt, (pt_x, pt_y), 2)pygame.draw.line(window, (0, 0, 255), cpt, (cpt[0], cpt[1]-radius), 2)text = font.render(str(angle), True, (255, 0, 0))window.blit(text, text.get_rect(center = cpt))pygame.display.flip()pygame.quit()出口()
If a sprite is in the centre of a circle at the point 250,250 in pygame what is the equation to find the edge of the circle in any direction relative to the original point. Could the equation have the angle (as X) in the equation?
The general formula is (x, y) = (cx + r * cos(a), cy + r * sin(a))
.
However, in your case° is at the top a nd the angel increase clockwise. Therefore the formual is:
angle_rad = math.radians(angle)
pt_x = cpt[0] + radius * math.sin(angle_rad)
pt_y = cpt[1] - radius * math.cos(angle_rad)
Alternatively you can use the pygame.math
module and pygame.math.Vector2.rotate
:
vec = pygame.math.Vector2(0, -radius).rotate(angle)
pt_x, pt_y = cpt[0] + vec.x, cpt[0] + vec.y
Minimal example:
import pygame
import math
pygame.init()
window = pygame.display.set_mode((500, 500))
font = pygame.font.SysFont(None, 40)
clock = pygame.time.Clock()
cpt = window.get_rect().center
angle = 0
radius = 100
run = True
while run:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
# solution 1
#angle_rad = math.radians(angle)
#pt_x = cpt[0] + radius * math.sin(angle_rad)
#pt_y = cpt[1] - radius * math.cos(angle_rad)
# solution 2
vec = pygame.math.Vector2(0, -radius).rotate(angle)
pt_x, pt_y = cpt[0] + vec.x, cpt[0] + vec.y
angle += 1
if angle >= 360:
angle = 0
window.fill((255, 255, 255))
pygame.draw.circle(window, (0, 0, 0), cpt, radius, 2)
pygame.draw.line(window, (0, 0, 255), cpt, (pt_x, pt_y), 2)
pygame.draw.line(window, (0, 0, 255), cpt, (cpt[0], cpt[1]-radius), 2)
text = font.render(str(angle), True, (255, 0, 0))
window.blit(text, text.get_rect(center = cpt))
pygame.display.flip()
pygame.quit()
exit()
这篇关于如何在 PyGame 中找到圆上点的坐标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!