微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

测量最近距离 - pygame

如何解决测量最近距离 - pygame

我需要找到这两个椭圆之间最近的距离。 有什么方法可以使用代码找到吗?

谢谢。

enter image description here

这是我的代码

    #Elliptical orbits

import pygame
import math
import sys

pygame.init()
screen = pygame.display.set_mode((1000,300))
pygame.display.set_caption("Elliptical orbit")
clock = pygame.time.Clock()

while(True):
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()

    xRadius = 250
    yRadius = 100
    x2Radius = 100
    y2Radius = 50

    for degree in range(0,360,10):
        x1 = int(math.cos(degree * 2 * math.pi / 360) * xRadius) + 300
        y1 = int(math.sin(degree * 2 * math.pi / 360) * yRadius) + 150
        x2 = int(math.cos(degree * 2 * math.pi / 360) * x2Radius) + 300
        y2 = int(math.sin(degree * 2 * math.pi / 360) * y2Radius) + 150
        screen.fill((0,0))
        pygame.draw.circle(screen,(255,0),[300,150],35)
        pygame.draw.ellipse(screen,255,255),[50,50,500,200],1)
        pygame.draw.ellipse(screen,[200,100,200,100],1)
        pygame.draw.circle(screen,(0,[x1,y1],15)
        pygame.draw.circle(screen,[x2,y2],5)

        pygame.display.flip()
        clock.tick(5)

解决方法

从 (x1,y1) 到 (x2,y2) 的 Euclidean distance 可以计算如下:

import math

dx = x2-x1
dy = y2-y1
distance = math.sqrt(dx*dx + dy*dy)

distance = math.hypot(x2-x1,y2-y1)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。