Advertisement
CosmicFox33

chicha v1.0

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