Хорошо ... Я не так хорош в математике (даже не закончил нашу версию средней школы) и сейчас очень устал, но мне нужно на орбиту четыре объекта эллиптически вокруг центроида и застряли на некоторое время, но это не так, как будто я ни разу не пробовал или никуда не приходил. Это то, что я придумал до сих пор:Орбитальные объекты вокруг центра тяжести в эллиптической форме, Python
from math import cos,sin,pi,sqrt,atan2
def orbit(p, deg, pivot=(0.32563325, 0.123498),ellipse=(0.5, 0.743992)):
# p = current (x,y)-position of current object,
# Pivot = (x,y)-position of point to orbit around,
# retrieved by centroid((x1,y1),(x2,y2),(x3,y3),(x4,y4))
# Ellipse = (width,height) of ellipse to rotate around
# retrieved by ellipse((x1,y1),(x2,y2),(x3,y3),(x4,y4))
px,py = pivot
if ellipse == (0.0,0.0):
o = polar(p[0]-px,p[1]-py)[0]
xr,yr = 1.0,1.0
else:
ew,eh = ellipse
if ew < eh:
o = eh/2 # Distance to the point most far away from the middle of the ellipse (Outer radius)
xr = ew/eh # Horizontal ratio of ellipse so we can move it back properly into ellipse after performing circular orbit
yr = 1.0 # Verical movement will not be affected because it's on the large axis
else:
o = ew/2
xr = 1.0
yr = eh/ew
x,y = p[0]-px,p[1]-py # Subtract pivot's position (that's the the point I want to orbit around)
d,a = polar(x,y) # Get angle
x,y = rect(o,a+deg) # Move point as far away from middle as it will be as most and make circular orbit around pivot by deg degrees
x,y = x*xr,y*yr # Move points back to elliptic shape by multiplying positions with according ratio <--- I guess it's here something goes wrong
x,y = x+px,y+py # Move point back to original position by adding pivot's positions
return x,y
# Other functions
def centroid(*points):
x,y = izip(*points)
return (sum(x)/len(points), sum(y)/len(points))
def ellipse(*points):
x,y = izip(*points)
xd,yd = max(x)-min(x),max(y)-min(y)
return (xd,yd)
def polar(x,y):
d = sqrt(x**2 + y**2)
a = atan2(y,x) * (180.0/pi)
return d, a
def rect(d,a):
x = d * cos(a*pi/180.0)
y = d * sin(a*pi/180.0)
return x, y
Если я использую Эллипс = (0.0,0.0) и орбиту все, что в «обычном» круг вместо эллиптической формы она работает просто отлично, так что я думаю, что когда я пытаюсь умножить x/y-позицию с отношением ew/eh или eh/ew, я делаю что-то неправильно, но я не могу понять, что на данный момент.
Я немного устал и попытаюсь поспать сейчас и посмотреть, смогу ли я решить это завтра, но здесь будет рассмотрена какая-то просьба.
Что произойдет, когда вы попробуете свой код? Чего вы ожидаете? – lvc
Координаты x и y эллипса могут быть описаны как функция параметра (обычно называемого 't'). Вы можете «вращать» объекты, увеличивая параметр. Я бы уточнил wikipedia и math.stackexchange.com для получения дополнительной информации о параметрических функциях эллипса. –
@ Trasp: Не могли бы вы использовать простой метод двойного круга? – SuperSaiyan