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
-
- Posts: 272
- 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
-
- Posts: 9
- Joined: Wed 12 Feb 2025, 21:45
Re: Koch snowflake
Thank you very much. I'll give it a go myself using your tips.
-
- Posts: 272
- 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.
-
- Posts: 9
- 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!
-
- Posts: 272
- Joined: Tue 18 Jun 2024, 09:32