I'm trying to write a program to produce a Koch snowflake - is there the equivalent way of rotate left and right by an angle theta and proceed forward as in Turtle? It's been driving me mad trying to do it with PLOT statements, trigonometry and trying to spot a pattern with which I can then loop.
Thanking anyone in anticipation.
Koch snowflake
-
Richard Russell
- Posts: 582
- Joined: Tue 18 Jun 2024, 09:32
Re: Koch snowflake
Spoiler alert: I've listed below the program I wrote 15 years ago to plot the Koch snowflake, so if you want to challenge yourself to write the code from scratch don't look at it! The approach I took in that program was the standard trigonometrical one of advancing the 'turtle' by distance * COS(angle) in the x-direction and distance * SIN(angle) in the y-direction.JMR wrote: ↑Thu 13 Feb 2025, 09:56 I'm trying to write a program to produce a Koch snowflake - is there the equivalent way of rotate left and right by an angle theta and proceed forward as in Turtle? It's been driving me mad trying to do it with PLOT statements, trigonometry and trying to spot a pattern with which I can then loop.
Thanking anyone in anticipation.
Don't scroll ...
... if you ...
... don't want ...
... to see ...
... the code!
Code: Select all
REM The Koch snowflake fractal, Richard Russell, 16-May-2010
MODE 0
OFF
PROCsnowflake(240, 740, 800, 4)
FILL 640,512
END
DEF PROCsnowflake(x, y, size, levels)
MOVE x, y
PROCkoch(x, y, size, levels, 0)
PROCkoch(x, y, size, levels, -2*PI/3)
PROCkoch(x, y, size, levels, -4*PI/3)
ENDPROC
DEF PROCkoch(RETURN x, RETURN y, size, level, angle)
IF level <= 0 THEN
x += size*COS(angle)
y += size*SIN(angle)
DRAW x, y
ELSE
PROCkoch(x, y, size/3, level-1, angle)
PROCkoch(x, y, size/3, level-1, angle+PI/3)
PROCkoch(x, y, size/3, level-1, angle-PI/3)
PROCkoch(x, y, size/3, level-1, angle)
ENDIF
ENDPROC
-
JMR
- Posts: 10
- Joined: Wed 12 Feb 2025, 21:45
Re: Koch snowflake
Thank you very much. I'll give it a go myself using your tips.
-
Richard Russell
- Posts: 582
- Joined: Tue 18 Jun 2024, 09:32
Re: Koch snowflake
Here's the output from my program, greatly slowed down:
You do not have the required permissions to view the files attached to this post.
-
JMR
- Posts: 10
- Joined: Wed 12 Feb 2025, 21:45
Re: Koch snowflake
I had to look and I'm glad that I did because there are things I've never used before such as calling a PROCedure within another one (I hope I've described what I mean clearly) and the term RETURN - showing my ignorance I'm afraid.
I code for fun and I think that this was a bit out of league! I've modified the program to show the first 6 snowflakes from levels = 0 to 6 which looks nice.
Thanks for the program and your help - now I just have to work out how it works!
I code for fun and I think that this was a bit out of league! I've modified the program to show the first 6 snowflakes from levels = 0 to 6 which looks nice.
Thanks for the program and your help - now I just have to work out how it works!
-
Richard Russell
- Posts: 582
- Joined: Tue 18 Jun 2024, 09:32
-
JMR
- Posts: 10
- Joined: Wed 12 Feb 2025, 21:45
Re: Koch snowflake but now recursion and quicksort
I've decided to bite the bullet and experiment with recursion and procedures. I'm trying to code a quicksort algorithm but I cannot see why it doesn't sort properly. I've checked the quicksort part manually and it works on paper so I think the problem is the implentation of the recursion part.
I've spent the best part of 2 days, on-and-off, trying to find the error(s) and I'm now just going around in circles. Could you please offer some insight as to what could be going wrong. It might be the IF statement at line 100.
10 nmax% = 10
20 DIM a%(nmax%)
30 a%() = 3, 1, 2, 8, 9, 5, 4
40 PROC_quicksort(a%(),0,6)
50 FOR f% = 0 TO 6
60 PRINT a%(f%)
70 NEXT f%
80 END
90 DEF PROC_quicksort(a%(),low%,high%)
100 IF high% - low% < 2 THEN ENDPROC
110 LOCAL pivot%, pindex%,j%,i%
120
130 j% = low% : i% = j% - 1
140 pivot% = a%(high%)
150
160 REPEAT
170 IF a%(j%) < pivot% THEN
180 i% = i% + 1
190 SWAP a%(j%),a%(i%)
200 j% = j% + 1
210 ELSE
220 j% = j% + 1
230 ENDIF
240 UNTIL j% = high%
250
260 SWAP a%(i% + 1),pivot%
270 pindex% = i% + 1
280 IF low% < pindex% PROC_quicksort(a%(),low%,pindex%-1)
290 IF high% > pindex% PROC_quicksort(a%(),pindex%+1,high%)
300
310 ENDPROC
Any help would be much appreciated. Thank you.
I've spent the best part of 2 days, on-and-off, trying to find the error(s) and I'm now just going around in circles. Could you please offer some insight as to what could be going wrong. It might be the IF statement at line 100.
10 nmax% = 10
20 DIM a%(nmax%)
30 a%() = 3, 1, 2, 8, 9, 5, 4
40 PROC_quicksort(a%(),0,6)
50 FOR f% = 0 TO 6
60 PRINT a%(f%)
70 NEXT f%
80 END
90 DEF PROC_quicksort(a%(),low%,high%)
100 IF high% - low% < 2 THEN ENDPROC
110 LOCAL pivot%, pindex%,j%,i%
120
130 j% = low% : i% = j% - 1
140 pivot% = a%(high%)
150
160 REPEAT
170 IF a%(j%) < pivot% THEN
180 i% = i% + 1
190 SWAP a%(j%),a%(i%)
200 j% = j% + 1
210 ELSE
220 j% = j% + 1
230 ENDIF
240 UNTIL j% = high%
250
260 SWAP a%(i% + 1),pivot%
270 pindex% = i% + 1
280 IF low% < pindex% PROC_quicksort(a%(),low%,pindex%-1)
290 IF high% > pindex% PROC_quicksort(a%(),pindex%+1,high%)
300
310 ENDPROC
Any help would be much appreciated. Thank you.
-
Richard Russell
- Posts: 582
- Joined: Tue 18 Jun 2024, 09:32
Re: Koch snowflake but now recursion and quicksort
Honestly I can't, because my knowledge of the Quicksort algorithm isn't good enough to spot the mistake, but what I can do is list a version which I believe does work, so perhaps comparing the two would be instructive.
Code: Select all
10 nmax% = 10
20 DIM a(nmax%)
30 a() = 3, 1, 2, 8, 9, 5, 4
40 PROCquicksort(a(),0,7)
50 FOR f% = 0 TO 6
60 PRINT a(f%)
70 NEXT f%
80 END
90
100 DEF PROCquicksort(a(), s%, n%)
110 LOCAL l%, p, r%, t%
120 IF n% < 2 THEN ENDPROC
130 t% = s% + n% - 1
140 l% = s%
150 r% = t%
160 p = a((l% + r%) DIV 2)
170 REPEAT
180 WHILE a(l%) < p l% += 1 : ENDWHILE
190 WHILE a(r%) > p r% -= 1 : ENDWHILE
200 IF l% <= r% THEN
210 SWAP a(l%), a(r%)
220 l% += 1
230 r% -= 1
240 ENDIF
250 UNTIL l% > r%
260 IF s% < r% PROCquicksort(a(), s%, r% - s% + 1)
270 IF l% < t% PROCquicksort(a(), l%, t% - l% + 1 )
280 ENDPROC