from reportlab.lib import colors from reportlab.lib.attrmap import * from reportlab.pdfgen.canvas import Canvas from reportlab.graphics.shapes import Group, Drawing, Ellipse, Wedge, String, STATE_DEFAULTS, Polygon, Line def _getShaded(col,shd=None,shading=0.1): if shd is None: from reportlab.lib.colors import Blacker if col: shd = Blacker(col,1-shading) return shd def _getLit(col,shd=None,lighting=0.1): if shd is None: from reportlab.lib.colors import Whiter if col: shd = Whiter(col,1-lighting) return shd def _draw_3d_bar(G, x1, x2, y0, yhigh, xdepth, ydepth, fillColor=None, fillColorShaded=None, strokeColor=None, strokeWidth=1, shading=0.1): fillColorShaded = _getShaded(fillColor,None,shading) fillColorShadedTop = _getShaded(fillColor,None,shading/2.0) def _add_3d_bar(x1, x2, y1, y2, xoff, yoff, G=G,strokeColor=strokeColor, strokeWidth=strokeWidth, fillColor=fillColor): G.add(Polygon((x1,y1, x1+xoff,y1+yoff, x2+xoff,y2+yoff, x2,y2), strokeWidth=strokeWidth, strokeColor=strokeColor, fillColor=fillColor,strokeLineJoin=1)) usd = max(y0, yhigh) if xdepth or ydepth: if y0!=yhigh: #non-zero height _add_3d_bar( x2, x2, y0, yhigh, xdepth, ydepth, fillColor=fillColorShaded) #side _add_3d_bar(x1, x2, usd, usd, xdepth, ydepth, fillColor=fillColorShadedTop) #top G.add(Polygon((x1,y0,x2,y0,x2,yhigh,x1,yhigh), strokeColor=strokeColor, strokeWidth=strokeWidth, fillColor=fillColor,strokeLineJoin=1)) #front if xdepth or ydepth: G.add(Line( x1, usd, x2, usd, strokeWidth=strokeWidth, strokeColor=strokeColor or fillColorShaded)) class _YStrip: def __init__(self,y0,y1, slope, fillColor, fillColorShaded, shading=0.1): self.y0 = y0 self.y1 = y1 self.slope = slope self.fillColor = fillColor self.fillColorShaded = _getShaded(fillColor,fillColorShaded,shading) def _ystrip_poly( x0, x1, y0, y1, xoff, yoff): return [x0,y0,x0+xoff,y0+yoff,x1+xoff,y1+yoff,x1,y1] def _make_3d_line_info( G, x0, x1, y0, y1, z0, z1, theta_x, theta_y, fillColor, fillColorShaded=None, tileWidth=1, strokeColor=None, strokeWidth=None, strokeDashArray=None, shading=0.1): zwidth = abs(z1-z0) xdepth = zwidth*theta_x ydepth = zwidth*theta_y depth_slope = xdepth==0 and 1e150 or -ydepth/float(xdepth) x = float(x1-x0) slope = x==0 and 1e150 or (y1-y0)/x c = slope>depth_slope and _getShaded(fillColor,fillColorShaded,shading) or fillColor zy0 = z0*theta_y zx0 = z0*theta_x tileStrokeWidth = 0.6 if tileWidth is None: D = [(x1,y1)] else: T = ((y1-y0)**2+(x1-x0)**2)**0.5 tileStrokeWidth *= tileWidth if Tself.x1: return 1 if o.s==self.s and o.i in (self.i-1,self.i+1): return a = self.a b = self.b oa = o.a ob = o.b det = ob*a - oa*b if -1e-81 or ou<0 or ou>1: return x = x0 + u*a y = self.y0 + u*b if _ZERO=small: a(seg) S.sort(key=_segKey) I = [] n = len(S) for i in range(0,n-1): s = S[i] for j in range(i+1,n): if s.intersect(S[j],I)==1: break I.sort() return I if __name__=='__main__': from reportlab.graphics.shapes import Drawing from reportlab.lib.colors import lightgrey, pink D = Drawing(300,200) _draw_3d_bar(D, 10, 20, 10, 50, 5, 5, fillColor=lightgrey, strokeColor=pink) _draw_3d_bar(D, 30, 40, 10, 45, 5, 5, fillColor=lightgrey, strokeColor=pink) D.save(formats=['pdf'],outDir='.',fnRoot='_draw_3d_bar') print(find_intersections([[(0,0.5),(1,0.5),(0.5,0),(0.5,1)],[(.2666666667,0.4),(0.1,0.4),(0.1,0.2),(0,0),(1,1)],[(0,1),(0.4,0.1),(1,0.1)]])) print(find_intersections([[(0.1, 0.2), (0.1, 0.4)], [(0, 1), (0.4, 0.1)]])) print(find_intersections([[(0.2, 0.4), (0.1, 0.4)], [(0.1, 0.8), (0.4, 0.1)]])) print(find_intersections([[(0,0),(1,1)],[(0.4,0.1),(1,0.1)]])) print(find_intersections([[(0,0.5),(1,0.5),(0.5,0),(0.5,1)],[(0,0),(1,1)],[(0.1,0.8),(0.4,0.1),(1,0.1)]]))