#%cython #uncomment this line to run in a Sage notebook # Rectiling v1 # #Improvements to make #- put bool option, whether to print rects with neg edges or not. #- add appropriate colouring for square tilings. #- check if input/enter vals add up properly. ERROR if not. from sage.misc.functional import show from sage.plot.graphics import Graphics from sage.plot.line import line2d from sage.plot.text import text from sage.plot.polygon import polygon2d as p #NB edgecolor has only worked since 2014;download the new polygon.py if needed from sage.misc.latex import latex from libc.math cimport abs #-------------------------------------------------- DEF cx=30*4 #x-pos of central rect in grid - NB keep as a multiple of 4. #Change cx here to affect number of rects calculated: approx (2*cx)^2 cdef bint PrintGrid=False #also makes pgn of the grid if True DEF GRIDPICSIZE=12 #size of grid pgn produced cdef bint LabelTiling=False#True # writes sizes on tiling if True DEF TILPICSIZE=16 #size of tiling pgn produced DEF EDGE=0.5 #black edge width, try 0.5 or 1 cdef bint ColourIn=True #make coloured tiling if True DEF MAXSIDE=250.0 #for colouring - set to approx largest side length #MAXSIDE controls how big the rects have to be before the red level maxes out #i.e. a smaller value will make the tiling look redder. #Current colour map: skinnier=greener. shorter=bluer. bigger=redder #-------------------------------------------------- DEF cy=cx #y-pos in grid of initial rectangle. DEF MAXDIM=2*cx DEF gridwidth=cx-4 # number of cells x cells to show on grid. must be 255: red=255 if red<0: red=0 f+=p(rect,color='#%02x%02x%02x' % (red,Colour(fw),Colour(fh)), \ edgecolor="black",thickness=EDGE) else: f+=p(rect,fill=False,color="black") if LabelTiling: f+=text("${},{}$".format(S.w,S.h),((S.L+S.R)/2.0,(S.T+S.B)/2.0),color="black") S.Drawn = True cdef DrawType1AndFindNeighboursSides(int X, int Y): if X>cx+TILH or Xcy+TILH or Y0 and N.w>0: #if not drawn yet, and has sides>0 SetSides(N,S.T,S.L+N.w,S.T-N.h,S.R) DrawType2AndFindNeighboursSides(X+1,Y) N=&g[X][Y+1] #the rectangle above if not N.Drawn and N.h>0 and N.w>0: SetSides(N,S.T+N.h,S.L+N.w,S.T,S.L) DrawType2AndFindNeighboursSides(X,Y+1) N=&g[X][Y-1] #the rectangle below if not N.Drawn and N.h>0 and N.w>0: SetSides(N,S.B,S.R,S.B-N.h,S.R-N.w) DrawType2AndFindNeighboursSides(X,Y-1) N=&g[X-1][Y] #the rectangle to left if not N.Drawn and N.h>0 and N.w>0: SetSides(N,S.B+N.h,S.L,S.B,S.L-N.w) DrawType2AndFindNeighboursSides(X-1,Y) cdef DrawType2AndFindNeighboursSides(int X, int Y): if X>cx+TILH or Xcy+TILH or Y0 and N.w>0: SetSides(N,S.B+N.h,S.R+N.w,S.B,S.R) DrawType1AndFindNeighboursSides(X+1,Y) N=&g[X][Y+1] #the rectangle above if not N.Drawn and N.h>0 and N.w>0: SetSides(N,S.T+N.h,S.R,S.T,S.R-N.w) DrawType1AndFindNeighboursSides(X,Y+1) N=&g[X][Y-1] #the rectangle below if not N.Drawn and N.h>0 and N.w>0: SetSides(N,S.B,S.L+N.w,S.B-N.h,S.L) DrawType1AndFindNeighboursSides(X,Y-1) N=&g[X-1][Y] #the rectangle to left if not N.Drawn and N.h>0 and N.w>0: SetSides(N,S.T,S.L,S.T-N.h,S.L-N.w) DrawType1AndFindNeighboursSides(X-1,Y) cdef SetSides(r *C,int T, int R, int B, int L): C.T=T C.R=R C.B=B C.L=L #Initialize vars for i in range(MAXDIM): for j in range(MAXDIM): C=&g[i][j] C.WK=False C.HK=False C.Drawn=False SetSides(C,-1000,-1000,-1000,-1000) #mainly so theyre not >0 #-------------------------------------------------- #manually enter known vals EnterSize([0,1],10,20) EnterSize([1,1],12,16) EnterSize([0,0],9,14) EnterSize([1,0],13,13) EnterSize([0,-1],8,9) EnterSize([1,-1],3,10) #e.g. to get a square tesselation # EnterSize([0,1],5,5) # EnterSize([1,1],2,2) # EnterSize([0,0],3,3) # EnterSize([1,0],4,4) # EnterSize([0,-1],10,10) # EnterSize([1,-1],9,9) #-------------------------------------------------- #draw grid if PrintGrid: f=Graphics() for i in range(cx-H,cx+H,2): for j in range(cx-H,cx+H,2): for k in range(2): f+=line2d([[i+0.1+k,j+1+k],[i+1.9+k,j+1+k]],color="black") f+=line2d([[i+k+1,j+1.1-k],[i+k+1,j+2.9-k]],color="black") while Extrapolate(): pass if PrintGrid: ShowGridVals() show(f, aspect_ratio=1,figsize=GRIDPICSIZE) f=Graphics() #Now draw tiling C=&g[cx][cy] #put grid rect [cy][cy] with its bottom left at (0,0), then work outwards SetSides(C,C.h,C.w,0,0) #pointer,top,right,bottom,left sides. #g[cx,cy] is called type 1 - bottom left of an h-line. its perp neighbours are type2. DrawType1AndFindNeighboursSides(cx,cy) show(f,aspect_ratio=1,figsize=TILPICSIZE,axes=False)