Advertisement
CosmicFox33

chicha v1.4

Apr 6th, 2022
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.84 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. if self._x2-self._x1:
  44. return math.atan(abs(self._y2-self._y1)/abs(self._x2-self._x1))
  45. else:
  46. return math.pi/2
  47.  
  48. def check_a(self):
  49. if self._x2-self._x1:
  50. return (self._y2-self._y1)/(self._x2-self._x1)
  51. else:
  52. return 1
  53.  
  54. def check_ins(self, circle):
  55. 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):
  56. return 1
  57. else:
  58. return 0
  59.  
  60. class Circle:
  61. def __init__(self, x=0., y=0., r=0.):
  62. self._x=x
  63. self._y=y
  64. self._r=r
  65.  
  66. def input(self):
  67. self._x=float(input())
  68. self._y=float(input())
  69. self._r=float(input())
  70.  
  71. def show(self):
  72. axes = plt.gca()
  73. axes.set_aspect("equal")
  74. cc = matplotlib.patches.Circle((self._x,self._y),self._r, fill=0)
  75. axes.add_patch(cc)
  76.  
  77. def dist(self, line):
  78. a=line.get_A()
  79. b=line.get_B()
  80. c=line.get_C()
  81. return abs(a*self._x+b*self._y+c)/((a**2+b**2)**(1/2))
  82.  
  83. def intersect(self, line):
  84. A=line.get_A()
  85. B=line.get_B()
  86. C=line.get_C()
  87. ys=(B*C+A*B*self._x-A**2*self._y)/(-A**2-B**2)
  88. if A:
  89. xs=-(B*ys+C)/A
  90. else:
  91. xs=self._x
  92. a=line.angle()
  93. l=(self._r**2-self.dist(line)**2)**(1/2)
  94. lx=l*math.cos(a)
  95. ly=l*math.sin(a)
  96. if line.check_a()>0:
  97. self.xi=xs-lx
  98. self.yi=ys-ly
  99. self.xj=xs+lx
  100. self.yj=ys+ly
  101. else:
  102. self.xi=xs-lx
  103. self.yi=ys+ly
  104. self.xj=xs+lx
  105. self.yj=ys-ly
  106. if line.check_a()>0 and line.check_ins(self):
  107. return 1
  108. else:
  109. return 0
  110.  
  111. def check(self, line):
  112. if self.dist(line)>self._r or not self.intersect(line):
  113. return 0
  114. else:
  115. return 1
  116.  
  117. class Polygon:
  118. def __init__(self):
  119. self._P=[]
  120. self._L=[]
  121.  
  122. def input(self):
  123. self._P=[[float(input()), float(input())] for i in range(int(input()))]
  124. l=len(self._P)
  125. for i in range(l):
  126. if i+1<l:
  127. self._L.append(Line(self._P[i][0], self._P[i][1], self._P[i+1][0], self._P[i+1][1]))
  128. else:
  129. self._L.append(Line(self._P[i][0], self._P[i][1], self._P[0][0], self._P[0][1]))
  130.  
  131. def check(self, circle):
  132. for i in self._L:
  133. if circle.check(i):
  134. return 1
  135. return 0
  136.  
  137. def show(self):
  138. for i in self._L:
  139. i.show()
  140. C=Circle()
  141. P=Polygon()
  142. C.input()
  143. P.input()
  144. print(P.check(C))
  145. P.show()
  146. C.show()
  147. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement