Yet more problems with starmap

16 views
Skip to first unread message

Richard Darwin

unread,
Jun 27, 2024, 9:20:22 PMJun 27
to Glowscript Users
I have had some success with this version, but while running the routine on its own, I find I don't see the label with data about the star that was clicked on.  In fact, the routine just hangs and freezes.  Presumably I've done something wrong with label(), but I don't know what.  
Here is my code so far:
```from vpython import *

scene = canvas(background=color.gray(0.25))
scene.width = 600; scene.height = 400; scene.background = color.gray(0.1)
box(color=color.black, pos=vector(-7,-7,-7))

stars = [ ('Sol', ('SUN', '--', 'Sol'), 'G2.0 V', vector(255, 255, 0), 4.85, '1', '1+8P', vector(0.0, -0.0, 0.0), '8 planets'),
            ('Proxima Centauri', ('GJ  551 C','49', 'Proxima'), 'M5.0 V', vector(192, 51, 0), 15.48, 0.11, '1+1P', vector(-0.97174, -0.74373, -0.44076), 'separation 7849"'),
            ('alpha Centauri A', ('GJ  559 A','50', 'a Centauri A, Rigil Kentaurus'), 'G2.0 V', vector(255, 255, 0), 4.38, 1.14, '3', vector(-1.00228, -0.83810, 0.28985), 'none'),
            ('alpha Centauri B', ('GJ  559 B','51', 'a Centauri B, Toliman'), 'K0 V', vector(255, 153, 0), 5.71, 0.92, '-', vector(-1.00218, -0.83784, 0.29099), 'orbit 18"'),
            ("Barnard's Star", ('GJ  699','57', "Barnard's Arrow"), 'M3.5 V', vector(192, 51, 0), 13.25, 0.16, '1', vector(0.00588, 0.61490, -1.72693), 'none'),
            ('Wolf 359', ('GJ  406','36', 'Wolf 359'), 'M5.5 V', vector(192, 51, 0), 16.64, 0.09, '1', vector(-0.60254, 0.17139, -2.30236), 'none'),
            ('Lalande 21185', ('GJ  411','37', 'Lalande 21185'), 'M2.0 V', vector(192, 51, 0), 10.44, 0.46, '1', vector(-2.46549, 0.62230, -0.02201), 'none'),
            ('Sirius B', ('GJ  244 B','-', 'Sirius B'), 'N', vector(0, 255, 255), 11.34, 1, '-', vector(-0.33614, 1.68420, -1.99374), 'orbit 8"'),
            ('Sirius A', ('GJ  244 A','219', 'Sirius A'), 'A1.0 V', vector(255, 255, 255), 1.47, 1.99, '2', vector(-0.33614, 1.68419, -1.993741), ''),
            ('BL Ceti', ('GJ 65 A','9', 'BL Ceti'), 'M5.5 V', vector(192, 51, 0), 15.47, 0.11, '2', vector(1.18752, 0.54759, -2.334656), ''),
            ('UV Ceti', ('GJ 65 B','10', 'UV Ceti'), 'M6.0 V', vector(192, 51, 0), 15.93, 0.1, '-', vector(1.18752, 0.54759, -2.33465), 'orbit < 2"'),
            ('Ross 154', ('GJ  729','3414', 'Ross 154'), 'M3.5 V', vector(192, 51, 0), 13.08, 0.17, '1', vector(-0.567120, 2.56748, -1.37119), '')
        ]
       
star_show = []

           
def aktis(x):
    """ aktis, aktina is Greek for radius
    Return product as calculated-radius based on mass
    """
    return x**0.78
    #. end def


def starcolour(spectra):
    """ Return a 3tuple from the color picker; spectra holds a string like 'M5.0 V' """
    sc = spectra[0]
    dicto = {
        'O': vector(0, 0.6, 1.0),        # blue
        'B': vector(0.4, 0.8, 1.0),      # blue-white
        'A': vector(1.0, 1.0, 1.0),      # white
        'F': vector(1.0, 1.0, 0.6),      # yellow-white
        'G': vector(1.0, 1.0, 0),        # yellow
        'K': vector(1.0, 0.6, 0),        # orange
        'M': vector(0.75, 0.2, 0),       # red
        'L': vector(1.0,0.0,1.0),        # magenta
        'D': vector(1.0, 0, 0)           # deep red
        }
    # print(f'in starcolour: {sc}')
    return dicto.get(sc, vector(0, 1.0, 1.0)) # if (sc in dicto.keys) else vector(0.0, 1.0, 1.0)    # cyan, why not!?
    #. end def

   
def show_starmap(sx):
    """ Show the starmap... """
    global star_show
    spectralclasses = ['O', 'B', 'A', 'F', 'G', 'K', 'M', 'L', 'D']
   
    Rf = 0.05
    vec0 = vector(0,0,0)
    L = 5e10
    # e = 0.05
    x = sx

    """
    # Build the triaxial marker
    xaxis = curve(color=color.gray(0.5), radius=3e8)
    xaxis.append(vec(-0.5,-0.5,-0.5))
    xaxis.append(vec(L,0,0))
    yaxis = curve(color=color.gray(0.5), radius=3e8)
    yaxis.append(vec(-0.5,-0.5,-0.5))
    yaxis.append(vec(0,L,0))
    zaxis = curve(color=color.gray(0.5), radius=3e8)
    zaxis.append(vec(-0.5,-0.5,-0.5))
    zaxis.append(vec(0,0,L))
    """

    # Set up the stars
    for i, star in enumerate(stars):
        star_show.append(sphere(pos=star[-2], color=starcolour(star[-7]), radius=Rf*aktis(star[-4]), emissive=True))
    # Eventually I'll use stars[sx].rect3 to normalize the stars around the star where our ship is docked that's asking for the starmap

    #. end def show_starmap

# Onclick handler
def click_handler(evt):
    """ Handle click event by showing a label about the star clicked-on. """
    global star_show
    for i, starx in enumerate(star_show):
        if scene.mouse.pick == starx:
            # Note we are consulting stars, the reference array, not starx
            star_label = label(xoffset=200, yoffset=250, linecolor=color.blue, space=5, text=f'***{stars[i][0]}: {stars[i][-7]} abs_mag: {stars[i][-5]}***')  # nomen: spectral, abs_mag
            star_label.canvas = scene
    #. end def click_handler


scene.bind('click', click_handler)    
   
   
while True:
    rate(30)
    show_starmap(0)
    # print('[[['repr(stars[0][-7]),repr(stars[0][-7][0]), starcolour(stars[0][-6]))
    #. end def

if __name__ == '__main__':
    show_starmap(0)
```
Any suggestions or clarifications would be appreciated. Thanks.







Bruce Sherwood

unread,
Jun 28, 2024, 11:26:31 AMJun 28
to Glowscript Users
Delete that "while True" code. You should execute  show_starmap(0) just once.

Secondly, in star_label, drastically reduce xoffset and yoffset; I think you are placing the label outside the canvas.

Bruce
Reply all
Reply to author
Forward
0 new messages