I guess, most of you know, what the most popular fractal looks like, but for those, who are still curious about the Koch Snowflake, I'll just leave this link to the Wikipedia article here.
Since the most elegant and easiest way to define any fractal essentially needs recursion, I had to approach the Ti Basic version with an alternative iterative method, because Ti Basic does not support local variables.
I will give you a quick overlook, how I solved it, so you get a clue what my code does:
We store all the points to lists, whereas the X position is stored to L1 and the Y position to L2, which will be plotted later by the command Plot1(, using the XYLine token as type in the parameters, so that it will automatically connect the points with lines.
Now the lists are copied and temporaly saved in L3 and L4, where they are edited adequately.
Proceeding through the whole list with a For( loop, we do the following for each and every line (which is equivalent to two points): (actually, we dont go through the whole list; only to the half and then we use a mirror algorithm, because the fractal is symmetrical)
- Calculate the angle of the line using the arctan function
- Calculate the length of the line
- Replace the line with two lines at the outer and a triangle at the inner using the calculated values and the sin( and cos( functions to convert the lines to X- and Y-Points and append them at the temporary list
- When proceeded through the whole list, update L1 and L2 and reset the temporary lists
- Show the result
The dim of the lists increase exponentially with dim(L1)(N)=1+4^N, at the Nth iteration. Therefore, the maximum iteration is the 4th iteration with 257 points stored, because dim(5) = 1025, which exceeds the maximum size of the list data type by 26 (999).
So, here is the code:
Program:Koch (515 Bytes)
:Radian
:0→Xmin:0→Ymin:94→Xmax:62→Ymax:AxesOff //friendly window settings
:{0,31+1/3,47,62+2/3,94→L1
:{3,3,18+2/3,3,3→L2
:Plot1(xyLine,L1,L2,° //I used the thinest mark here
:DispGraph
:Repeat getKey
:{0→L3:{3→L4
:For(I,2,1+int(.5dim(L1
:L1(I)-L1(I-1
:If Ans:Then
:tan-1((L2(I)-L2(I-1))/Ans→D //D = Angle of line relative to x axis
:Else //prevents Zero Division if a line would go straight up or down
:π/2(1-2(L2(I)<L2(I-1→D
:End
:√((L1(I)-L1(I-1))^2+(L2(I)-L2(I-1))^2→L //phythagorean theorem; use the shorter ^2 token (getkey ID=61)
:√(2/36L^2→T //length of each of the two triangle lines; note, that only the L has to be squared, the 36 is already a 6 squared
:L/3→P //length of each of the two outer lines
:L1(I-1)+Pcos(D→L3(1+dim(L3 //now all line points are being calculated
:L2(I-1)+Psin(D→L4(1+dim(L4
:L3(dim(L3))+Tcos(D+π/4→L3(1+dim(L3
:L4(dim(L4))+Tsin(D+π/4→L4(1+dim(L4
:L3(dim(L3))+Tcos(D-π/4→L3(1+dim(L3
:L4(dim(L4))+Tsin(D-π/4→L4(1+dim(L4
:L1(I→L3(1+dim(L3
:L2(I→L4(1+dim(L4
:End
:augment(L3,seq(94-L3(dim(L3)-X),X,1,dim(L3)-1→L1 //seq creates the symmetry copy of the list
:augment(L4,seq(L4(dim(L4)-X),X,1,dim(L4)-1→L2
:DispGraph
:End
:ZStandart //code after the end is optional, clears everything nice up
:AxesOn
:ClrAllLists
:PlotsOff
:ClrHome
You can zoom in by pressing On at runtime, and using the built in ZBox command (or any other appropriate Zoom command) at the graph screen.
How do you like it? I highly appreciate feedback or optimizations. :)
Wake me, when you need me.