Advertisement
trds

classLinearEquation

Jan 30th, 2021
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.14 KB | None | 0 0
  1. class LinearEquation:
  2.     def __init__(self, a, b, c, d, e, f):
  3.         self.__a = a
  4.         self.__b = b
  5.         self.__c = c
  6.         self.__d = d
  7.         self.__e = e
  8.         self.__f = f
  9.  
  10.     def getA(self):
  11.         return self.__a
  12.  
  13.     def getB(self):
  14.         return self.__b
  15.  
  16.     def getC(self):
  17.         return self.__c
  18.  
  19.     def getD(self):
  20.         return self.__d
  21.  
  22.     def getE(self):
  23.         return self.__e
  24.  
  25.     def getF(self):
  26.         return self.__f
  27.  
  28.     # isSolvable()<=>ab-bc!=0
  29.  
  30.     def isSolvable(self):
  31.         if ((self.__a * self.__d - self.__b * self.__c) != 0):
  32.             return True
  33.         else:
  34.             return False
  35.  
  36.     def getX(self):
  37.         return ((self.__e * self.__d - self.__b * self.__f) / (self.__a * self.__d - self.__b * self.__c))
  38.  
  39.     def getY(self):
  40.         return ((self.__a * self.__f - self.__e * self.__c) / (self.__a * self.__d - self.__b * self.__c))
  41.  
  42.     def __repr__(self):
  43.         s1 = ("{}x + {} + {}y = {}".format(self.__a, self.__b, self.__e))
  44.         s2 = ("{}x + {} + {}y= {}".format(self.__c, self.__d, self.__f))
  45.         rezultat = s1 + "\n" + s2
  46.         return rezultat
  47.  
  48.  
  49. s1 = LinearEquation(2, 3, 1, 7, 5, 8)
  50. if s1.isSolvable():
  51.     x = s1.getX()
  52.     y = s1.getY()
  53.     print("Sistemul are solutia ({},{})".format(x, y))
  54.  
  55. else:
  56.     print("Sistemul nu are solutie")
  57.  
  58. s2 = LinearEquation(2, 3, 2, 3, 4, 1)
  59. if s2.isSolvable():
  60.     x = s2.getX()
  61.     y = s2.getY()
  62.     print("Sistemul are solutia ({},{})".format(x, y))
  63. else:
  64.     print("Sistemul nu are solutie")
  65.  
  66. import math
  67.  
  68.  
  69. class Punct:
  70.     def __init__(self, xx, yy):
  71.         self.x = xx
  72.         self.y = yy
  73.  
  74.     def __repr__(self):
  75.         return 'X este {} si Y este {}'.format(self.x, self.y)
  76.  
  77.     def __eq__(self, other):
  78.         if self.x == other.__xx and self.y == other.__yy:
  79.             return False
  80.         else:
  81.             return True
  82.  
  83.     def distanta(self, other):
  84.         dis = math.sqrt((self.x - other.__xx) ** 2 + (self.x - other.__yy) ** 2)
  85.         return dis
  86.  
  87.  
  88. class Segment:
  89.     def __init__(self, a, b):
  90.         self.__start = a
  91.         self.__stop = b
  92.  
  93.     def __repr__(self):
  94.         return 'Segmentul a are: {} iar segmentul b are: {}'.format(self.__start, self.__stop)
  95.  
  96.  
  97.     def intersectie (self, other):
  98.         a1 = 1 / (self.__stop.x - self.__start.x)
  99.         b1 = -1 / (self.__stop.y - self.__start.y)
  100.         t1 = self.__start.x / (self.__stop.x - self.__start.x)
  101.         t2 = (-self.__start.y) / (self.__stop.y - self.__start.y)
  102.         e1 = t1+t2
  103.  
  104.         a2 = 1 / (other.__stop.x - other.__start.x)
  105.         b2 = -1 / (other.__stop.y - other.__start.y)
  106.         t1 = (other.__start.x)/(other.__stop.x - other.__start.x)
  107.         t2 = (-other.__start.y)/(other.__stop.y - other.__start.y)
  108.         e2 = t1+t2
  109.  
  110.         s=LinearEquation (a1,b1,a2,b2,e1,e2)
  111.         if s.isSolvable():
  112.             x=s.getX()
  113.             y=s.getY()
  114.             p=Punct(x,y)
  115.             return p
  116.         return None
  117.  
  118. p1=Punct (2.0,2.0)
  119. p2=Punct(0,0)
  120. Segment1=Segment(p1,p2)
  121. p3=Punct(0,2.0)
  122. p4=Punct(2.0,0)
  123. Segment2=Segment(p3,p4)
  124. p=Segment1.intersectie(Segment2)
  125. print(p)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement