Advertisement
programusy

1

Apr 8th, 2024
12
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.96 KB | None | 0 0
  1. import sys
  2. import math
  3.  
  4. from PyQt5.QtCore import *
  5. from PyQt5.QtGui import *
  6. from PyQt5.QtWidgets import *
  7.  
  8.  
  9. class Mainwindow(QMainWindow):
  10. def __init__(self):
  11. super().__init__()
  12. self.setWindowTitle("Funkcja kwadratowa")
  13.  
  14. self.label = QLabel(self)
  15. self.canvas = QPixmap(600, 600)
  16. self.canvas.fill((QColor(Qt.lightGray)))
  17. self.label.setPixmap(self.canvas)
  18. self.label.setGeometry(0, 100, 600, 600)
  19. self.update()
  20.  
  21. self.label1 = QLabel("pierwiastki rownania y=a*x2+b*x+c", self)
  22. self.label1.setFont(QFont("Calibri", 16))
  23. self.label1.setGeometry(620, 100, 550, 40)
  24.  
  25. self.label2 = QLabel("a=", self)
  26. self.label2.setFont(QFont("Calibri", 16))
  27. self.label2.setGeometry(620, 200, 550, 30)
  28.  
  29. self.label3 = QLabel("b=", self)
  30. self.label3.setFont(QFont("Calibri", 16))
  31. self.label3.setGeometry(620, 250, 550, 30)
  32.  
  33. self.label4 = QLabel("c=", self)
  34. self.label4.setFont(QFont("Calibri", 16))
  35. self.label4.setGeometry(620, 300, 550, 30)
  36.  
  37. validator = QDoubleValidator(-999.00, -999.00, 2)
  38. validator.setLocale(QLocale("en_US"))
  39.  
  40. self.a_value = QLineEdit("2", self)
  41. self.a_value.setFont(QFont("Calibri", 16))
  42. self.a_value.setGeometry(700, 200, 400, 30)
  43. self.a_value.setValidator(validator)
  44.  
  45. self.b_value = QLineEdit("4", self)
  46. self.b_value.setFont(QFont("Calibri", 16))
  47. self.b_value.setGeometry(700, 250, 400, 30)
  48. self.b_value.setValidator(validator)
  49.  
  50. self.c_value = QLineEdit("1", self)
  51. self.c_value.setFont(QFont("Calibri", 16))
  52. self.c_value.setGeometry(700, 300, 400, 30)
  53. self.c_value.setValidator(validator)
  54.  
  55. self.label5 = QLabel("Pierwiastki rownanie", self)
  56. self.label5.setFont(QFont("Calibri", 16))
  57. self.label5.setGeometry(620, 400, 550, 40)
  58.  
  59. self.x1_label = QLabel("x1=", self)
  60. self.x1_label.setFont(QFont("Calibri", 16))
  61. self.x1_label.setGeometry(620, 450, 50, 30)
  62.  
  63. self.x2_label = QLabel("x2=", self)
  64. self.x2_label.setFont(QFont("Calibri", 16))
  65. self.x2_label.setGeometry(620, 500, 50, 30)
  66.  
  67. self.x1_value = QLineEdit("", self)
  68. self.x1_value.setFont(QFont("Calibri", 16))
  69. self.x1_value.setGeometry(700, 450, 400, 30)
  70.  
  71. self.x2_value = QLineEdit("", self)
  72. self.x2_value.setFont(QFont("Calibri", 16))
  73. self.x2_value.setGeometry(700, 500, 400, 30)
  74.  
  75. self.xp_value = QLineEdit("-10", self)
  76. self.xp_value.setFont(QFont("Calibri", 16))
  77. self.xp_value.setGeometry(50, 660, 100, 30)
  78. self.xp_value.setValidator(validator)
  79.  
  80. self.xk_value = QLineEdit("10", self)
  81. self.xk_value.setFont(QFont("Calibri", 16))
  82. self.xk_value.setGeometry(450, 660, 100, 30)
  83. self.xk_value.setValidator(validator)
  84.  
  85. self.yk_value = QLabel("", self)
  86. self.yk_value.setFont(QFont("Calibri", 16))
  87. self.yk_value.setGeometry(0, 160, 100, 30)
  88.  
  89. self.yp_value = QLabel("", self)
  90. self.yp_value.setFont(QFont("Calibri", 16))
  91. self.yp_value.setGeometry(0, 620, 100, 30)
  92.  
  93. self.gora_button = QPushButton("oblicz", self)
  94. self.gora_button.setGeometry(620, 550, 150, 150)
  95. self.gora_button.clicked.connect(self.oblicz)
  96.  
  97. self.gora_button = QPushButton("rysuj", self)
  98. self.gora_button.setGeometry(790, 550, 150, 150)
  99. self.gora_button.clicked.connect(self.rysuj)
  100. self.show()
  101.  
  102. def oblicz(self):
  103. print("oblicz")
  104.  
  105. a = float(self.a_value.text())
  106. b = float(self.b_value.text())
  107. c = float(self.c_value.text())
  108.  
  109. delta = b * b - (4 * a * c)
  110. print(delta)
  111.  
  112. if (delta < 0):
  113. self.x1_value.setText("brak")
  114. self.x2_value.setText("brak")
  115. else:
  116. pdelta = math.sqrt(delta)
  117. x1 = (-b - pdelta) / (2 * a)
  118. x2 = (-b + pdelta) / (2 * a)
  119.  
  120. self.x1_value.setText(str(x1))
  121. self.x2_value.setText(str(x2))
  122.  
  123. def rysuj(self):
  124. print("rysuj")
  125.  
  126. y=[]
  127. yint=[]
  128.  
  129. a = float(self.a_value.text())
  130. b = float(self.b_value.text())
  131. c = float(self.c_value.text())
  132.  
  133. painter = QPainter(self.label.pixmap())
  134. painter.setPen(QPen(Qt.black, 2, Qt.SolidLine))
  135. painter.fillRect(0, 0, 600, 600, QColor(Qt.lightGray))
  136.  
  137. painter.drawLine(50, 50, 50, 550)
  138. painter.drawLine(50, 550, 550, 550)
  139. painter.drawLine(50, 50, 40, 60)
  140. painter.drawLine(50, 50, 60, 60)
  141. painter.drawText(20, 50, "Y")
  142. painter.drawLine(550, 550, 540, 540)
  143. painter.drawLine(550, 550, 540, 560)
  144. painter.drawText(560, 560, "X")
  145.  
  146. xp = float(self.xp_value.text())
  147. xk = float(self.xk_value.text())
  148.  
  149. print(f"xp, xk = {xp}, {xk}")
  150.  
  151. step = (xk-xp)/500
  152. print(f"step = {step}")
  153.  
  154. x=xp
  155. for i in range(0, 501):
  156. y.append(a*x*x + b*x + c)
  157. tmp = x
  158. x = x + step
  159. print(f"step, y = {step}, {y[i]}")
  160.  
  161. ymin = y[0]
  162. ymax = y[0]
  163.  
  164. for i in range(0, 501):
  165. if y[i] > ymax:
  166. ymax = y[i]
  167. if y[i] < ymin:
  168. ymin = y[i]
  169. print(f"ymin, ymax = {ymin}, {ymax}")
  170.  
  171. self.yp_value.setText(str(ymin))
  172. self.yk_value.setText(str(ymax))
  173.  
  174. for i in range(0, 501):
  175. yint.append(int(50+500*(ymax-y[i])/(ymax-ymin)))
  176. print(yint)
  177.  
  178. painter.setPen(QPen(Qt.darkBlue, 3, Qt.SolidLine))
  179. xi = 50
  180. for i in range(0, 500):
  181. painter.drawLine(xi, yint[i], xi+1, yint[i+1])
  182. xi = xi+1
  183.  
  184. self.update()
  185.  
  186.  
  187. app = QApplication(sys.argv)
  188. window = Mainwindow()
  189. window.resize(1200, 800)
  190. sys.exit(app.exec())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement