Advertisement
CosmicFox33

chicha v1.1

Apr 4th, 2022
848
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.74 KB | None | 0 0
  1. import matplotlib.pyplot as plt
  2. import matplotlib.patches
  3. import matplotlib.path
  4. import numpy as np
  5. import math
  6.  
  7. class Line:
  8.     def __init__(self, x1, y1, x2, y2):
  9.         if x2<x1:
  10.             self._x1=x2
  11.             self._y1=y2
  12.             self._x2=x1
  13.             self._y2=y1
  14.         else:
  15.             self._x1=x1
  16.             self._y1=y1
  17.             self._x2=x2
  18.             self._y2=y2
  19.         self.xi=0
  20.         self.yi=0
  21.         self.xj=0
  22.         self.yj=0
  23.        
  24.     def get_A(self):
  25.         return self._y2-self._y1
  26.  
  27.     def get_B(self):
  28.         return self._x1-self._x2
  29.  
  30.     def get_C(self):
  31.         return self._x2*self._y1-self._x1*self._y2
  32.  
  33.     def info(self):
  34.         print(self.get_A(), self.get_B(), self.get_C())
  35.    
  36.     def show(self):
  37.         x=[self._x1,self._x2]
  38.         y=[self._y1,self._y2]
  39.         plt.plot(x, y)
  40.        
  41.  
  42.     def angle(self):
  43.         return math.atan(abs(self._y2-self._y1)/abs(self._x2-self._x1))
  44.  
  45.     def check_a(self):
  46.         return (self._y2-self._y1)/(self._x2-self._x1)
  47.    
  48.     def check_ins(self, circle):
  49.         if (self._x1<=circle.xi<=self._x2 and self._y1<=circle.yi<=self._y2 or self._x1<=circle.xj<=self._x2 and self._y1<=circle.yj<=self._y2) or self.check_a()<0 and (self._x1<=circle.xi<=self._x2 and self._y2<=circle.yi<=self._y1 or self._x1<=circle.xj<=self._x2 and self._y2<=circle.yj<=self._y1):
  50.             return 1
  51.         else:
  52.             return 0
  53.  
  54. class Circle:
  55.     def __init__(self, x, y, r):
  56.         self._x=x
  57.         self._y=y
  58.         self._r=r
  59.    
  60.     def show(self):
  61.         axes = plt.gca()
  62.         axes.set_aspect("equal")
  63.         cc = matplotlib.patches.Circle((self._x,self._y),self._r, fill=0)
  64.         axes.add_patch(cc)
  65.  
  66.     def dist(self, line):
  67.         a=line.get_A()
  68.         b=line.get_B()
  69.         c=line.get_C()
  70.         return abs(a*self._x+b*self._y+c)/((a**2+b**2)**(1/2))
  71.    
  72.     def intersect(self, line):
  73.         A=line.get_A()
  74.         B=line.get_B()
  75.         C=line.get_C()        
  76.         ys=(B*C+A*B*self._x-A**2*self._y)/(-A**2-B**2)
  77.         xs=-(B*ys+C)/A
  78.         a=line.angle()
  79.         l=(self._r**2-self.dist(line)**2)**(1/2)
  80.         lx=l*math.cos(a)
  81.         ly=l*math.sin(a)
  82.         if line.check_a()>0:
  83.             self.xi=xs-lx
  84.             self.yi=ys-ly
  85.             self.xj=xs+lx
  86.             self.yj=ys+ly
  87.         else:
  88.             self.xi=xs-lx
  89.             self.yi=ys+ly
  90.             self.xj=xs+lx
  91.             self.yj=ys-ly
  92.         if line.check_a()>0 and line.check_ins(self):
  93.             return 1
  94.         else:
  95.             return 0
  96.        
  97.     def check(self, line):
  98.         if self.dist(line)>self._r or not self.intersect(line):
  99.             return 0
  100.         else:
  101.             return 1
  102.  
  103. L=Line(1.,1.,4.,3.)
  104. C=Circle(2., 0., 1.5)
  105. L.show()
  106. C.show()
  107. plt.show()
  108. print(C.dist(L))
  109. print(C.check(L))
  110. L.info()
  111.  
  112.  
  113. x1=float(input())
  114. y1=float(input())
  115. x2=float(input())
  116. y2=float(input())
  117. x0=float(input())
  118. y0=float(input())
  119. r=float(input())
  120. x=[x1,x2]
  121. y=[y1,y2]
  122. axes = plt.gca()
  123. axes.set_aspect("equal")
  124. cc = matplotlib.patches.Circle((x0,y0),r, fill=0)
  125. axes.add_patch(cc)
  126. plt.plot(x, y)
  127. plt.show()
  128.  
  129. if x1==x2 and y1!=y2:
  130.     h=abs(x1-x0)
  131.     if h>r:
  132.         print("пересечений нет 1")
  133.     else:
  134.         Y1=y0+(r**2-h**2)**(1/2)
  135.         Y2=y0-(r**2-h**2)**(1/2)
  136.         if y2<y1:
  137.             y1, y2=y2, y1
  138.         if not (y1<=Y1<=y2 or y1<=Y2<=y2):
  139.             print("пересечений нет 2")
  140.         else:
  141.             print("пересечение есть")
  142. elif x1!=x2 and y1==y2:
  143.     h=abs(y1-y0)
  144.     if h>r:
  145.         print("пересечений нет 1")
  146.     else:
  147.         X1=x0+(r**2-h**2)**(1/2)
  148.         X2=x0+(r**2-h**2)**(1/2)
  149.         if x2<x1:
  150.             x1,x2=x2,x1
  151.         if not (x1<=X1<=x2 or x1<=X2<=x2):
  152.             print("пересечений нет 2")
  153.         else:
  154.             print("пересечение есть")
  155. else:
  156.     if x2<x1:
  157.         x1,x2,y1,y2=x2,x1,y2,y1
  158.     A=y2-y1
  159.     B=x1-x2
  160.     C=x2*y1-x1*y2
  161.     h=abs(A*x0+B*y0+C)/((A**2+B**2)**(1/2))
  162.     if h>r:
  163.         print("пересечений нет 1")
  164.     ys=(B*C+A*B*x0-A**2*y0)/(-A**2-B**2)
  165.     xs=-(B*ys+C)/A
  166.     l=(r**2-h**2)**(1/2)
  167.     a=math.atan(abs(y2-y1)/abs(x2-x1))
  168.     lx=l*math.cos(a)
  169.     ly=l*math.sin(a)
  170.     ok=(y2-y1)/(x2-x1)
  171.     if ok>0:
  172.         xi=xs-lx
  173.         yi=ys-ly
  174.         xj=xs+lx
  175.         yj=ys+ly
  176.     else:
  177.         xi=xs-lx
  178.         yi=ys+ly
  179.         xj=xs+lx
  180.         yj=ys-ly
  181.     if ok>0 and (x1<=xi<=x2 and y1<=yi<=y2 or x1<=xj<=x2 and y1<=yj<=y2) or ok<0 and (x1<=xi<=x2 and y2<=yi<=y1 or x1<=xj<=x2 and y2<=yj<=y1):
  182.         print("пересечение есть")
  183.     else:
  184.         print("пересечений нет 2")
  185.    
  186.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement