def getAngleFromLongestEdge(areaPoly, origin=None): """ finds the angle of the longest edge in relation to the x-axis. if there is a draw, the edge closest to the origin wins. This function is only usable for konvex polygons, but works for concave as well. the square distance is used since it's faster to compute """ if not origin: origin=(0,0) last=areaPoly[-1] longest=None dmax=0 for node in areaPoly: d2=fun.getDistanceSq(node, last) if d2==dmax: #look for distance to origin dtmp=min([fun.getDistanceSq(node, origin), fun.getDistanceSq(last, origin)]) dtmp2=min([fun.getDistanceSq(node, longest[0]), fun.getDistanceSq(last, longest[1])]) if dtmp<dtmp2: longest = (node,last) dmax=d2 elif d2>dmax: longest = (node,last) dmax=d2 last=node return fun.angleToXAxis(longest)
def getAngleFromLongestEdge(self, areaPoly=None, origin=None): """ finds the angle of the longest edge in relation to the x-axis. if there is a draw, the edge closest to the origin wins. This function is only usable for konvex polygons, but works for concave as well. the square distance is used since it's faster to compute the shift thing only works if the origin is in the "lower left" corner. """ if not origin: origin=self.origin if not areaPoly: areaPoly=self.areaPoly last=areaPoly[-1] longest=None dmax=0 for node in areaPoly: d2=fun.getDistanceSq(node, last) if d2==dmax: #look for distance to origin dtmp=min([fun.getDistanceSq(node, origin), fun.getDistanceSq(last, origin)]) dtmp2=min([fun.getDistanceSq(node, longest[0]), fun.getDistanceSq(last, longest[1])]) if dtmp<dtmp2: longest = (node,last) dmax=d2 elif d2>dmax: longest = (node,last) dmax=d2 last=node #now, calculate the distance to this line. #we need to make a line, not a ray, in order to get the extension as well. infRay=np.array(longest) infRay=infRay+1e5*(infRay-infRay[1])+1e5*(infRay-infRay[0]) #infinite extension of longest pTmp, t=col.closestLinePoint(origin, infRay, True) assert t!=1 and t!=0 d=fun.getDistance(pTmp,origin) assert d>=0 #now, we know that d+shift=n*L+C..where n is an integer n=round((d-self.C)/float(self.L)) shift=self.L*n+self.C-d assert abs(shift)<=self.L angle=fun.angleToXAxis(longest) assert angle>=0 if 0<angle<=pi/2: #shift x negative shift=(-shift,0) elif pi/2<angle<=pi: #shift y negative shift=(0,-shift) elif pi<angle<=3*pi/2.0: #shift x positive shift=(shift,0) else: shift=(0, -shift) return angle, shift
def plot_coverage(G=None, ax=None, color='#666677'): if not G or not ax: raise Exception() points=15 angles=np.linspace(0, np.pi, points) #for half circle for e in G.edges(data=False): """p1=np.array(e[0]) p2=np.array(e[1]) d=p1-p2 circles=int(np.sqrt(np.dot(d,d))/6.)#one every 6m for i in range(circles): p=p2+d*(i+1)/(circles+1) c=Circle(p, radius=G.C, alpha=470, color='#666677') ax.add_patch(c)""" x=e[0][0], e[1][0] y=e[0][1], e[1][1] try: w=G.C*2 except: w=24 nodes=[] dir=fun.angleToXAxis(e)-np.pi/2. l=fun.getDistance(e[0], e[1]) nodes.append(fun.getCartesian([w/2.,0], origin=e[0], direction=dir, fromLocalCart=True)) nodes.append(fun.getCartesian([w/2.,l], origin=e[0], direction=dir, fromLocalCart=True)) #now, fix end half circle. for ang in angles: loc=fun.getCartesian([w/2., ang]) #get local cartesian from cyl. p=fun.getCartesian(loc, origin=e[1], direction=dir, fromLocalCart=True) nodes.append(p) nodes.append(fun.getCartesian([-w/2.,l], origin=e[0], direction=dir, fromLocalCart=True)) nodes.append(fun.getCartesian([-w/2.,0], origin=e[0], direction=dir, fromLocalCart=True)) #fix the other half circle for ang in angles: loc=fun.getCartesian([w/2., ang]) #get local cartesian from cyl. p=fun.getCartesian(loc, origin=e[0], direction=dir-np.pi, fromLocalCart=True) nodes.append(p) pol = Polygon(nodes, alpha=150, color=color, edgecolor='none') ax.add_patch(pol) return ax