Zadanie projektowe 2 - kalkulator brył
unknown
python
2 years ago
11 kB
5
Indexable
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QRadioButton, QLabel, QDoubleSpinBox, QPushButton, \
QLineEdit, QGroupBox, QFormLayout
from PyQt5.QtCore import Qt
import math
class GeometryCalculatorApp(QWidget):
def __init__(self):
super().__init__()
self.init_ui()
def init_ui(self):
self.setWindowTitle('Kalkulator Geometryczny')
self.bryla_group = QGroupBox('Wybierz bryłę:')
self.bryla_layout = QVBoxLayout(self.bryla_group)
self.bryla_buttons = {
'Kula': QRadioButton('Kula'),
'Czworościan': QRadioButton('Czworościan'),
'Ostrosłup prosty': QRadioButton('Ostrosłup prosty'),
'Stożek': QRadioButton('Stożek'),
'Walec': QRadioButton('Walec'),
'Elipsoida': QRadioButton('Elipsoida'),
}
for button in self.bryla_buttons.values():
self.bryla_layout.addWidget(button)
self.obliczenie_group = QGroupBox('Wybierz obliczenie:')
self.obliczenie_layout = QVBoxLayout(self.obliczenie_group)
self.obliczenie_buttons = {
'Objętość': QRadioButton('Objętość'),
'Masa': QRadioButton('Masa'),
'Pole powierzchni': QRadioButton('Pole powierzchni'),
}
for button in self.obliczenie_buttons.values():
self.obliczenie_layout.addWidget(button)
self.parametry_group = QGroupBox('Podaj parametry:')
self.parametry_layout = QFormLayout(self.parametry_group)
self.parametry_input_1 = QDoubleSpinBox()
self.parametry_input_1.setRange(0, 100000)
self.parametry_input_1.setDecimals(2)
self.parametry_input_1.setSingleStep(0.1)
self.parametry_input_2 = QDoubleSpinBox()
self.parametry_input_2.setRange(0, 100000)
self.parametry_input_2.setDecimals(2)
self.parametry_input_2.setSingleStep(0.1)
self.parametry_input_3 = QDoubleSpinBox()
self.parametry_input_3.setRange(0, 100000)
self.parametry_input_3.setDecimals(2)
self.parametry_input_3.setSingleStep(0.1)
self.parametry_input_gestosc = QDoubleSpinBox()
self.parametry_input_gestosc.setRange(0, 100000)
self.parametry_input_gestosc.setDecimals(2)
self.parametry_input_gestosc.setSingleStep(0.1)
self.parametry_layout.addRow('r/h:', self.parametry_input_1)
self.parametry_layout.addRow('a:', self.parametry_input_2)
self.parametry_layout.addRow('b:', self.parametry_input_3)
self.parametry_layout.addRow('d:', self.parametry_input_gestosc)
self.calculate_button = QPushButton('Oblicz')
self.wynik_label = QLabel('Wynik:')
self.wynik_label.setAlignment(Qt.AlignCenter)
self.wynik_output = QLineEdit()
self.wynik_output.setReadOnly(True)
layout = QVBoxLayout()
layout.addWidget(self.bryla_group)
layout.addWidget(self.obliczenie_group)
layout.addWidget(self.parametry_group)
layout.addWidget(self.calculate_button)
layout.addWidget(self.wynik_label)
layout.addWidget(self.wynik_output)
self.setLayout(layout)
self.calculate_button.clicked.connect(self.calculate)
self.show()
def calculate(self):
bryla = ''
for button, name in self.bryla_buttons.items():
if name.isChecked():
bryla = button
obliczenie = ''
for button, name in self.obliczenie_buttons.items():
if name.isChecked():
obliczenie = button
parametry = [
self.parametry_input_1.value(),
self.parametry_input_2.value(),
self.parametry_input_3.value(),
self.parametry_input_gestosc.value()
]
potrzebne_parametry = self.wybierz_potrzebne_parametry(bryla, obliczenie)
if bryla and obliczenie:
wynik = self.oblicz_geometrie(bryla, obliczenie, potrzebne_parametry)
self.wynik_output.setText(str(wynik))
def wybierz_potrzebne_parametry(self, bryla, obliczenie):
potrzebne_parametry = []
r = self.parametry_input_1.value()
a = self.parametry_input_2.value()
b = self.parametry_input_3.value()
h = self.parametry_input_1.value()
gestosc = self.parametry_input_gestosc.value()
if bryla == 'Kula':
if obliczenie == 'Objętość':
potrzebne_parametry = [r]
elif obliczenie == 'Masa':
potrzebne_parametry = [r, gestosc]
elif obliczenie == 'Pole powierzchni':
potrzebne_parametry = [r]
elif bryla == 'Czworościan':
if obliczenie == 'Objętość':
potrzebne_parametry = [a]
elif obliczenie == 'Masa':
potrzebne_parametry = [a, gestosc]
elif obliczenie == 'Pole powierzchni':
potrzebne_parametry = [a]
elif bryla == 'Ostrosłup prosty':
if obliczenie == 'Objętość':
potrzebne_parametry = [a, b, h]
elif obliczenie == 'Masa':
potrzebne_parametry = [a, b, h, gestosc]
elif obliczenie == 'Pole powierzchni':
potrzebne_parametry = [a, b, h]
elif bryla == 'Stożek':
if obliczenie == 'Objętość':
potrzebne_parametry = [r, h]
elif obliczenie == 'Masa':
potrzebne_parametry = [r, h, gestosc]
elif obliczenie == 'Pole powierzchni':
potrzebne_parametry = [r]
elif bryla == 'Walec':
if obliczenie == 'Objętość':
potrzebne_parametry = [r, h]
elif obliczenie == 'Masa':
potrzebne_parametry = [r, h, gestosc]
elif obliczenie == 'Pole powierzchni':
potrzebne_parametry = [r, h]
elif bryla == 'Elipsoida':
if obliczenie == 'Objętość':
potrzebne_parametry = [a, b]
elif obliczenie == 'Masa':
potrzebne_parametry = [a, b, gestosc]
elif obliczenie == 'Pole powierzchni':
potrzebne_parametry = [a, b]
return potrzebne_parametry
def oblicz_geometrie(self, bryla, wlasciwosc, parametry):
wynik = None
if bryla == 'Kula':
if wlasciwosc == 'Objętość':
wynik = self.objetosc_kuli(*parametry)
elif wlasciwosc == 'Masa':
wynik = self.masa_kuli(*parametry)
elif wlasciwosc == 'Pole powierzchni':
wynik = self.pole_powierzchni_kuli(*parametry)
elif bryla == 'Czworościan':
if wlasciwosc == 'Objętość':
wynik = self.objetosc_czworoscianu(*parametry)
elif wlasciwosc == 'Masa':
wynik = self.masa_czworoscianu(*parametry)
elif wlasciwosc == 'Pole powierzchni':
wynik = self.pole_powierzchni_czworoscianu(*parametry)
elif bryla == 'Ostrosłup prosty':
if wlasciwosc == 'Objętość':
wynik = self.objetosc_ostroslupa_prostego(*parametry)
elif wlasciwosc == 'Masa':
wynik = self.masa_ostroslupa_prostego(*parametry)
elif wlasciwosc == 'Pole powierzchni':
wynik = self.pole_powierzchni_ostroslupa_prostego(*parametry)
elif bryla == 'Stożek':
if wlasciwosc == 'Objętość':
wynik = self.objetosc_stozka(*parametry)
elif wlasciwosc == 'Masa':
wynik = self.masa_stozka(*parametry)
elif wlasciwosc == 'Pole powierzchni':
wynik = self.pole_powierzchni_stozka(*parametry)
elif bryla == 'Walec':
if wlasciwosc == 'Objętość':
wynik = self.objetosc_walca(*parametry)
elif wlasciwosc == 'Masa':
wynik = self.masa_walca(*parametry)
elif wlasciwosc == 'Pole powierzchni':
wynik = self.pole_powierzchni_walca(*parametry)
elif bryla == 'Elipsoida':
if wlasciwosc == 'Objętość':
wynik = self.objetosc_elipsoidy(*parametry)
elif wlasciwosc == 'Masa':
wynik = self.masa_elipsoidy(*parametry)
elif wlasciwosc == 'Pole powierzchni':
wynik = self.pole_powierzchni_elipsoidy(*parametry)
return wynik
def objetosc_kuli(self, r):
V = (4 / 3) * math.pi * r ** 3
return V
def masa_kuli(self, r, gestosc):
m = (4 / 3) * math.pi * r ** 3 * gestosc
return m
def pole_powierzchni_kuli(self, r):
P = 4 * math.pi * r ** 2
return P
def objetosc_czworoscianu(self, a):
V = (a ** 3) / 6
return V
def masa_czworoscianu(self, a, gestosc):
m = (a ** 3) / 6 * gestosc
return m
def pole_powierzchni_czworoscianu(self, a):
P = math.sqrt(3) * a ** 2
return P
def objetosc_ostroslupa_prostego(self, a, b, h):
V = (1 / 3) * a * b * h
return V
def masa_ostroslupa_prostego(self, a, b, h, gestosc):
m = (1 / 3) * a * b * h * gestosc
return m
def pole_powierzchni_ostroslupa_prostego(self, a, b, h):
p = math.sqrt((a / 2) ** 2 + h ** 2)
P = a * b + a * p + b * p
return P
def objetosc_stozka(self, r, h):
V = (1 / 3) * math.pi * r ** 2 * h
return V
def masa_stozka(self, r, h, gestosc):
m = (1 / 3) * math.pi * r ** 2 * h * gestosc
return m
def pole_powierzchni_stozka(self, r, l):
P = math.pi * r * (r + l)
return P
def objetosc_walca(self, r, h):
V = math.pi * r ** 2 * h
return V
def masa_walca(self, r, h, gestosc):
m = math.pi * r ** 2 * h * gestosc
return m
def pole_powierzchni_walca(self, r, h):
P = 2 * math.pi * r * (r + h)
return P
def objetosc_elipsoidy(self, a, b):
V = (4 / 3) * math.pi * a * b ** 2
return V
def masa_elipsoidy(self, a, b, gestosc):
m = (4 / 3) * math.pi * a * b ** 2 * gestosc
return m
def pole_powierzchni_elipsoidy(self, a, b):
s = math.sqrt(1 - (b ** 2 / a ** 2))
P = 2 * math.pi * b * (b + (a / s) * math.asin(s))
return P
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = GeometryCalculatorApp()
sys.exit(app.exec_())
Editor is loading...
Leave a Comment