Добавил:
chrysler_a57_mltbnk
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз:
Предмет:
Файл:семестр 1 / lab10
.pyclass Triangle:
def __init__(self, A, B, C):
check_a = False
check_b = False
check_c = False
try:
float(A[0])
float(A[1])
float(B[0])
float(B[1])
except:
check_a = True
try:
float(B[0])
float(B[1])
float(C[0])
float(C[1])
except:
check_b = True
try:
float(C[0])
float(C[1])
float(A[0])
float(A[1])
except:
check_c = True
if check_a == True:
print("Invalid input data: one of the points is not a vector.")
elif check_a == False:
self.a = (((B[0] - A[0]) **2 + (B[1] - A[1]) **2)) **0.5
if check_b == True:
print("Invalid input data: one of the points is not a vector.")
elif check_b == False:
self.b = (((C[0] - B[0]) **2 + (C[1] - B[1]) **2)) **0.5
if check_c == True:
print("Invalid input data: one of the points is not a vector.")
elif check_c == False:
self.c = (((A[0] - C[0]) **2 + (A[1] - C[1]) **2)) **0.5
if self.a > 0 and self.b > 0 and self.c > 0:
self.cr = True
else:
self.cr = False
if self.a < (self.b + self.c) and self.b < (self.c + self.a) and self.c < (self.a + self.b):
self.cr = True
else:
self.cr = False
if self.cr == True:
self.halfperimeter = (self.a + self.b + self.c) / 2
self.square = (self.halfperimeter * (self.halfperimeter - self.a) * (self.halfperimeter - self.b) * (self.halfperimeter - self.c))**0.5
print("Triangle creation was successful.", chr(10))
elif self.cr == False:
print("Tringle creation failed.", chr(10))
def per(self):
if self.cr == True:
self.perimeter = self.halfperimeter * 2
print(f'Perimeter of this triangle is {self.perimeter}. {chr(10)}')
else:
print("Perimeter calculation failed: this triangle does not exists.", chr(10))
def sq(self):
if self.cr == True:
self.square = (self.halfperimeter * (self.halfperimeter - self.a) * (self.halfperimeter - self.b) * (self.halfperimeter - self.c))**0.5
print(f'Square of this triangle is {self.square}. {chr(10)}')
else:
print("Square calculation failed: this triangle does not exists.", chr(10))
def md(self):
if self.cr == True:
self.median_M1 = ((2 * (self.a**2 + self.c**2) - self.b**2)**0.5 / 2) # to BC
self.median_M2 = ((2 * (self.a**2 + self.b**2) - self.c**2)**0.5 / 2) # to CA
self.median_M3 = ((2 * (self.c**2 + self.b**2) - self.a**2)**0.5 / 2) # to AB
print(f'Median M1 to side «b» is {self.median_M1}.')
print(f'Median M2 to side «c» is {self.median_M2}.')
print(f'Median M3 to side «a» is {self.median_M3}. {chr(10)}')
else:
print("Medians calculation failed: this triangle does not exists.", chr(10))
def ht(self):
if self.cr == True:
self.hight_H1 = 2 * self.square / self.b # to BC
self.hight_H2 = 2 * self.square / self.c # to CA
self.hight_H3 = 2 * self.square / self.a # to AB
print(f'Hight H1 to side «b» is {self.hight_H1}.')
print(f'Hight H2 to side «c» is {self.hight_H2}.')
print(f'Hight H3 to side «a» is {self.hight_H3}. {chr(10)}')
else:
print("Hights calculation failed: this triangle does not exists.", chr(10))
def sdeq(self):
if self.cr == True:
if self.a == self.b == self.c:
print(f'This triangle is equal-sided because side «a» is equal to sides «b» and «c». {chr(10)}')
elif self.a == self.b:
print(f'This triangle is isosceles because side «a» is equal to sides «b». {chr(10)}')
elif self.b == self.c:
print(f'This triangle is isosceles because side «b» is equal to sides «c». {chr(10)}')
elif self.c == self.a:
print(f'This triangle is isosceles because side «c» is equal to sides «a». {chr(10)}')
else:
print(f'This is general triangle.')
else:
print("Sides comparison failed: this triangle does not exists.", chr(10))
def __eq__(self,other):
if self.a == other.AB and self.b == other.BC and self.c == other.CA:
return True
else:
return False
def pr(self):
if self.cr == True:
print(f'This is triangle with sides {self.a}, {self.b}, {self.c}. {chr(10)}')
else:
print("Output failed: this triangle does not exists.", chr(10))
ABC = Triangle([5,35],[12,-2],[6,26])
ABC.pr()
ABC.md()
ABC.ht()
Соседние файлы в папке семестр 1
