Truchet Tiling

A place for users to post example code or tutorials and how to articles.

Truchet Tiling

Postby ZXDunny » Mon May 04, 2015 4:57 pm

Truchet Tiling is a pattern made of two tile-able images - the second a rotation of the first. This is usually two quarter-circles diagonally opposite eachother.

Code: Select all
10 REM Truchet Tiling
20 LET sz=(INT(RND*192)*2)+8: DO: LET w=2+INT(RND*sz): LET sq=((sz/2)+(w/2))*0.7071: LOOP UNTIL sq-1<=sz-sq: DEGREES: SCREEN LOCK: CLS 0
30 LET mp=sz/2,mw=w/2,gs=256/w,mn=mp-mw,mx=mp+mw: FOR x=0 TO mx-1: FOR y=0 TO mx-1: LET a=SQR(x*x+y*y)
40 IF a<=mn OR a>mx THEN LET l=0 ELSE LET l=(a-mn)*gs
50 PLOT OVER 8;INK l;x,y;sz-x,sz-y;sz+sz-x,y;sz+x,sz-y
60 NEXT y: NEXT x
70 DIM g$(2): WINDOW GRAB g$(1),0,0,0,sz,sz: WINDOW GRAB g$(2),0,sz,0,sz,sz
80 LET p=RND*360: LET q=(2+INT(RND*2))*64: FOR f=0 TO 255: PALETTE HSV f,p,q,(SIN(f*(180/256))*255): NEXT f
90 FOR x=0 TO SCRw STEP sz: FOR y=0 TO SCRh STEP sz: WINDOW PUT g$(INT(RND*2)+1),0,x,y: NEXT y: NEXT x: WAIT SCREEN: PAUSE 0: CLS 0: GO TO 20

Line 20 sets up the variables - random size, with random pipe-width, paying attention enough to stop the two pipes overlapping eachother. Lines 30 to 60 generate the two pattern tiles, and line 70 GRABs them into graphic strings. Then line 80 creates a palette - a random hue, with a sine-wave lightness to create a "pipe" appearance. Finally, line 90 draws the image and waits for a key before starting again!

Here's some sample output:


10 PRINT "SCUNTHORPE"(2 TO 5);" ";: GOTO 10
Posts: 538
Joined: Fri Oct 01, 2010 9:53 am
Location: Scunthorpe, North Lincolnshire, UK

Return to Tutorials and Examples

Who is online

Users browsing this forum: No registered users and 1 guest