2012-06-03 6 views
0

좋아요 ... 저는 수학이별로 좋지 않습니다 (우리 학교 버전을 끝내지도 못했음). 그리고 지금은 피곤하지만 나는 궤도에 올 필요가 있습니다. 4 개의 객체가 중심 주위로 타원형으로 갇혀 있지만 잠시 멈추지 않았습니다. 이것은 내가 지금까지 가지고 올 한 것입니다 : 내가 타원 사용 = (0.0, 0.0) 대신 그냥 잘 작동 타원형의 "보통"원에 모든 궤도 경우타원형 모양의 중심 주위에 4 개의 물체를 도는 것, 파이썬

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 

, 그래서 그것이 x/y 위치에 비율 ew/eh 또는 eh/ew를 곱하려고 시도했을 때 나는 잘못된 것을하지만, 지금은 무엇을 생각할 수 없는지 추측합니다.

나는 약간 피곤해서 잠을 잘 자고 내일 해결할 수 있는지 알아볼 것이다. 그러나 약간의 도발은 정말로 여기에 호소 될 것이다.

+0

코드를 시도하면 어떻게됩니까? 무슨 일이 일어날 것으로 예상됩니까? – lvc

+1

타원의 x 및 y 좌표는 매개 변수 (일반적으로 't'라고 함)의 함수로 설명 할 수 있습니다. 매개 변수를 증가시켜 객체를 "회전"시킬 수 있습니다. 타원의 매개 변수 함수에 대한 자세한 내용은 wikipedia 및 math.stackexchange.com을 확인하십시오. –

+0

@Trasp : 간단한 이중 원형 기술을 사용할 수 없습니까? – SuperSaiyan

답변

1

각 타원에 대한 수학을 캡슐화하고 샘플 사용법을 보여줍니다. 원하는 출력을위한 코드를 추가해야합니다.

from math import sin, cos, pi 

class Ellipse(object): 
    @classmethod 
    def fromBox(cls, p1=(-1.,-1.), p2=(1.,1.), period=1., offs=0., clockwise=False): 
     """ 
     Construct an Ellipse from an axis-aligned bounding box 
     p1, p2  diagonally-opposed corners of the bounding box 
     period  time for a complete orbit 
     offs  offset into initial rotation 
     clockwise direction of rotation 
     """ 
     x1,y1 = p1 
     x2,y2 = p2 
     # find center point 
     cx, cy = (x1 + x2)*0.5, (y1 + y2)*0.5 
     # find major and minor semi-axes and corresponding theta 
     a,b = abs(x2 - x1)*0.5, abs(y2 - y1)*0.5 
     if a >= b: 
      theta = 0.0 
     else: 
      a,b = b,a 
      theta = pi/2 
     # return an Ellipse object 
     return cls(cx, cy, a, b, theta, period, offs, clockwise) 

    def __init__(self, x=0., y=0., a=1., b=1., theta=0., period=1., offs=0., clockwise=False): 
     """ 
     Create an ellipse 
     x,y   center point 
     a   semi-major axis 
     b   semi-minor axis 
     theta  major axis inclination (in radians) 
     period  time for a complete orbit 
     offs  offset into initial rotation 
     clockwise direction of rotation 
     """ 
     self.x = x 
     self.y = y 
     self.period = -period if clockwise else period 
     self._freq = self.period/(2. * pi) 
     self.offs = offs 

     s_th = sin(theta) 
     c_th = cos(theta) 
     self._ast = a * s_th 
     self._act = a * c_th 
     self._bst = b * s_th 
     self._bct = b * c_th 

    def at(self, t): 
     """ 
     Evaluate the ellipse at time t 
     """ 
     _t = (t - self.offs) * self._freq 
     st = sin(_t) 
     ct = cos(_t) 
     return self.x + self._act*ct - self._bst*st, self.y + self._act*st + self._bst*ct 

def main(): 
    a = Ellipse.fromBox((-0.67436675, -0.376502), (1.32563325, 0.623498)) 
    b = Ellipse(0.32563325, 0.123498, 0.9, 0.6, pi/6) 
    c = Ellipse(0.32563325, 0.123498, 1.1, 0.5, pi/4) 
    d = Ellipse(0.32563325, 0.123498, 1.0, 0.5, pi/2) 

    t_step = 0.03 
    for t in xrange(200): 
     drawCircle(a.at(t*t_step), "red") 
     drawCircle(b.at(t*t_step), "blue") 
     drawCircle(c.at(t*t_step), "green") 
     drawCircle(d.at(t*t_step), "yellow") 

if __name__=="__main__": 
    main()