Hadlock

Barycenter finder

May 1st, 2026
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.82 KB | Source Code | 0 0
  1. import pygame,sys
  2. # bary v1
  3. pygame.init()
  4. W,H=900,500;S=pygame.display.set_mode((W,H));pygame.display.set_caption("Barycenter")
  5. F=pygame.font.SysFont("monospace",13);FB=pygame.font.SysFont("monospace",14,bold=True)
  6. MS=1.989e30;RS=696000;AU=1.496e8;ME=5.972e24
  7. PL=[("Mercury",57.9,.0553),("Venus",108.2,.815),("Earth",149.6,1),("Mars",227.9,.107),("Jupiter",778.5,317.8),("Saturn",1432,95.2),("Uranus",2867,14.5),("Neptune",4515,17.1),("Pluto",5906,.0022),("Eris",10120,.0028),("Ceres",414,.000157)]
  8. PR={"Eris":(10120,.0028),"Pluto":(5906,.0022),"Ceres":(414,.000157)}
  9. SL={"d":10120.,"m":.0028};DR=None
  10. def bc(au,me): sep=au*AU/1000;m2=me*ME;r=sep*m2/(MS+m2);return r,RS-r,RS>r
  11. def sl(lbl,k,x,y,mn,mx,w=250):
  12.     v=SL[k];px=x+int((v-mn)/(mx-mn)*w)
  13.     pygame.draw.rect(S,(50,50,70),(x,y+7,w,4));pygame.draw.circle(S,(100,190,255),(px,y+9),7)
  14.     S.blit(F.render(f"{lbl}: {v:.4g}",1,(190,190,210)),(x,y-16));return pygame.Rect(x,y+2,w,14)
  15. BG=(12,12,22);CI=(70,210,110);CO=(240,80,80);CH=(70,170,255)
  16. cols=[("Body",115),("AU",75),("Mass/ME",80),("Bary km",100),("Diff km",105),("Inside",70)]
  17. SR={};clock=pygame.time.Clock()
  18. while True:
  19.     S.fill(BG);mx,my=pygame.mouse.get_pos()
  20.     for e in pygame.event.get():
  21.         if e.type==pygame.QUIT:sys.exit()
  22.         if e.type==pygame.MOUSEBUTTONUP:DR=None
  23.         if e.type==pygame.MOUSEBUTTONDOWN:
  24.             if SR.get("d") and SR["d"].collidepoint(e.pos):DR="d"
  25.             if SR.get("m") and SR["m"].collidepoint(e.pos):DR="m"
  26.             for i,(n,(a,m)) in enumerate(PR.items()):
  27.                 if pygame.Rect(W-155,75+i*26,140,20).collidepoint(e.pos):SL["d"]=a;SL["m"]=m
  28.         if e.type==pygame.MOUSEMOTION and DR:
  29.             cfg=[("d",80,0.1,15000,250),("m",390,1e-6,400,250)]
  30.             for k,x,mn,mx2,w in cfg:
  31.                 if DR==k:SL[k]=mn+max(0,min(1,(mx-x)/w))*(mx2-mn)
  32.     SR["d"]=sl("Dist AU","d",80,45,.1,15000);SR["m"]=sl("Mass x ME","m",390,45,1e-6,400)
  33.     for i,(n,(a,m)) in enumerate(PR.items()):
  34.         br=pygame.Rect(W-155,75+i*26,140,20);pygame.draw.rect(S,(35,35,55),br,border_radius=3)
  35.         S.blit(F.render(n,1,(170,170,210)),(W-150,78+i*26))
  36.     cx=20;cy=100
  37.     for h,w in cols:
  38.         pygame.draw.rect(S,(25,35,65),(cx,cy,w,20));S.blit(FB.render(h,1,CH),(cx+2,cy+3));cx+=w
  39.     for i,(nm,au,me) in enumerate(PL+[("Custom",SL["d"],SL["m"])]):
  40.         r,df,ins=bc(au,me);cy2=122+i*20;cx2=20;custom=nm=="Custom"
  41.         vs=[nm,f"{au:.1f}",f"{me:.5g}",f"{r:,.0f}",f"{abs(df):,.0f}","YES" if ins else "NO"]
  42.         for j,((h,w),v) in enumerate(zip(cols,vs)):
  43.             pygame.draw.rect(S,(45,45,75) if custom else (22+i%2*8,22+i%2*8,35+i%2*8),(cx2,cy2,w,18))
  44.             tc=(255,240,80) if custom else (CI if(ins and j==5)else(CO if j==5 else(190,190,190)))
  45.             S.blit(F.render(v,1,tc),(cx2+2,cy2+2));cx2+=w
  46.     pygame.display.flip();clock.tick(60)
  47.  
Advertisement
Add Comment
Please, Sign In to add comment