如何在 PyGame 中找到圆上点的坐标?

Jac*_*ton 2 python math pygame

如果一个精灵位于 pygame 中 250,250 点的圆心,那么找到相对于原始点的任意方向的圆边缘的方程是什么?方程中可以有角度(如X)吗?

Rab*_*d76 7

通用公式为(x, y) = (cx + r * cos(a), cy + r * sin(a))

\n

但是,在您的情况下 \xc2\xb0 位于顶部,角度顺时针增加。因此公式为:

\n
angle_rad = math.radians(angle)\npt_x = cpt[0] + radius * math.sin(angle_rad)\npt_y = cpt[1] - radius * math.cos(angle_rad)  \n
Run Code Online (Sandbox Code Playgroud)\n

或者,您可以使用pygame.math模块和pygame.math.Vector2.rotate

\n
vec = pygame.math.Vector2(0, -radius).rotate(angle)\npt_x, pt_y = cpt[0] + vec.x, cpt[1] + vec.y\n
Run Code Online (Sandbox Code Playgroud)\n
\n

最小的例子:

\n

\n
import pygame\nimport math\n\npygame.init()\nwindow = pygame.display.set_mode((500, 500))\nfont = pygame.font.SysFont(None, 40)\nclock = pygame.time.Clock()\ncpt = window.get_rect().center\nangle = 0\nradius = 100\n\nrun = True\nwhile run:\n    clock.tick(60)\n    for event in pygame.event.get():\n        if event.type == pygame.QUIT:\n            run = False  \n\n    # solution 1\n    #angle_rad = math.radians(angle)\n    #pt_x = cpt[0] + radius * math.sin(angle_rad)\n    #pt_y = cpt[1] - radius * math.cos(angle_rad)    \n    \n    # solution 2\n    vec = pygame.math.Vector2(0, -radius).rotate(angle)\n    pt_x, pt_y = cpt[0] + vec.x, cpt[1] + vec.y\n    \n    angle += 1     \n    if angle >= 360:\n        angle = 0\n\n    window.fill((255, 255, 255))\n    pygame.draw.circle(window, (0, 0, 0), cpt, radius, 2)\n    pygame.draw.line(window, (0, 0, 255), cpt, (pt_x, pt_y), 2)\n    pygame.draw.line(window, (0, 0, 255), cpt, (cpt[0], cpt[1]-radius), 2)\n    text = font.render(str(angle), True, (255, 0, 0))\n    window.blit(text, text.get_rect(center = cpt))\n    pygame.display.flip()\n\npygame.quit()\nexit()\n
Run Code Online (Sandbox Code Playgroud)\n