Advertisement
CosmicFox33

chicha v1.2

Apr 4th, 2022
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.08 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, y, r):
  62. self._x=x
  63. self._y=y
  64. self._r=r
  65.  
  66. def show(self):
  67. axes = plt.gca()
  68. axes.set_aspect("equal")
  69. cc = matplotlib.patches.Circle((self._x,self._y),self._r, fill=0)
  70. axes.add_patch(cc)
  71.  
  72. def dist(self, line):
  73. a=line.get_A()
  74. b=line.get_B()
  75. c=line.get_C()
  76. return abs(a*self._x+b*self._y+c)/((a**2+b**2)**(1/2))
  77.  
  78. def intersect(self, line):
  79. A=line.get_A()
  80. B=line.get_B()
  81. C=line.get_C()
  82. ys=(B*C+A*B*self._x-A**2*self._y)/(-A**2-B**2)
  83. if A:
  84. xs=-(B*ys+C)/A
  85. else:
  86. xs=self._x
  87. a=line.angle()
  88. l=(self._r**2-self.dist(line)**2)**(1/2)
  89. lx=l*math.cos(a)
  90. ly=l*math.sin(a)
  91. if line.check_a()>0:
  92. self.xi=xs-lx
  93. self.yi=ys-ly
  94. self.xj=xs+lx
  95. self.yj=ys+ly
  96. else:
  97. self.xi=xs-lx
  98. self.yi=ys+ly
  99. self.xj=xs+lx
  100. self.yj=ys-ly
  101. if line.check_a()>0 and line.check_ins(self):
  102. return 1
  103. else:
  104. return 0
  105.  
  106. def check(self, line):
  107. if self.dist(line)>self._r or not self.intersect(line):
  108. return 0
  109. else:
  110. return 1
  111.  
  112. ##class Polygon:
  113.  
  114.  
  115. L=Line(1.,0.,4.,0.)
  116. C=Circle(2.5, 1., 1.5)
  117. L.show()
  118. C.show()
  119. plt.show()
  120. print(C.dist(L))
  121. print(C.check(L))
  122. L.info()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement