Advertisement
CosmicFox33

chicha v1.5

Apr 7th, 2022
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.26 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. 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.xj<=max(self._x1,self._x2)):
  69. return 1
  70. else:
  71. return 0
  72.  
  73. class Circle:
  74. def __init__(self, x=0., y=0., r=0.):
  75. self._x=x
  76. self._y=y
  77. self._r=r
  78.  
  79. def input(self):
  80. self._x=float(input())
  81. self._y=float(input())
  82. self._r=float(input())
  83.  
  84. def show(self):
  85. axes = plt.gca()
  86. axes.set_aspect("equal")
  87. cc = matplotlib.patches.Circle((self._x,self._y),self._r, fill=0)
  88. axes.add_patch(cc)
  89.  
  90. def dist(self, line):
  91. a=line.get_A()
  92. b=line.get_B()
  93. c=line.get_C()
  94. if a==0:
  95. return abs(self._y+c/b)
  96. elif b==0:
  97. return abs(self._x+c/a)
  98. return abs(a*self._x+b*self._y+c)/((a**2+b**2)**(1/2))
  99.  
  100. def intersect(self, line):
  101. A=line.get_A()
  102. B=line.get_B()
  103. C=line.get_C()
  104. l=(self._r**2-self.dist(line)**2)**(1/2)
  105. if A==0:
  106. if C:
  107. self.ys=-(B/C)
  108. else:
  109. self.ys=0.
  110. self.xs=self._x
  111. self.xi=self._x-l
  112. self.yi=self.ys
  113. self.xj=self._x+l
  114. self.yj=self.ys
  115. elif B==0:
  116. self.ys=self._y
  117. if C:
  118. self.xs=-(A/C)
  119. else:
  120. self.xs=0.
  121. self.xi=self.xs
  122. self.yi=self._y-l
  123. self.xj=self.xs
  124. self.yj=self._y+l
  125. else:
  126. a=line.angle()
  127. lx=l*math.cos(a)
  128. ly=l*math.sin(a)
  129. self.ys=(B*C+A*B*self._x-A**2*self._y)/(-A**2-B**2)
  130. self.xs=-(B*self.ys+C)/A
  131. if line.check_a()=='c':
  132. self.xi=self.xs-lx
  133. self.yi=self.ys-ly
  134. self.xj=self.xs+lx
  135. self.yj=self.ys+ly
  136. elif line.check_a()=='d':
  137. self.xi=self.xs-lx
  138. self.yi=self.ys+ly
  139. self.xj=self.xs+lx
  140. self.yj=self.ys-ly
  141. if line.check_ins(self):
  142. return 1
  143. else:
  144. return 0
  145.  
  146. def check(self, line):
  147. if self.dist(line)>self._r or not self.intersect(line):
  148. return 0
  149. else:
  150. return 1
  151.  
  152. class Polygon:
  153. def __init__(self):
  154. self._P=[]
  155. self._L=[]
  156.  
  157. def input(self):
  158. self._P=[[float(input()), float(input())] for i in range(int(input()))]
  159. l=len(self._P)
  160. for i in range(l):
  161. if i+1<l:
  162. self._L.append(Line(self._P[i][0], self._P[i][1], self._P[i+1][0], self._P[i+1][1]))
  163. else:
  164. self._L.append(Line(self._P[i][0], self._P[i][1], self._P[0][0], self._P[0][1]))
  165.  
  166. def check(self, circle):
  167. for i in self._L:
  168. if circle.check(i):
  169. return 1
  170. return 0
  171.  
  172. def show(self):
  173. for i in self._L:
  174. i.show()
  175. C=Circle()
  176. P=Polygon()
  177. C.input()
  178. P.input()
  179. print(P.check(C))
  180. P.show()
  181. C.show()
  182. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement