Пример #1
1
def line(pos=(0,0), np=2, rotate=0.0, scale=1.0, xscale=1.0, yscale=1.0,
           thickness=None, start=(0,0), end=(0,1), path=False):
        v = vis.vector((end[0]-start[0]), (end[1]-start[1]))
        if thickness is None:
            thickness = 0.01*vis.mag(v)
        dv = thickness*vis.norm(vis.vector(0,0,1).cross(v))
        dx = dv.x
        dy = dv.y
        cp = [] # outer line
        cpi = [] # inner line
        vline = (vis.vector(end)-vis.vector(start)).norm()
        mline = vis.mag(vis.vector(end)-vis.vector(start))
        for i in range(np):
            x = start[0] + (vline*i)[0]/float(np-1)*mline
            y = start[1] + (vline*i)[1]/float(np-1)*mline
            cp.append( (x+pos[0],y+pos[1]) )
            cpi.append( (x+pos[0]+dx,y+pos[1]+dy) )
        if not path:
                cpi.reverse()
                for p in cpi:
                    cp.append(p)
                cp.append(cp[0])
        if rotate != 0.0: cp = rotatecp(cp, pos, rotate)
        if scale != 1.0: xscale = yscale = scale
        pp = Polygon(cp)
        if xscale != 1.0 or yscale != 1.0: pp.scale(xscale,yscale)
        if not path:
                return pp
        else:
                return [cp]
Пример #2
0
def convert(pos=(0, 0, 0), up=(0, 1, 0), points=None, closed=True):
    pos = vector(pos)
    up = norm(vector(up))
    up0 = vector(0, 1, 0)
    angle = acos(up.dot(up0))
    reorient = (angle > 0.0)
    axis = up0.cross(up)
    pts = []
    for pt in points:
        newpt = vector(pt[0], 0, -pt[1])
        if reorient: newpt = newpt.rotate(angle=angle, axis=axis)
        pts.append(pos + newpt)
    if closed and (pts[-1] != pts[0]): pts.append(pts[0])
    return pts
Пример #3
0
def convert(pos=(0,0,0), up=(0,1,0), points=None, closed=True):
        pos = vector(pos)
        up = norm(vector(up))
        up0 = vector(0,1,0)
        angle = acos(up.dot(up0))
        reorient = (angle > 0.0)
        axis = up0.cross(up)
        pts = []
        for pt in points:
            newpt = vector(pt[0],0,-pt[1])
            if reorient: newpt = newpt.rotate(angle=angle, axis=axis)
            pts.append(pos+newpt)
        if closed and (pts[-1] != pts[0]): pts.append(pts[0])
        return pts
Пример #4
0
def ship(s, v, a, dt, spaceship, predraw):
    dst = vector(0, 0, 0) - s  # computing spaceship - Sun distance vector
    gravity_acc = (u / (mag2(dst))) * norm(dst)  # computing gravity force

    v += (gravity_acc + a) * dt  # numerical integration of velocity
    s += v * dt  # numerical integration of position

    spaceship.pos = s  # assigning new position to the spaceship
    spaceship.trail.append(pos=s, retain=2000)  # appending spaceship trail with new position

    # recovers the spaceship to the initial position in case of crashing into the Sun or going out of range:

    if star_radius <= mag(s) or mag(s) <= sun_radius:
        spaceship.pos = s0
        spaceship.trail.pos = []    # clear the trail
        s, v, a = vector(s0.astuple()), vector(v0.astuple()), vector(a0.astuple())  # assign initial values
        predraw = False  # redraw the orbital prediction

    return s, v, a, predraw
Пример #5
0
def ship(s, v, a, dt, spaceship, predraw):
    dst = vector(0, 0, 0) - s  # computing spaceship - Sun distance vector
    gravity_acc = (u / (mag2(dst))) * norm(dst)  # computing gravity force

    v += (gravity_acc + a) * dt  # numerical integration of velocity
    s += v * dt  # numerical integration of position

    spaceship.pos = s  # assigning new position to the spaceship
    spaceship.trail.append(
        pos=s, retain=2000)  # appending spaceship trail with new position

    # recovers the spaceship to the initial position in case of crashing into the Sun or going out of range:

    if star_radius <= mag(s) or mag(s) <= sun_radius:
        spaceship.pos = s0
        spaceship.trail.pos = []  # clear the trail
        s, v, a = vector(s0.astuple()), vector(v0.astuple()), vector(
            a0.astuple())  # assign initial values
        predraw = False  # redraw the orbital prediction

    return s, v, a, predraw
Пример #6
0
def key_check(ship_mode, s, v, a, dt, sw_lbl, lbl_off, predraw, earthpos, t, ship):
    if scene.kb.keys:  # check if there are any keys pressed in the queue

        key = scene.kb.getkey()  # retrieve the pressed key

        # change simulation speed: (steps are small on purpose; user has to press and hold for smooth transition)
        if key == 'q' and 1 <= dt < 125:
            dt += 2
        elif key == 'q' and dt <= 0.95:
            dt += 0.05
        elif key == 'a' and dt > 1:
            dt -= 2
        elif key == 'a' and 0.2 < dt <= 1:
            dt -= 0.05

        elif key == 'esc':
            Exit()  # exit the program

        elif key == 'p':
            lbl.visible = True  # show pause label
            lbl.pos = scene.center  # center the label on the screen
            scene.kb.getkey()  # wait for key input
            lbl.visible = False  # hide pause label

        elif key == 'x':
            if scene.stereo == 'nostereo':
                scene.stereo = 'redcyan'  # turn on 3D rendering mode (red-cyan)
            elif scene.stereo == 'redcyan':
                scene.stereo = 'crosseyed'  # turn on 3D rendering mode (cross-eyed)
            else:
                scene.stereo = 'nostereo'  # turn off 3D rendering mode

        elif key == 'l':
            sw_lbl = not sw_lbl  # planet/spaceship label switch
            lbl_off = not lbl_off

        elif key == 's':
            #if ship_mode:
 #               music.fadeout(2500) # fade out music when quitting spaceship mode
#            else:
#                music.play(-1)   # turn on music when entering spaceship mode

            ship_mode = not ship_mode  # spaceship mode switch
            a = vector(0, 0, 0)  # reset acceleration
            sw_lbl = True  # turn on spaceship label

        # spaceship thrusters control:

        elif key == 'up':
            #a += vector(0, 0, am)  # old cartesian steering
            a += am*norm(v)  # accelerate into the direction of motion
        elif key == 'down':
            a -= am*norm(v)  # accelerate against the direction of motion
        elif key == 'left':
            a -= am*cross(norm(v),cross(norm(v),norm(s)))   # accelerate to the left of the velocity

        elif key == 'right':
            a += am*cross(norm(v),cross(norm(v),norm(s)))  # accelerate to the right of the velocity
        elif key == 'd':
            a += am*cross(norm(v),norm(s))  # accelerate to the 'top' of the velocity
        elif key == 'c':
            a -= am*cross(norm(v),norm(s))   # accelerate to the 'bottom' of the velocity
        elif key == '0':
            a = vector(0, 0, 0)  # reset acceleration

        # pre-programmed spaceship positions/scenarios:

        elif key == '1':  # Earth-Sun Lagrange point 3 (L3 - 'counter-Earth')
            s = -earthpos
            v0 = (planets[2][1][:, (int(-t + 1)) % n] - planets[2][1][:, (int(-t)) % n]) / (365.25 * 86400 / n)
            v = vector(v0[0], v0[1], v0[2])
            a = vector(0, 0, 0)
            predraw = False
            ship.trail.append(pos=s, retain=1)

        elif key == '2':  # polar orbit around the Sun
            s = vector(3e+07, -2e+06, 2e+08)
            v = vector(0, 24, 0)
            a = vector(0, 0, 0)
            predraw = False
            ship.trail.append(pos=s, retain=1)

        elif key == '3':  # orbit of the probe Helios 2  (fastest man-made object to date - 0.02% speed of light)
            s = vector(43e6, 0, 0)  # perihelion
            v = vector(0, 0, 70.22)
            a = vector(0, 0, 0)
            predraw = False
            ship.trail.append(pos=s, retain=1)

        elif key == '4':  # Halley's comet (derived using specific orbital energy, 162.3 deg inclination)
            s = vector(87813952, 0, 0)  # perihelion
            v = vector(0, 16.7, 52)
            a = vector(0, 0, 0)
            predraw = False
            ship.trail.append(pos=s, retain=1)

    return ship_mode, s, v, a, dt, sw_lbl, lbl_off, predraw, ship
Пример #7
0
def key_check(ship_mode, s, v, a, dt, sw_lbl, lbl_off, predraw, earthpos, t,
              ship):
    if scene.kb.keys:  # check if there are any keys pressed in the queue

        key = scene.kb.getkey()  # retrieve the pressed key

        # change simulation speed: (steps are small on purpose; user has to press and hold for smooth transition)
        if key == 'q' and 1 <= dt < 125:
            dt += 2
        elif key == 'q' and dt <= 0.95:
            dt += 0.05
        elif key == 'a' and dt > 1:
            dt -= 2
        elif key == 'a' and 0.2 < dt <= 1:
            dt -= 0.05

        elif key == 'esc':
            Exit()  # exit the program

        elif key == 'p':
            lbl.visible = True  # show pause label
            lbl.pos = scene.center  # center the label on the screen
            scene.kb.getkey()  # wait for key input
            lbl.visible = False  # hide pause label

        elif key == 'x':
            if scene.stereo == 'nostereo':
                scene.stereo = 'redcyan'  # turn on 3D rendering mode (red-cyan)
            elif scene.stereo == 'redcyan':
                scene.stereo = 'crosseyed'  # turn on 3D rendering mode (cross-eyed)
            else:
                scene.stereo = 'nostereo'  # turn off 3D rendering mode

        elif key == 'l':
            sw_lbl = not sw_lbl  # planet/spaceship label switch
            lbl_off = not lbl_off

        elif key == 's':
            #if ship_mode:
            #               music.fadeout(2500) # fade out music when quitting spaceship mode
            #            else:
            #                music.play(-1)   # turn on music when entering spaceship mode

            ship_mode = not ship_mode  # spaceship mode switch
            a = vector(0, 0, 0)  # reset acceleration
            sw_lbl = True  # turn on spaceship label

        # spaceship thrusters control:

        elif key == 'up':
            #a += vector(0, 0, am)  # old cartesian steering
            a += am * norm(v)  # accelerate into the direction of motion
        elif key == 'down':
            a -= am * norm(v)  # accelerate against the direction of motion
        elif key == 'left':
            a -= am * cross(norm(v), cross(
                norm(v), norm(s)))  # accelerate to the left of the velocity

        elif key == 'right':
            a += am * cross(norm(v), cross(
                norm(v), norm(s)))  # accelerate to the right of the velocity
        elif key == 'd':
            a += am * cross(norm(v),
                            norm(s))  # accelerate to the 'top' of the velocity
        elif key == 'c':
            a -= am * cross(
                norm(v), norm(s))  # accelerate to the 'bottom' of the velocity
        elif key == '0':
            a = vector(0, 0, 0)  # reset acceleration

        # pre-programmed spaceship positions/scenarios:

        elif key == '1':  # Earth-Sun Lagrange point 3 (L3 - 'counter-Earth')
            s = -earthpos
            v0 = (planets[2][1][:, (int(-t + 1)) % n] -
                  planets[2][1][:, (int(-t)) % n]) / (365.25 * 86400 / n)
            v = vector(v0[0], v0[1], v0[2])
            a = vector(0, 0, 0)
            predraw = False
            ship.trail.append(pos=s, retain=1)

        elif key == '2':  # polar orbit around the Sun
            s = vector(3e+07, -2e+06, 2e+08)
            v = vector(0, 24, 0)
            a = vector(0, 0, 0)
            predraw = False
            ship.trail.append(pos=s, retain=1)

        elif key == '3':  # orbit of the probe Helios 2  (fastest man-made object to date - 0.02% speed of light)
            s = vector(43e6, 0, 0)  # perihelion
            v = vector(0, 0, 70.22)
            a = vector(0, 0, 0)
            predraw = False
            ship.trail.append(pos=s, retain=1)

        elif key == '4':  # Halley's comet (derived using specific orbital energy, 162.3 deg inclination)
            s = vector(87813952, 0, 0)  # perihelion
            v = vector(0, 16.7, 52)
            a = vector(0, 0, 0)
            predraw = False
            ship.trail.append(pos=s, retain=1)

    return ship_mode, s, v, a, dt, sw_lbl, lbl_off, predraw, ship
Пример #8
0
def roundc(cp, roundness=0.1, nseg=8, invert=False):

    vort = 0.0
    cp.pop()
    for i in range(len(cp)):
        i1 = (i+1)%len(cp)
        i2 = (i+2)%len(cp)
        v1 = vis.vector(cp[i1]) - vis.vector(cp[i])
        v2 = vis.vector(cp[(i2)%len(cp)]) - vis.vector(cp[i1])
        dv = vis.dot(v1,v2)
        vort += dv

    if vort > 0: cp.reverse()

    l = 999999
    
    for i in range(len(cp)):
        p1 = vis.vector(cp[i])
        p2 = vis.vector(cp[(i+1)%len(cp)])
        lm = vis.mag(p2-p1)
        if lm < l: l = lm

    r = l*roundness
    ncp = []
    lcp = len(cp)

    for i in range(lcp):
        i1 = (i+1)%lcp
        i2 = (i+2)%lcp
        
        w0 = vis.vector(cp[i])
        w1 = vis.vector(cp[i1])
        w2 = vis.vector(cp[i2])

        wrt = vis.cross((w1-w0),(w2-w0))

        v1 = w1-w0
        v2 = w1-w2
        rax = vis.norm(((vis.norm(v1)+vis.norm(v2))/2.0))
        angle = acos(vis.dot(vis.norm(v2),vis.norm(v1)))
        afl = 1.0
        if wrt[2] > 0: afl = -1.0
        angle2 = angle/2.0
        cc = r/sin(angle2)
        ccp = vis.vector(cp[i1]) - rax*cc
        tt = r/tan(angle2)
        t1 = vis.vector(cp[i1]) -vis.norm(v1)*tt
        t2 = vis.vector(cp[i1]) -vis.norm(v2)*tt

        ncp.append(tuple(t1)[0:2])
        nc = []
        a = 0
        dseg = afl*(pi-angle)/nseg
        if not invert:
            for i in range(nseg):
                nc.append(rotatep(t1, ccp, a))
                ncp.append(tuple(nc[-1])[0:2])
                a -= dseg
        else:
            dseg = afl*(angle)/nseg
            for i in range(nseg):
                nc.append(rotatep(t1, (cp[i1][0],cp[i1][1],0), a))
                ncp.append(tuple(nc[-1])[0:2])
                a += dseg
        ncp.append(tuple(t2)[0:2])
    ncp.append(ncp[0])
    return ncp