dim 150 150 % calculates direction based on rotation 0 - right ,1 - up ,2 - left , 3- down % rotation can be +1 - clockwise and -1 counterclockwise procedure calculateDir { dir rot newDir } { expression newDir { dir + rot } if_then_else { newDir == -1 } { number newDir 3 } { } if_then_else { newDir == 4 } { number newDir 0 } { } } % draws a line based on the start Point and direction and returns end point procedure step { dir stepLength startPoint returnPoint } { getx startPointX startPoint gety startPointY startPoint if_then_else { dir == 0 } { expression newX { startPointX + stepLength } point returnPoint newX startPointY } { } if_then_else { dir == 1 } { expression newY { startPointY + stepLength } point returnPoint startPointX newY } { } if_then_else { dir == 2 } { expression newX { startPointX - stepLength } point returnPoint newX startPointY } { } if_then_else { dir == 3 } { expression newY { startPointY - stepLength } point returnPoint startPointX newY } { } drawsegment startPoint returnPoint } % draws Hilbert curve with given direction order and start point procedure hilbert { dir rot order stepLength startPoint returnPoint } { expression negRot { ( -1 ) * rot } expression newOrder { order - 1 } if_then_else { order > 0 } { call calculateDir { dir rot dir2 } call hilbert { dir2 negRot newOrder stepLength startPoint returnPoint1 } call step { dir2 stepLength returnPoint1 returnPoint2 } call calculateDir { dir2 negRot dir3 } call hilbert { dir3 rot newOrder stepLength returnPoint2 returnPoint3 } call step { dir3 stepLength returnPoint3 returnPoint4 } call hilbert { dir3 rot newOrder stepLength returnPoint4 returnPoint5 } call calculateDir { dir3 negRot dir4 } call step { dir4 stepLength returnPoint5 returnPoint6 } call hilbert {dir4 negRot newOrder stepLength returnPoint6 returnPoint7 } getx returnPointX returnPoint7 gety returnPointY returnPoint7 point returnPoint returnPointX returnPointY } { getx returnPointX startPoint gety returnPointY startPoint point returnPoint returnPointX returnPointY } } point numOfIterations 1 0 7 0 animation_frames 7 1 getx n numOfIterations expression stepLength { 150 / pow( 2 , n )} expression startX { 75 / pow(2,n) } expression startY { 75 / pow(2,n) } point start startX startY call hilbert { 0 1 n stepLength start return }