Advertisement
CosmicFox33

chicha v1.6

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