Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from ast import Return
- import matplotlib.pyplot as plt
- import matplotlib.patches
- import matplotlib.path
- import numpy as np
- import math
- class Line:
- def __init__(self, x1, y1, x2, y2):
- if x2<x1:
- self._x1=x2
- self._y1=y2
- self._x2=x1
- self._y2=y1
- else:
- self._x1=x1
- self._y1=y1
- self._x2=x2
- self._y2=y2
- self.xi=0.
- self.yi=0.
- self.xj=0.
- self.yj=0.
- self.xs=0.
- self.ys=0.
- def get_A(self):
- if abs(self._y2-self._y1)<0.000001:
- return 0
- return self._y2-self._y1
- def get_B(self):
- if abs(self._x1-self._x2)<0.000001:
- return 0
- return self._x1-self._x2
- def get_C(self):
- if abs(self._x2*self._y1-self._x1*self._y2)<0.000001:
- return 0
- return self._x2*self._y1-self._x1*self._y2
- def info(self):
- print(self.get_A(), self.get_B(), self.get_C(), self._x1, self._y1, self._x2, self._y2)
- def show(self):
- x=[self._x1,self._x2]
- y=[self._y1,self._y2]
- plt.plot(x, y, color='red')
- def angle(self):
- if abs(self._x2-self._x1)>0.000001:
- return math.atan(abs(self._y2-self._y1)/abs(self._x2-self._x1))
- else:
- return math.pi/2
- def check_a(self):
- if abs(self._y2-self._y1)<0.000001:
- return 'a'
- elif abs(self._x2-self._x1)<0.000001:
- return 'b'
- elif (self._y2-self._y1)/(self._x2-self._x1)>0:
- return 'c'
- else:
- return 'd'
- def check_ins(self, circle):
- print(circle.xi,circle.yi,circle.xj,circle.yj)
- 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)):
- return 1
- else:
- return 0
- class Circle:
- def __init__(self, x=0., y=0., r=0.):
- self._x=x
- self._y=y
- self._r=r
- def input(self):
- self._x=float(input())
- self._y=float(input())
- self._r=float(input())
- def show(self):
- axes = plt.gca()
- axes.set_aspect("equal")
- cc = matplotlib.patches.Circle((self._x,self._y),self._r, fill=0)
- axes.add_patch(cc)
- def dist(self, line):
- a=line.get_A()
- b=line.get_B()
- c=line.get_C()
- if a==0:
- return abs(self._y+c/b)
- elif b==0:
- return abs(self._x+c/a)
- return abs(a*self._x+b*self._y+c)/((a**2+b**2)**(1/2))
- def intersect(self, line):
- A=line.get_A()
- B=line.get_B()
- C=line.get_C()
- l=(self._r**2-self.dist(line)**2)**(1/2)
- if A==0:
- if C:
- self.ys=-(B/C)
- else:
- self.ys=0.
- self.xs=self._x
- self.xi=self._x-l
- self.yi=self.ys
- self.xj=self._x+l
- self.yj=self.ys
- elif B==0:
- self.ys=self._y
- if C:
- self.xs=-(A/C)
- else:
- self.xs=0.
- self.xi=self.xs
- self.yi=self._y-l
- self.xj=self.xs
- self.yj=self._y+l
- else:
- a=line.angle()
- lx=l*math.cos(a)
- ly=l*math.sin(a)
- self.ys=(B*C+A*B*self._x-A**2*self._y)/(-A**2-B**2)
- self.xs=-(B*self.ys+C)/A
- if line.check_a()=='c':
- self.xi=self.xs-lx
- self.yi=self.ys-ly
- self.xj=self.xs+lx
- self.yj=self.ys+ly
- elif line.check_a()=='d':
- self.xi=self.xs-lx
- self.yi=self.ys+ly
- self.xj=self.xs+lx
- self.yj=self.ys-ly
- if line.check_ins(self):
- return 1
- else:
- return 0
- def check(self, line):
- if self.dist(line)>self._r or not self.intersect(line):
- return 0
- else:
- return 1
- class Polygon:
- def __init__(self):
- self._P=[]
- self._L=[]
- def input(self):
- self._P=[[float(input()), float(input())] for i in range(int(input()))]
- l=len(self._P)
- for i in range(l):
- if i+1<l:
- self._L.append(Line(self._P[i][0], self._P[i][1], self._P[i+1][0], self._P[i+1][1]))
- else:
- self._L.append(Line(self._P[i][0], self._P[i][1], self._P[0][0], self._P[0][1]))
- def check(self, circle):
- for i in self._L:
- if circle.check(i):
- return 1
- return 0
- def show(self):
- for i in self._L:
- i.show()
- C=Circle()
- P=Polygon()
- C.input()
- P.input()
- print(P.check(C))
- P.show()
- C.show()
- plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement