Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sys
- import math
- from PyQt5.QtCore import *
- from PyQt5.QtGui import *
- from PyQt5.QtWidgets import *
- class Mainwindow(QMainWindow):
- def __init__(self):
- super().__init__()
- self.setWindowTitle("Funkcja kwadratowa")
- self.label = QLabel(self)
- self.canvas = QPixmap(600, 600)
- self.canvas.fill((QColor(Qt.lightGray)))
- self.label.setPixmap(self.canvas)
- self.label.setGeometry(0, 100, 600, 600)
- self.update()
- self.label1 = QLabel("pierwiastki rownania y=a*x2+b*x+c", self)
- self.label1.setFont(QFont("Calibri", 16))
- self.label1.setGeometry(620, 100, 550, 40)
- self.label2 = QLabel("a=", self)
- self.label2.setFont(QFont("Calibri", 16))
- self.label2.setGeometry(620, 200, 550, 30)
- self.label3 = QLabel("b=", self)
- self.label3.setFont(QFont("Calibri", 16))
- self.label3.setGeometry(620, 250, 550, 30)
- self.label4 = QLabel("c=", self)
- self.label4.setFont(QFont("Calibri", 16))
- self.label4.setGeometry(620, 300, 550, 30)
- validator = QDoubleValidator(-999.00, -999.00, 2)
- validator.setLocale(QLocale("en_US"))
- self.a_value = QLineEdit("2", self)
- self.a_value.setFont(QFont("Calibri", 16))
- self.a_value.setGeometry(700, 200, 400, 30)
- self.a_value.setValidator(validator)
- self.b_value = QLineEdit("4", self)
- self.b_value.setFont(QFont("Calibri", 16))
- self.b_value.setGeometry(700, 250, 400, 30)
- self.b_value.setValidator(validator)
- self.c_value = QLineEdit("1", self)
- self.c_value.setFont(QFont("Calibri", 16))
- self.c_value.setGeometry(700, 300, 400, 30)
- self.c_value.setValidator(validator)
- self.label5 = QLabel("Pierwiastki rownanie", self)
- self.label5.setFont(QFont("Calibri", 16))
- self.label5.setGeometry(620, 400, 550, 40)
- self.x1_label = QLabel("x1=", self)
- self.x1_label.setFont(QFont("Calibri", 16))
- self.x1_label.setGeometry(620, 450, 50, 30)
- self.x2_label = QLabel("x2=", self)
- self.x2_label.setFont(QFont("Calibri", 16))
- self.x2_label.setGeometry(620, 500, 50, 30)
- self.x1_value = QLineEdit("", self)
- self.x1_value.setFont(QFont("Calibri", 16))
- self.x1_value.setGeometry(700, 450, 400, 30)
- self.x2_value = QLineEdit("", self)
- self.x2_value.setFont(QFont("Calibri", 16))
- self.x2_value.setGeometry(700, 500, 400, 30)
- self.xp_value = QLineEdit("-10", self)
- self.xp_value.setFont(QFont("Calibri", 16))
- self.xp_value.setGeometry(50, 660, 100, 30)
- self.xp_value.setValidator(validator)
- self.xk_value = QLineEdit("10", self)
- self.xk_value.setFont(QFont("Calibri", 16))
- self.xk_value.setGeometry(450, 660, 100, 30)
- self.xk_value.setValidator(validator)
- self.yk_value = QLabel("", self)
- self.yk_value.setFont(QFont("Calibri", 16))
- self.yk_value.setGeometry(0, 160, 100, 30)
- self.yp_value = QLabel("", self)
- self.yp_value.setFont(QFont("Calibri", 16))
- self.yp_value.setGeometry(0, 620, 100, 30)
- self.gora_button = QPushButton("oblicz", self)
- self.gora_button.setGeometry(620, 550, 150, 150)
- self.gora_button.clicked.connect(self.oblicz)
- self.gora_button = QPushButton("rysuj", self)
- self.gora_button.setGeometry(790, 550, 150, 150)
- self.gora_button.clicked.connect(self.rysuj)
- self.show()
- def oblicz(self):
- print("oblicz")
- a = float(self.a_value.text())
- b = float(self.b_value.text())
- c = float(self.c_value.text())
- delta = b * b - (4 * a * c)
- print(delta)
- if (delta < 0):
- self.x1_value.setText("brak")
- self.x2_value.setText("brak")
- else:
- pdelta = math.sqrt(delta)
- x1 = (-b - pdelta) / (2 * a)
- x2 = (-b + pdelta) / (2 * a)
- self.x1_value.setText(str(x1))
- self.x2_value.setText(str(x2))
- def rysuj(self):
- print("rysuj")
- y=[]
- yint=[]
- a = float(self.a_value.text())
- b = float(self.b_value.text())
- c = float(self.c_value.text())
- painter = QPainter(self.label.pixmap())
- painter.setPen(QPen(Qt.black, 2, Qt.SolidLine))
- painter.fillRect(0, 0, 600, 600, QColor(Qt.lightGray))
- painter.drawLine(50, 50, 50, 550)
- painter.drawLine(50, 550, 550, 550)
- painter.drawLine(50, 50, 40, 60)
- painter.drawLine(50, 50, 60, 60)
- painter.drawText(20, 50, "Y")
- painter.drawLine(550, 550, 540, 540)
- painter.drawLine(550, 550, 540, 560)
- painter.drawText(560, 560, "X")
- xp = float(self.xp_value.text())
- xk = float(self.xk_value.text())
- print(f"xp, xk = {xp}, {xk}")
- step = (xk-xp)/500
- print(f"step = {step}")
- x=xp
- for i in range(0, 501):
- y.append(a*x*x + b*x + c)
- tmp = x
- x = x + step
- print(f"step, y = {step}, {y[i]}")
- ymin = y[0]
- ymax = y[0]
- for i in range(0, 501):
- if y[i] > ymax:
- ymax = y[i]
- if y[i] < ymin:
- ymin = y[i]
- print(f"ymin, ymax = {ymin}, {ymax}")
- self.yp_value.setText(str(ymin))
- self.yk_value.setText(str(ymax))
- for i in range(0, 501):
- yint.append(int(50+500*(ymax-y[i])/(ymax-ymin)))
- print(yint)
- painter.setPen(QPen(Qt.darkBlue, 3, Qt.SolidLine))
- xi = 50
- for i in range(0, 500):
- painter.drawLine(xi, yint[i], xi+1, yint[i+1])
- xi = xi+1
- self.update()
- app = QApplication(sys.argv)
- window = Mainwindow()
- window.resize(1200, 800)
- sys.exit(app.exec())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement