def draw(self, g): pyHAbstractFigure.draw(self, g) if self.points: l = len(self.points) j = 0 while j < l - 3: p0 = vector(self.points[j + 0].getX(), self.points[j + 0].getY()) p1 = vector(self.points[j + 1].getX(), self.points[j + 1].getY()) p2 = vector(self.points[j + 2].getX(), self.points[j + 2].getY()) p3 = vector(self.points[j + 3].getX(), self.points[j + 3].getY()) pi = p0 for i in range(0, 21): t = i / 20.0 p02 = p0 * (1 - t)**2 + p1 * 2 * (1 - t) * t + p2 * t**2 p12 = p1 * (1 - t)**2 + p2 * 2 * (1 - t) * t + p3 * t**2 p = (1 - t) * p02 + t * p12 g.drawLine(pi.x, pi.y, p.x, p.y) pi = p j += 3 p0 = self.points[j] for p1 in self.points[j + 1:]: g.drawLine(p0.getX(), p0.getY(), p1.getX(), p1.getY()) p0 = p1
def intersect(self, lin1): if (self.canIntersect(lin1)): (a0, b0, c0) = self.getGeneralEquation() (a1, b1, c1) = lin1.getGeneralEquation() a = np.array([[a0, b0], [a1, b1]]) b = np.array([-c0, -c1]) x = np.linalg.solve(a, b) return vector(x) else: raise NoIntersectLines("No Intersect Lines")
def containPoint(self,q): for i in range(len(self.points)-1): p0=vector(self.points[i].getX(),self.points[i].getY()) p1=vector(self.points[i+1].getX(),self.points[i+1].getY()) pq=vector(q.getX(),q.getY()) vs=p1-p0 vLen=vs.mag vn=vs.norm() if vn.mag==0: continue vq=pq-p0 d=vn.cross(vq).mag if abs(d)<=1: #project vq on vn l=vn.dot(vq) if l>=0: prj=vn*l #projection length must be shorter than segment lenght vn prjLen=prj.mag if prjLen>=0 and prjLen<=vLen: return True return False
def distance(self, point): (a, b, c) = self.getGeneralEquation() return abs(a * point.getX + b * point.getY + c) / vector(a, b).mag
def __init__(self, p0, p1): self.p = p0 self.v = vector(p1 - p0).norm()
return self.distance(point) < 0.001 def intersect(self, lin1): if (self.canIntersect(lin1)): (a0, b0, c0) = self.getGeneralEquation() (a1, b1, c1) = lin1.getGeneralEquation() a = np.array([[a0, b0], [a1, b1]]) b = np.array([-c0, -c1]) x = np.linalg.solve(a, b) return vector(x) else: raise NoIntersectLines("No Intersect Lines") class NoIntersectLines(Exception): def __init__(self, value): self.value = value def __str__(self): return repr(self.value) v1 = vector(1, 2, 3) v2 = vector(0, 0, 0) v3 = vector(3, 4, 12) vp = v1.cross(v2) print(vp) # v5 = vector(1, 2, 3, 4) print('end')