Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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
- def get_A(self):
- return self._y2-self._y1
- def get_B(self):
- return self._x1-self._x2
- def get_C(self):
- return self._x2*self._y1-self._x1*self._y2
- def info(self):
- print(self.get_A(), self.get_B(), self.get_C())
- def show(self):
- x=[self._x1,self._x2]
- y=[self._y1,self._y2]
- plt.plot(x, y)
- def angle(self):
- if self._x2-self._x1:
- return math.atan(abs(self._y2-self._y1)/abs(self._x2-self._x1))
- else:
- return math.pi/2
- def check_a(self):
- if self._x2-self._x1:
- return (self._y2-self._y1)/(self._x2-self._x1)
- else:
- return 1
- def check_ins(self, circle):
- 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):
- 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()
- 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()
- ys=(B*C+A*B*self._x-A**2*self._y)/(-A**2-B**2)
- if A:
- xs=-(B*ys+C)/A
- else:
- xs=self._x
- a=line.angle()
- l=(self._r**2-self.dist(line)**2)**(1/2)
- lx=l*math.cos(a)
- ly=l*math.sin(a)
- if line.check_a()>0:
- self.xi=xs-lx
- self.yi=ys-ly
- self.xj=xs+lx
- self.yj=ys+ly
- else:
- self.xi=xs-lx
- self.yi=ys+ly
- self.xj=xs+lx
- self.yj=ys-ly
- if line.check_a()>0 and 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