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):
- return math.atan(abs(self._y2-self._y1)/abs(self._x2-self._x1))
- def check_a(self):
- return (self._y2-self._y1)/(self._x2-self._x1)
- 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, y, r):
- self._x=x
- self._y=y
- self._r=r
- 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)
- xs=-(B*ys+C)/A
- 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
- L=Line(1.,1.,4.,3.)
- C=Circle(2., 0., 1.5)
- L.show()
- C.show()
- plt.show()
- print(C.dist(L))
- print(C.check(L))
- L.info()
- x1=float(input())
- y1=float(input())
- x2=float(input())
- y2=float(input())
- x0=float(input())
- y0=float(input())
- r=float(input())
- x=[x1,x2]
- y=[y1,y2]
- axes = plt.gca()
- axes.set_aspect("equal")
- cc = matplotlib.patches.Circle((x0,y0),r, fill=0)
- axes.add_patch(cc)
- plt.plot(x, y)
- plt.show()
- if x1==x2 and y1!=y2:
- h=abs(x1-x0)
- if h>r:
- print("пересечений нет 1")
- else:
- Y1=y0+(r**2-h**2)**(1/2)
- Y2=y0-(r**2-h**2)**(1/2)
- if y2<y1:
- y1, y2=y2, y1
- if not (y1<=Y1<=y2 or y1<=Y2<=y2):
- print("пересечений нет 2")
- else:
- print("пересечение есть")
- elif x1!=x2 and y1==y2:
- h=abs(y1-y0)
- if h>r:
- print("пересечений нет 1")
- else:
- X1=x0+(r**2-h**2)**(1/2)
- X2=x0+(r**2-h**2)**(1/2)
- if x2<x1:
- x1,x2=x2,x1
- if not (x1<=X1<=x2 or x1<=X2<=x2):
- print("пересечений нет 2")
- else:
- print("пересечение есть")
- else:
- if x2<x1:
- x1,x2,y1,y2=x2,x1,y2,y1
- A=y2-y1
- B=x1-x2
- C=x2*y1-x1*y2
- h=abs(A*x0+B*y0+C)/((A**2+B**2)**(1/2))
- if h>r:
- print("пересечений нет 1")
- ys=(B*C+A*B*x0-A**2*y0)/(-A**2-B**2)
- xs=-(B*ys+C)/A
- l=(r**2-h**2)**(1/2)
- a=math.atan(abs(y2-y1)/abs(x2-x1))
- lx=l*math.cos(a)
- ly=l*math.sin(a)
- ok=(y2-y1)/(x2-x1)
- if ok>0:
- xi=xs-lx
- yi=ys-ly
- xj=xs+lx
- yj=ys+ly
- else:
- xi=xs-lx
- yi=ys+ly
- xj=xs+lx
- yj=ys-ly
- if ok>0 and (x1<=xi<=x2 and y1<=yi<=y2 or x1<=xj<=x2 and y1<=yj<=y2) or ok<0 and (x1<=xi<=x2 and y2<=yi<=y1 or x1<=xj<=x2 and y2<=yj<=y1):
- print("пересечение есть")
- else:
- print("пересечений нет 2")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement