Advertisement
Momir

Grafik funkcije

Mar 21st, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 11.57 KB | None | 0 0
  1. import contextlib
  2. import math
  3. import os
  4. import sys
  5.  
  6. with contextlib.redirect_stdout(None):
  7.     import pygame
  8.  
  9. os.system('color F0')
  10. pygame.init()
  11.  
  12. # Postavlja font
  13. pygame.font.init()
  14. myfont = pygame.font.SysFont('Arial Rounded MT Bold', 30)
  15.  
  16. # Postavlja naslov terminala
  17. sys.stdout.write("\x1b]2;Grafik funkcije\x07")
  18.  
  19. # Postavlja naslov grafičkog prozora
  20. pygame.display.set_caption('Grafik funkcije')
  21.  
  22. pygame.event.set_allowed(pygame.KEYDOWN)
  23.  
  24.  
  25. def sin(abc):
  26.     return math.sin(abc)
  27.  
  28.  
  29. def cos(abc2):
  30.     return math.cos(abc2)
  31.  
  32.  
  33. def tan(abc3):
  34.     return math.tan(abc3)
  35.  
  36.  
  37. def arctan(abc4):
  38.     return math.atan(abc4)
  39.  
  40.  
  41. #  Ispisuje poruku sa uputstvom za korišćenje programa
  42. def uvod():
  43.     print("")
  44.     print("                KOMANDE               ")
  45.     print("--------------------------------------")
  46.     print(" Pomeranje grafika: strelice")
  47.     print(" Skaliranje grafika: W, A, S, D")
  48.     print(" Ponovno unošenje formule: Q")
  49.     print(" Grafik bez IV kvadranta: 1")
  50.     print(" Grafik sa IV kvadrantom: 2")
  51.     print(" Podešavanje prozora: E, R, T, F")
  52.     print(" Pomoćne linije: G")
  53.     print("--------------------------------------")
  54.     print(" Formula za grafik funkcije y=f(x),")
  55.     print(" gde je f(x)=2*x+sin(x) se u program")
  56.     print(" unosi u obliku 2*x+sin(x). Program")
  57.     print(" prepoznaje funkcije sin(), cos(),")
  58.     print(" tan() i arctan(). Redosled i broj")
  59.     print(" funkcija i promenljive x u formuli ")
  60.     print(" je nebitan.")
  61.     print("--------------------------------------")
  62.     print("")
  63.  
  64.  
  65. uvod()
  66.  
  67.  
  68. def unos_formule():
  69.     formula = input(" Unesite formulu: ")
  70.     print("")
  71.     return formula
  72.  
  73.  
  74. def function(x):
  75.     yyy = eval(formula.lower())
  76.     return yyy
  77.  
  78.  
  79. # Definisanje koeficijenata i slobodnih članova
  80. A = 440
  81. B = 1.0
  82. C = 1.0
  83. E = 0
  84. P = 0
  85.  
  86. # Indikator za pomoćne linije
  87. pomlinije = False
  88.  
  89. formula = unos_formule()
  90.  
  91. #  Početne dimenzije grafičkog prozora
  92. width = 680
  93. height = 440  # + 50
  94.  
  95.  
  96. def update():
  97.     pygame.display.flip()
  98.  
  99.  
  100. screen = pygame.display.set_mode((width, height + 50))
  101.  
  102.  
  103. #  Ispis pozadine grafičkog prozora sa I kvadrantom
  104. def ispis_ekrana1():
  105.     screen.fill((255, 255, 255))
  106.     pygame.draw.line(screen, (0, 0, 0), (82, 8), (82, height + 13), 3)
  107.     pygame.draw.line(screen, (0, 0, 0), (72, height), (width - 25, height), 3)
  108.     pygame.draw.line(screen, (0, 0, 0), (82, 8), (85, 23), 3)
  109.     pygame.draw.line(screen, (0, 0, 0), (82, 8), (79, 23), 3)
  110.     pygame.draw.line(screen, (0, 0, 0), (width - 25, height), (width - 40, height + 3), 3)
  111.     pygame.draw.line(screen, (0, 0, 0), (width - 25, height), (width - 40, height - 3), 3)
  112.     textsurfacey = myfont.render('y', False, (0, 0, 0))
  113.     screen.blit(textsurfacey, (60, 11))
  114.     textsurfacex = myfont.render('x', False, (0, 0, 0))
  115.     screen.blit(textsurfacex, (width - 45, height + 7))
  116.     for obx in range(0, 999999, 70):
  117.         pygame.draw.line(screen, (0, 0, 0), (obx + 82, height), (obx + 82, height + 13), 3)
  118.         if obx != 0 and pomlinije is True:
  119.             pygame.draw.line(screen, (198, 198, 198), (obx + 82, height - 2), (obx + 82, 25), 1)
  120.         pom = round(float(obx * C / 100 + P), 2)
  121.         if pom == -0.00:
  122.             pom = 0.00
  123.         textsurfaceobx = myfont.render(str(pom), False, (0, 0, 0))
  124.         screen.blit(textsurfaceobx, (obx + 64, height + 16))
  125.         if obx + 82 > width - 130:
  126.             break
  127.  
  128.     for oby in range(0, 99999, 50):
  129.         pygame.draw.line(screen, (0, 0, 0), (72, height - oby), (82, height - oby), 3)
  130.         if oby != 0 and pomlinije is True:
  131.             pygame.draw.line(screen, (198, 198, 198), (84, height - oby), (width - 40, height - oby), 1)
  132.         pom2 = round(float((oby + E) / abs(B) / 100), 2)
  133.         if pom2 == -0.00:
  134.             pom2 = 0.00
  135.         textsurfaceoby = myfont.render(str(pom2), False, (0, 0, 0))
  136.         screen.blit(textsurfaceoby, (10, height - 7 - oby))
  137.         if height - oby < 90:
  138.             break
  139.     update()
  140.  
  141.  
  142. #  Ispis pozadine grafičkog prozora sa I i IV kvadrantom
  143. def ispis_ekrana2():
  144.     screen.fill((255, 255, 255))
  145.     pygame.draw.line(screen, (0, 0, 0), (82, 8), (82, height + 13), 3)
  146.     pygame.draw.line(screen, (0, 0, 0), (72, (height + 40) / 2), (width - 25, (height + 40) / 2), 3)
  147.     pygame.draw.line(screen, (0, 0, 0), (82, 8), (85, 23), 3)
  148.     pygame.draw.line(screen, (0, 0, 0), (82, 8), (79, 23), 3)
  149.     pygame.draw.line(screen, (0, 0, 0), (width - 25, (height + 40) / 2), (width - 40, (height + 40) / 2 + 3), 3)
  150.     pygame.draw.line(screen, (0, 0, 0), (width - 25, (height + 40) / 2), (width - 40, (height + 40) / 2 - 3), 3)
  151.     textsurfacey = myfont.render('y', False, (0, 0, 0))
  152.     screen.blit(textsurfacey, (60, 11))
  153.     textsurfacex = myfont.render('x', False, (0, 0, 0))
  154.     screen.blit(textsurfacex, (width - 45, (height + 40) / 2 + 7))
  155.     for obx in range(0, 999999, 70):
  156.         if obx != 0 and pomlinije is True:
  157.             pygame.draw.line(screen, (198, 198, 198), (obx + 82, height - 2), (obx + 82, 25), 1)
  158.         pygame.draw.line(screen, (0, 0, 0), (obx + 82, (height + 40) / 2), (obx + 82, (height + 40) / 2 + 13), 3)
  159.         pom = round(float(obx * C / 100 + P), 2)
  160.         if pom == -0.00:
  161.             pom = 0.00
  162.         textsurfaceobx = myfont.render(str(pom), False, (0, 0, 0))
  163.  
  164.         if obx != 0:
  165.             screen.blit(textsurfaceobx, (obx + 64, (height + 40) / 2 + 16))
  166.         else:
  167.             screen.blit(textsurfaceobx, (obx + 64, height + 16))
  168.         if obx + 82 > width - 130:
  169.             break
  170.  
  171.     for oby in range(0, 99999, 50):
  172.         pygame.draw.line(screen, (0, 0, 0), (72, (height + 40) / 2 - oby), (82, (height + 40) / 2 - oby), 3)
  173.         if oby != 0 and pomlinije is True:
  174.             pygame.draw.line(screen, (198, 198, 198), (84, (height + 40) / 2 - oby),
  175.                              (width - 40, (height + 40) / 2 - oby), 1)
  176.         pom2 = round(float((oby + E) / abs(B) / 100), 2)
  177.         if pom2 == -0.00:
  178.             pom2 = 0.00
  179.         textsurfaceoby = myfont.render(str(pom2), False, (0, 0, 0))
  180.         screen.blit(textsurfaceoby, (10, (height + 40) / 2 - 7 - oby))
  181.         if (height + 40) / 2 - oby < 90:
  182.             break
  183.     for oby in range(0, 99999, 50):
  184.         pygame.draw.line(screen, (0, 0, 0), (72, (height + 40) / 2 + oby), (82, (height + 40) / 2 + oby), 3)
  185.         if oby != 0 and pomlinije is True:
  186.             pygame.draw.line(screen, (198, 198, 198), (84, (height + 40) / 2 + oby),
  187.                              (width - 40, (height + 40) / 2 + oby), 1)
  188.         pom2 = round(float((oby + E) / abs(B) / 100), 2)
  189.         if pom2 == -0.00:
  190.             pom2 = 0.00
  191.         textsurfaceoby = myfont.render(str(pom2), False, (0, 0, 0))
  192.         if oby != 0:
  193.             screen.blit(textsurfaceoby, (10, (height + 40) / 2 - 7 + oby))
  194.         if (height + 40) / 2 + oby > height - 50:
  195.             break
  196.     update()
  197.  
  198.  
  199. def napravi_prozor():
  200.     pygame.display.set_mode((width, height + 50))
  201.  
  202.  
  203. ispis_ekrana1()
  204.  
  205. finish = True
  206.  
  207. update()
  208.  
  209. while finish:
  210.  
  211.     for event in pygame.event.get():
  212.         if event.type == pygame.QUIT:
  213.             finish = False
  214.         #  Unos komandi koje detektuju samo jedan klik
  215.         if event.type == pygame.KEYDOWN and event.key == pygame.K_q:
  216.             formula = unos_formule()
  217.             if A == height:
  218.                 ispis_ekrana1()
  219.             else:
  220.                 ispis_ekrana2()
  221.         if event.type == pygame.KEYDOWN and event.key == pygame.K_g:
  222.             if pomlinije is False:
  223.                 pomlinije = True
  224.             else:
  225.                 pomlinije = False
  226.             if A == height:
  227.                 ispis_ekrana1()
  228.             else:
  229.                 ispis_ekrana2()
  230.         if event.type == pygame.KEYDOWN and event.key == pygame.K_1 and A == (height + 40) / 2:
  231.             A = height
  232.             ispis_ekrana1()
  233.         if event.type == pygame.KEYDOWN and event.key == pygame.K_2 and A == height:
  234.             A = (height + 40) / 2
  235.             ispis_ekrana2()
  236.  
  237.     #  Unos komandi koje traju
  238.     keys_pressed = pygame.key.get_pressed()
  239.  
  240.     if keys_pressed[pygame.K_r]:
  241.         if height > 140:
  242.             if A == height:
  243.                 height -= 10
  244.                 A = height
  245.                 napravi_prozor()
  246.                 ispis_ekrana1()
  247.             else:
  248.                 height -= 10
  249.                 A = (height + 40) / 2
  250.                 napravi_prozor()
  251.                 ispis_ekrana2()
  252.  
  253.     if keys_pressed[pygame.K_f]:
  254.         if A == height:
  255.             height += 10
  256.             A = height
  257.             napravi_prozor()
  258.             ispis_ekrana1()
  259.         else:
  260.             height += 10
  261.             A = (height + 40) / 2
  262.             napravi_prozor()
  263.             ispis_ekrana2()
  264.  
  265.     if keys_pressed[pygame.K_e]:
  266.         if width > 290:
  267.             width -= 10
  268.             napravi_prozor()
  269.             if A == height:
  270.                 ispis_ekrana1()
  271.             else:
  272.                 ispis_ekrana2()
  273.  
  274.     if keys_pressed[pygame.K_t]:
  275.         width += 10
  276.         napravi_prozor()
  277.         if A == height:
  278.             ispis_ekrana1()
  279.         else:
  280.             ispis_ekrana2()
  281.  
  282.     if keys_pressed[pygame.K_UP]:
  283.         E -= 0.8
  284.         if A == height:
  285.             ispis_ekrana1()
  286.         else:
  287.             ispis_ekrana2()
  288.  
  289.     if keys_pressed[pygame.K_DOWN]:
  290.         E += 0.8
  291.         if A == height:
  292.             ispis_ekrana1()
  293.         else:
  294.             ispis_ekrana2()
  295.  
  296.     if keys_pressed[pygame.K_LEFT]:
  297.         P += 0.02
  298.         if A == height:
  299.             ispis_ekrana1()
  300.         else:
  301.             ispis_ekrana2()
  302.  
  303.     if keys_pressed[pygame.K_RIGHT]:
  304.         P -= 0.02
  305.         if A == height:
  306.             ispis_ekrana1()
  307.         else:
  308.             ispis_ekrana2()
  309.  
  310.     if keys_pressed[pygame.K_a]:
  311.         C *= 1.005
  312.         if A == height:
  313.             ispis_ekrana1()
  314.         else:
  315.             ispis_ekrana2()
  316.  
  317.     if keys_pressed[pygame.K_d]:
  318.         C /= 1.005
  319.         if A == height:
  320.             ispis_ekrana1()
  321.         else:
  322.             ispis_ekrana2()
  323.  
  324.     if keys_pressed[pygame.K_w]:
  325.         if B > -0.02:
  326.             if B <= 0:
  327.                 B = 0.02
  328.             else:
  329.                 B += 0.015
  330.         else:
  331.             B += 0.015
  332.         if A == height:
  333.             ispis_ekrana1()
  334.         else:
  335.             ispis_ekrana2()
  336.  
  337.     if keys_pressed[pygame.K_s]:
  338.         if B < 0.02:
  339.             if B >= 0:
  340.                 B = -0.02
  341.             else:
  342.                 B -= 0.015
  343.         else:
  344.             B -= 0.015
  345.         if A == height:
  346.             ispis_ekrana1()
  347.         else:
  348.             ispis_ekrana2()
  349.  
  350.     #  Ispis linije funkcije
  351.     x = 0
  352.     y = int(A - 2 - B * 100 * function(C * (float(x) / 100) + P) + E)
  353.     for x2 in range(0, 2000):
  354.         if x2 < width - 122:
  355.             y2 = int(A - 2 - B * 100 * function(C * (float(x2) / 100) + P) + E)
  356.             if y2 < 25:
  357.                 y2 = 25
  358.             if y2 > height:
  359.                 y2 = height
  360.  
  361.             if x2 < width - 122 and x < width - 122 and (y > 25 or y2 > 25) and (y < height or y2 < height):
  362.                 if x2 == 0:
  363.                     pygame.draw.line(screen, (0, 0, 0), (x + 84, y), (x2 + 84, y2), 2)
  364.                 else:
  365.                     pygame.draw.line(screen, (237, 0, 0), (x + 84, y), (x2 + 84, y2), 2)
  366.                 x = x2
  367.                 y = y2
  368.             else:
  369.                 x = x2
  370.                 y = y2
  371.                 continue
  372.         else:
  373.             break
  374.  
  375.     update()
  376.     # pygame.time.delay(1)  #  U zavisnosti od performansi računara može pomoći u smanjenju treperenja grafika
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement