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, 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)
- 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:
- L=Line(1.,0.,4.,0.)
- C=Circle(2.5, 1., 1.5)
- L.show()
- C.show()
- plt.show()
- print(C.dist(L))
- print(C.check(L))
- L.info()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement