mathphys910ee.py

Created by laigna

Created on March 14, 2026

8.66 KB

Ruutvõrrand, tõus ja sirge, proportsioonid, trigonomeetria, jõud, energia ja töö, elekter, kinemaatika


# MathPhys 9-10 EE - NumWorks
# Created by Pärle Laigna - https://parle.laigna.com
# Nooled:vali OK:kinnita Back:tagasi
from kandinsky import fill_rect as F,draw_string as D
from ion import keydown as K
from time import sleep as Z
from math import pi,sqrt,sin,cos,tan,asin,acos,atan,degrees,radians
SW,SH=320,222
BK=(0,)*3;WH=(255,)*3
BL=(50,110,230);TL=(0,190,190)
YL=(220,200,60);DG=(120,)*3

okv=1 if K(4)or K(52) else 0

def okd():
 return K(4)or K(52)

def okp():
 global okv
 d=okd()
 if d and not okv:
  okv=1
  return True
 if not d:okv=0
 return False

def wup():
 while okd():Z(0.02)
 Z(0.12)

def menu(title,items,sub=False):
 sel=0;n=len(items)
 while True:
  F(0,0,SW,SH,BK)
  D(title,SW//2-len(title)*5,6,TL,BK)
  F(0,24,SW,2,(40,)*3)
  for i in range(n):
   y=34+i*22
   if i==sel:
    F(8,y,SW-16,20,BL)
    D("> "+items[i],14,y+2,WH,BL)
   else:
    D("  "+items[i],14,y+2,DG,BK)
  if sub:D("Back=tagasi",100,SH-16,(60,)*3,BK)
  else:D("Back=valju",115,SH-16,(60,)*3,BK)
  while True:
   if K(1):sel=(sel-1)%n;Z(0.15);break
   if K(2):sel=(sel+1)%n;Z(0.15);break
   if okp():wup();return sel
   if K(17):return-1
   Z(0.04)

def inp(prompt):
 try:return float(input(prompt))
 except:return None

def show(lines):
 F(0,0,SW,SH,BK)
 D("TULEMUS",125,6,TL,BK)
 F(0,24,SW,2,(40,)*3)
 for i,ln in enumerate(lines):
  c=YL if i==len(lines)-1 else WH
  D(str(ln),14,34+i*20,c,BK)
 D("OK=tagasi",120,SH-16,DG,BK)
 while True:
  if okp()or K(17):wup();return
  Z(0.04)

# === ALGEBRA ===
def f_quad():
 a=inp("a: ");b=inp("b: ");c=inp("c: ")
 if a and a!=0 and b is not None and c is not None:
  disc=b*b-4*a*c
  r=["ax^2 + bx + c = 0",
   "a="+str(a)+" b="+str(b)+" c="+str(c),
   "D = b^2-4ac = "+str(round(disc,6))]
  if disc>0:
   x1=(-b+sqrt(disc))/(2*a)
   x2=(-b-sqrt(disc))/(2*a)
   r+=["x1 = "+str(round(x1,6)),
    "x2 = "+str(round(x2,6))]
  elif disc==0:
   x1=-b/(2*a)
   r+=["x = "+str(round(x1,6))+" (kahekordne)"]
  else:
   re=-b/(2*a);im=sqrt(-disc)/(2*a)
   r+=["x1 = "+str(round(re,4))+"+"+str(round(im,4))+"i",
    "x2 = "+str(round(re,4))+"-"+str(round(im,4))+"i"]
  show(r)

def f_slope():
 x1=inp("x1: ");y1=inp("y1: ")
 x2=inp("x2: ");y2=inp("y2: ")
 if x1 is not None and y1 is not None and x2 is not None and y2 is not None:
  r=["Punktid: ("+str(x1)+","+str(y1)+") ("+str(x2)+","+str(y2)+")"]
  if x2!=x1:
   m=(y2-y1)/(x2-x1)
   b=y1-m*x1
   d=sqrt((x2-x1)**2+(y2-y1)**2)
   r+=["Tous m = "+str(round(m,6)),
    "y-loikep. b = "+str(round(b,6)),
    "y = "+str(round(m,4))+"x + "+str(round(b,4)),
    "Kaugus = "+str(round(d,6))]
  else:r+=["Pustsirge (x="+str(x1)+")"]
  show(r)

def f_prop():
 c=menu("Proportsioonid",
  ["a/b = c/d  (leia d)",
   "a/b = c/d  (leia c)"],True)
 if c==0:
  a=inp("a: ");b=inp("b: ");cv=inp("c: ")
  if a and b and cv is not None and a!=0:
   show(["a/b = c/d","a="+str(a)+" b="+str(b)+" c="+str(cv),
    "d = b*c/a","d = "+str(round(b*cv/a,6))])
 elif c==1:
  a=inp("a: ");b=inp("b: ");d=inp("d: ")
  if a and b and d is not None and b!=0:
   show(["a/b = c/d","a="+str(a)+" b="+str(b)+" d="+str(d),
    "c = a*d/b","c = "+str(round(a*d/b,6))])

# === TRIGONOMEETRIA ===
def f_trig():
 c=menu("Trigonomeetria",
  ["sin/cos/tan (nurk->suhe)",
   "arcsin/cos/tan (suhe->nurk)",
   "Taisnurkne kolmnurk"],True)
 if c==0:
  a=inp("Nurk (kraadid): ")
  if a is not None:
   r=radians(a)
   show(["Nurk = "+str(a)+" kraadi",
    "sin = "+str(round(sin(r),8)),
    "cos = "+str(round(cos(r),8)),
    "tan = "+str(round(tan(r),8))if abs(cos(r))>1e-10 else "tan = maaramata"])
 elif c==1:
  v=inp("Suhe vaartus: ")
  if v is not None:
   r=["Vaartus = "+str(v)]
   if-1<=v<=1:
    r+=["arcsin = "+str(round(degrees(asin(v)),6))+" kr",
     "arccos = "+str(round(degrees(acos(v)),6))+" kr"]
   else:r+=["arcsin/arccos: |v| peab olema <=1"]
   r+=["arctan = "+str(round(degrees(atan(v)),6))+" kr"]
   show(r)
 elif c==2:
  print("\n"*5+"Taisnurkne kolmnurk: tea 2 kulge")
  a=inp("Kulg a (voi 0=teadmata): ")
  b=inp("Kulg b (voi 0=teadmata): ")
  c2=inp("Hupotenuus c (voi 0=teadmata): ")
  if a is not None and b is not None and c2 is not None:
   r=["Taisnurkse kolmnurga lahendaja"]
   if a and b and not c2:
    c2=sqrt(a*a+b*b)
    r+=["c = "+str(round(c2,6))]
   elif a and c2 and not b:
    if c2>a:b=sqrt(c2*c2-a*a);r+=["b = "+str(round(b,6))]
   elif b and c2 and not a:
    if c2>b:a=sqrt(c2*c2-b*b);r+=["a = "+str(round(a,6))]
   if a and c2 and c2!=0:
    A=degrees(asin(a/c2))
    r+=["Nurk A = "+str(round(A,4))+" kr",
     "Nurk B = "+str(round(90-A,4))+" kr"]
   show(r)

# === MEHAANIKA ===
def f_force():
 c=menu("Joud (F=ma)",
  ["F = m x a  (leia joud)",
   "m = F / a  (leia mass)",
   "a = F / m  (leia kiirendus)"],True)
 if c==0:
  m=inp("Mass (kg): ");a=inp("Kiirendus (m/s2): ")
  if m and a is not None:
   show(["F = m x a","m="+str(m)+" a="+str(a),
    "F = "+str(round(m*a,6))+" N"])
 elif c==1:
  f=inp("Joud (N): ");a=inp("Kiirendus (m/s2): ")
  if f is not None and a and a!=0:
   show(["m = F / a","F="+str(f)+" a="+str(a),
    "m = "+str(round(f/a,6))+" kg"])
 elif c==2:
  f=inp("Joud (N): ");m=inp("Mass (kg): ")
  if f is not None and m and m!=0:
   show(["a = F / m","F="+str(f)+" m="+str(m),
    "a = "+str(round(f/m,6))+" m/s2"])

def f_energy():
 c=menu("Energia ja too",
  ["Ek = 1/2 mv^2 (kineetiline)",
   "Ep = mgh (potentsiaalne)",
   "A = F x d (too)",
   "P = A / t (voimsus)"],True)
 if c==0:
  m=inp("Mass (kg): ");v=inp("Kiirus (m/s): ")
  if m and v is not None:
   show(["Kineetiline energia","m="+str(m)+" v="+str(v),
    "Ek = 0.5 x m x v^2",
    "Ek = "+str(round(0.5*m*v*v,4))+" J"])
 elif c==1:
  m=inp("Mass (kg): ");h=inp("Korgus (m): ")
  if m and h is not None:
   g=9.81
   show(["Potentsiaalne energia (g=9.81)",
    "m="+str(m)+" h="+str(h),
    "Ep = m x g x h",
    "Ep = "+str(round(m*g*h,4))+" J"])
 elif c==2:
  f=inp("Joud (N): ");d=inp("Teepikkus (m): ")
  if f is not None and d is not None:
   show(["Too","F="+str(f)+" d="+str(d),
    "A = F x d","A = "+str(round(f*d,4))+" J"])
 elif c==3:
  w=inp("Too (J): ");t=inp("Aeg (s): ")
  if w is not None and t and t!=0:
   show(["Voimsus","A="+str(w)+" t="+str(t),
    "P = A / t","P = "+str(round(w/t,4))+" W"])

# === ELEKTER ===
def f_ohm():
 c=menu("Ohmi seadus + Voimsus",
  ["U = I x R  (pinge)",
   "I = U / R  (vool)",
   "R = U / I  (takistus)",
   "P = U x I  (voimsus)",
   "P = I^2 x R",
   "P = U^2 / R"],True)
 vals=[(("Vool I (A): ","Takistus R (oom): "),lambda i,r:("U = I x R","U = "+str(round(i*r,6))+" V")),
  (("Pinge U (V): ","Takistus R (oom): "),lambda v,r:("I = U / R","I = "+str(round(v/r,6))+" A")),
  (("Pinge U (V): ","Vool I (A): "),lambda v,i:("R = U / I","R = "+str(round(v/i,6))+" oom")),
  (("Pinge U (V): ","Vool I (A): "),lambda v,i:("P = U x I","P = "+str(round(v*i,6))+" W")),
  (("Vool I (A): ","Takistus R (oom): "),lambda i,r:("P = I^2 x R","P = "+str(round(i*i*r,6))+" W")),
  (("Pinge U (V): ","Takistus R (oom): "),lambda v,r:("P = U^2 / R","P = "+str(round(v*v/r,6))+" W"))]
 if 0<=c<6:
  pr,fn=vals[c]
  a=inp(pr[0]);b=inp(pr[1])
  if a is not None and b is not None and b!=0:
   f,r=fn(a,b)
   show([f,"= "+str(a)+", "+str(b),r])

# === KINEMAATIKA ===
def f_kinem():
 c=menu("Kinemaatika",
  ["v = v0 + at",
   "s = v0*t + 1/2*a*t^2",
   "v^2 = v0^2 + 2as",
   "s = (v0+v)/2 * t"],True)
 if c==0:
  v0=inp("v0 (m/s): ");a=inp("a (m/s2): ");t=inp("t (s): ")
  if v0 is not None and a is not None and t is not None:
   show(["v = v0 + a*t",
    "v0="+str(v0)+" a="+str(a)+" t="+str(t),
    "v = "+str(round(v0+a*t,6))+" m/s"])
 elif c==1:
  v0=inp("v0 (m/s): ");a=inp("a (m/s2): ");t=inp("t (s): ")
  if v0 is not None and a is not None and t is not None:
   d=v0*t+0.5*a*t*t
   show(["s = v0*t + 0.5*a*t^2",
    "v0="+str(v0)+" a="+str(a)+" t="+str(t),
    "s = "+str(round(d,6))+" m"])
 elif c==2:
  v0=inp("v0 (m/s): ");a=inp("a (m/s2): ");d=inp("s (m): ")
  if v0 is not None and a is not None and d is not None:
   v2=v0*v0+2*a*d
   if v2>=0:
    show(["v^2 = v0^2 + 2as",
     "v0="+str(v0)+" a="+str(a)+" s="+str(d),
     "v^2 = "+str(round(v2,4)),
     "v = "+str(round(sqrt(v2),6))+" m/s"])
   else:
    show(["v^2 = v0^2 + 2as",
     "v^2 = "+str(round(v2,4)),
     "Keha peatub enne s"])
 elif c==3:
  v0=inp("v0 (m/s): ");v=inp("v (m/s): ");t=inp("t (s): ")
  if v0 is not None and v is not None and t is not None:
   show(["s = (v0+v)/2 * t",
    "v0="+str(v0)+" v="+str(v)+" t="+str(t),
    "s = "+str(round((v0+v)/2*t,6))+" m"])

CATS=["Algebra - Ruutvorrand",
 "Algebra - Tous ja sirge",
 "Algebra - Proportsioonid",
 "Trigonomeetria",
 "Joud (F=ma)",
 "Energia ja too",
 "Elekter (Ohmi seadus)",
 "Kinemaatika (liikumine)"]
FNS=[f_quad,f_slope,f_prop,f_trig,
 f_force,f_energy,f_ohm,f_kinem]
def run():
 while True:
  c=menu("MathPhys 9-10 EE",CATS)
  if c<0:return
  while True:
   FNS[c]()
   break
run()

During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:

With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our cookies policy.