Reputation: 83
I need to find the closest distance between these 2 ellipses. Is there any method that I can find that using a code?
Here is my code.
#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, 0))
pygame.draw.circle(screen, (255, 0, 0), [300, 150], 35)
pygame.draw.ellipse(screen, (255, 255, 255), [50, 50, 500, 200], 1)
pygame.draw.ellipse(screen, (255, 0, 255), [200, 100, 200, 100], 1)
pygame.draw.circle(screen, (0, 0, 255), [x1, y1], 15)
pygame.draw.circle(screen, (255, 0, 255), [x2, y2], 5)
pygame.display.flip()
clock.tick(5)
Upvotes: 1
Views: 164
Reputation: 210890
The Euclidean distance from (x1
, y1
) to (x2
, y2
) can be calculated as follows:
import math
dx = x2-x1
dy = y2-y1
distance = math.sqrt(dx*dx + dy*dy)
or
distance = math.hypot(x2-x1, y2-y1)
Upvotes: 2