Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import contextlib
- import math
- import os
- import sys
- with contextlib.redirect_stdout(None):
- import pygame
- os.system('color F0')
- pygame.init()
- # Postavlja font
- pygame.font.init()
- myfont = pygame.font.SysFont('Arial Rounded MT Bold', 30)
- # Postavlja naslov terminala
- sys.stdout.write("\x1b]2;Grafik funkcije\x07")
- # Postavlja naslov grafičkog prozora
- pygame.display.set_caption('Grafik funkcije')
- pygame.event.set_allowed(pygame.KEYDOWN)
- def sin(abc):
- return math.sin(abc)
- def cos(abc2):
- return math.cos(abc2)
- def tan(abc3):
- return math.tan(abc3)
- def arctan(abc4):
- return math.atan(abc4)
- # Ispisuje poruku sa uputstvom za korišćenje programa
- def uvod():
- print("")
- print(" KOMANDE ")
- print("--------------------------------------")
- print(" Pomeranje grafika: strelice")
- print(" Skaliranje grafika: W, A, S, D")
- print(" Ponovno unošenje formule: Q")
- print(" Grafik bez IV kvadranta: 1")
- print(" Grafik sa IV kvadrantom: 2")
- print(" Podešavanje prozora: E, R, T, F")
- print(" Pomoćne linije: G")
- print("--------------------------------------")
- print(" Formula za grafik funkcije y=f(x),")
- print(" gde je f(x)=2*x+sin(x) se u program")
- print(" unosi u obliku 2*x+sin(x). Program")
- print(" prepoznaje funkcije sin(), cos(),")
- print(" tan() i arctan(). Redosled i broj")
- print(" funkcija i promenljive x u formuli ")
- print(" je nebitan.")
- print("--------------------------------------")
- print("")
- uvod()
- def unos_formule():
- formula = input(" Unesite formulu: ")
- print("")
- return formula
- def function(x):
- yyy = eval(formula.lower())
- return yyy
- # Definisanje koeficijenata i slobodnih članova
- A = 440
- B = 1.0
- C = 1.0
- E = 0
- P = 0
- # Indikator za pomoćne linije
- pomlinije = False
- formula = unos_formule()
- # Početne dimenzije grafičkog prozora
- width = 680
- height = 440 # + 50
- def update():
- pygame.display.flip()
- screen = pygame.display.set_mode((width, height + 50))
- # Ispis pozadine grafičkog prozora sa I kvadrantom
- def ispis_ekrana1():
- screen.fill((255, 255, 255))
- pygame.draw.line(screen, (0, 0, 0), (82, 8), (82, height + 13), 3)
- pygame.draw.line(screen, (0, 0, 0), (72, height), (width - 25, height), 3)
- pygame.draw.line(screen, (0, 0, 0), (82, 8), (85, 23), 3)
- pygame.draw.line(screen, (0, 0, 0), (82, 8), (79, 23), 3)
- pygame.draw.line(screen, (0, 0, 0), (width - 25, height), (width - 40, height + 3), 3)
- pygame.draw.line(screen, (0, 0, 0), (width - 25, height), (width - 40, height - 3), 3)
- textsurfacey = myfont.render('y', False, (0, 0, 0))
- screen.blit(textsurfacey, (60, 11))
- textsurfacex = myfont.render('x', False, (0, 0, 0))
- screen.blit(textsurfacex, (width - 45, height + 7))
- for obx in range(0, 999999, 70):
- pygame.draw.line(screen, (0, 0, 0), (obx + 82, height), (obx + 82, height + 13), 3)
- if obx != 0 and pomlinije is True:
- pygame.draw.line(screen, (198, 198, 198), (obx + 82, height - 2), (obx + 82, 25), 1)
- pom = round(float(obx * C / 100 + P), 2)
- if pom == -0.00:
- pom = 0.00
- textsurfaceobx = myfont.render(str(pom), False, (0, 0, 0))
- screen.blit(textsurfaceobx, (obx + 64, height + 16))
- if obx + 82 > width - 130:
- break
- for oby in range(0, 99999, 50):
- pygame.draw.line(screen, (0, 0, 0), (72, height - oby), (82, height - oby), 3)
- if oby != 0 and pomlinije is True:
- pygame.draw.line(screen, (198, 198, 198), (84, height - oby), (width - 40, height - oby), 1)
- pom2 = round(float((oby + E) / abs(B) / 100), 2)
- if pom2 == -0.00:
- pom2 = 0.00
- textsurfaceoby = myfont.render(str(pom2), False, (0, 0, 0))
- screen.blit(textsurfaceoby, (10, height - 7 - oby))
- if height - oby < 90:
- break
- update()
- # Ispis pozadine grafičkog prozora sa I i IV kvadrantom
- def ispis_ekrana2():
- screen.fill((255, 255, 255))
- pygame.draw.line(screen, (0, 0, 0), (82, 8), (82, height + 13), 3)
- pygame.draw.line(screen, (0, 0, 0), (72, (height + 40) / 2), (width - 25, (height + 40) / 2), 3)
- pygame.draw.line(screen, (0, 0, 0), (82, 8), (85, 23), 3)
- pygame.draw.line(screen, (0, 0, 0), (82, 8), (79, 23), 3)
- pygame.draw.line(screen, (0, 0, 0), (width - 25, (height + 40) / 2), (width - 40, (height + 40) / 2 + 3), 3)
- pygame.draw.line(screen, (0, 0, 0), (width - 25, (height + 40) / 2), (width - 40, (height + 40) / 2 - 3), 3)
- textsurfacey = myfont.render('y', False, (0, 0, 0))
- screen.blit(textsurfacey, (60, 11))
- textsurfacex = myfont.render('x', False, (0, 0, 0))
- screen.blit(textsurfacex, (width - 45, (height + 40) / 2 + 7))
- for obx in range(0, 999999, 70):
- if obx != 0 and pomlinije is True:
- pygame.draw.line(screen, (198, 198, 198), (obx + 82, height - 2), (obx + 82, 25), 1)
- pygame.draw.line(screen, (0, 0, 0), (obx + 82, (height + 40) / 2), (obx + 82, (height + 40) / 2 + 13), 3)
- pom = round(float(obx * C / 100 + P), 2)
- if pom == -0.00:
- pom = 0.00
- textsurfaceobx = myfont.render(str(pom), False, (0, 0, 0))
- if obx != 0:
- screen.blit(textsurfaceobx, (obx + 64, (height + 40) / 2 + 16))
- else:
- screen.blit(textsurfaceobx, (obx + 64, height + 16))
- if obx + 82 > width - 130:
- break
- for oby in range(0, 99999, 50):
- pygame.draw.line(screen, (0, 0, 0), (72, (height + 40) / 2 - oby), (82, (height + 40) / 2 - oby), 3)
- if oby != 0 and pomlinije is True:
- pygame.draw.line(screen, (198, 198, 198), (84, (height + 40) / 2 - oby),
- (width - 40, (height + 40) / 2 - oby), 1)
- pom2 = round(float((oby + E) / abs(B) / 100), 2)
- if pom2 == -0.00:
- pom2 = 0.00
- textsurfaceoby = myfont.render(str(pom2), False, (0, 0, 0))
- screen.blit(textsurfaceoby, (10, (height + 40) / 2 - 7 - oby))
- if (height + 40) / 2 - oby < 90:
- break
- for oby in range(0, 99999, 50):
- pygame.draw.line(screen, (0, 0, 0), (72, (height + 40) / 2 + oby), (82, (height + 40) / 2 + oby), 3)
- if oby != 0 and pomlinije is True:
- pygame.draw.line(screen, (198, 198, 198), (84, (height + 40) / 2 + oby),
- (width - 40, (height + 40) / 2 + oby), 1)
- pom2 = round(float((oby + E) / abs(B) / 100), 2)
- if pom2 == -0.00:
- pom2 = 0.00
- textsurfaceoby = myfont.render(str(pom2), False, (0, 0, 0))
- if oby != 0:
- screen.blit(textsurfaceoby, (10, (height + 40) / 2 - 7 + oby))
- if (height + 40) / 2 + oby > height - 50:
- break
- update()
- def napravi_prozor():
- pygame.display.set_mode((width, height + 50))
- ispis_ekrana1()
- finish = True
- update()
- while finish:
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- finish = False
- # Unos komandi koje detektuju samo jedan klik
- if event.type == pygame.KEYDOWN and event.key == pygame.K_q:
- formula = unos_formule()
- if A == height:
- ispis_ekrana1()
- else:
- ispis_ekrana2()
- if event.type == pygame.KEYDOWN and event.key == pygame.K_g:
- if pomlinije is False:
- pomlinije = True
- else:
- pomlinije = False
- if A == height:
- ispis_ekrana1()
- else:
- ispis_ekrana2()
- if event.type == pygame.KEYDOWN and event.key == pygame.K_1 and A == (height + 40) / 2:
- A = height
- ispis_ekrana1()
- if event.type == pygame.KEYDOWN and event.key == pygame.K_2 and A == height:
- A = (height + 40) / 2
- ispis_ekrana2()
- # Unos komandi koje traju
- keys_pressed = pygame.key.get_pressed()
- if keys_pressed[pygame.K_r]:
- if height > 140:
- if A == height:
- height -= 10
- A = height
- napravi_prozor()
- ispis_ekrana1()
- else:
- height -= 10
- A = (height + 40) / 2
- napravi_prozor()
- ispis_ekrana2()
- if keys_pressed[pygame.K_f]:
- if A == height:
- height += 10
- A = height
- napravi_prozor()
- ispis_ekrana1()
- else:
- height += 10
- A = (height + 40) / 2
- napravi_prozor()
- ispis_ekrana2()
- if keys_pressed[pygame.K_e]:
- if width > 290:
- width -= 10
- napravi_prozor()
- if A == height:
- ispis_ekrana1()
- else:
- ispis_ekrana2()
- if keys_pressed[pygame.K_t]:
- width += 10
- napravi_prozor()
- if A == height:
- ispis_ekrana1()
- else:
- ispis_ekrana2()
- if keys_pressed[pygame.K_UP]:
- E -= 0.8
- if A == height:
- ispis_ekrana1()
- else:
- ispis_ekrana2()
- if keys_pressed[pygame.K_DOWN]:
- E += 0.8
- if A == height:
- ispis_ekrana1()
- else:
- ispis_ekrana2()
- if keys_pressed[pygame.K_LEFT]:
- P += 0.02
- if A == height:
- ispis_ekrana1()
- else:
- ispis_ekrana2()
- if keys_pressed[pygame.K_RIGHT]:
- P -= 0.02
- if A == height:
- ispis_ekrana1()
- else:
- ispis_ekrana2()
- if keys_pressed[pygame.K_a]:
- C *= 1.005
- if A == height:
- ispis_ekrana1()
- else:
- ispis_ekrana2()
- if keys_pressed[pygame.K_d]:
- C /= 1.005
- if A == height:
- ispis_ekrana1()
- else:
- ispis_ekrana2()
- if keys_pressed[pygame.K_w]:
- if B > -0.02:
- if B <= 0:
- B = 0.02
- else:
- B += 0.015
- else:
- B += 0.015
- if A == height:
- ispis_ekrana1()
- else:
- ispis_ekrana2()
- if keys_pressed[pygame.K_s]:
- if B < 0.02:
- if B >= 0:
- B = -0.02
- else:
- B -= 0.015
- else:
- B -= 0.015
- if A == height:
- ispis_ekrana1()
- else:
- ispis_ekrana2()
- # Ispis linije funkcije
- x = 0
- y = int(A - 2 - B * 100 * function(C * (float(x) / 100) + P) + E)
- for x2 in range(0, 2000):
- if x2 < width - 122:
- y2 = int(A - 2 - B * 100 * function(C * (float(x2) / 100) + P) + E)
- if y2 < 25:
- y2 = 25
- if y2 > height:
- y2 = height
- if x2 < width - 122 and x < width - 122 and (y > 25 or y2 > 25) and (y < height or y2 < height):
- if x2 == 0:
- pygame.draw.line(screen, (0, 0, 0), (x + 84, y), (x2 + 84, y2), 2)
- else:
- pygame.draw.line(screen, (237, 0, 0), (x + 84, y), (x2 + 84, y2), 2)
- x = x2
- y = y2
- else:
- x = x2
- y = y2
- continue
- else:
- break
- update()
- # 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