Page 1 of 1

Koch flake anyone?

Posted: Wed Jul 06, 2005 3:06 pm
by romulusnr
I'm racking my brain on how to create a Koch snowflake (or just one side)... anyone have any examples/suggestions?

Posted: Wed Jul 06, 2005 3:23 pm
by aaronstj
The problem with creating a Kich snowflask or other such fractals is that you can't have the recursion "bottom out" and just draw a line or some other primitive. This is by design (as I understand it) since the language in context free and the rules aren't allowed to know things like what recursion level they are at or any contextual information like that when they're invoked.

Ironically, this is really the only way to draw a "true" Koch snowflake, since it should never bottom out, but just keep going down more levels. But you can't really draw it like you'd want to. You can leave a trail of primitive "footprints", though, like MtnViewMark did in his version of Serpinki's Triangle

Posted: Wed Jul 06, 2005 8:38 pm
by MtnViewJohn
Koch triangles and other fractals are L-systems. Context Free was not architected to create L-systems. There are other graphics tools that are better for creating L-systems. Check out FractInt.

Posted: Wed Jul 06, 2005 10:32 pm
by aaronstj
MtnViewJohn wrote:Koch triangles and other fractals are L-systems. Context Free was not architected to create L-systems.
Nevertheless, some of us are suckers for pain.

Code: Select all

startshape snowflake

rule snowflake {
	triangle { r 180 s 1.154701}
	kochedgetrans {}
	kochedgetrans { r 120 }
	kochedgetrans { r -120 }
} 

rule kochedgetrans {
	kochedge { y .288675 } 
}

rule kochedge {	
	triangle {s .38490 y .09622}

	kochedge { 
		y  0.14433
		x -0.08333
		s  0.33333
		r 60
	}

	kochedge { 
		y 0.14433
		x 0.08333
		s 0.33333
		r -60
	}

	kochedge {
		s  0.33333
		x -0.33333
	}

	kochedge {
		s  0.33333
		x  0.33333
	}
}

rule triangle {
	meta_TRIANGLE{ s .25}
}

rule meta_TRIANGLE{
	TRI{}
	TRI{r 120}
	TRI{r -120}
}
rule TRI{
	SQUARE{x -0.183013 y 0.683013 r -30 }
	SQUARE{x 0.183013 y 0.683013 r 30 }
	TRI{y 1.1547 s 0.42265 }
}
I've used more trig just today than I have probably my entire life. Getting a little tired of it :)

Posted: Thu Jul 07, 2005 4:32 pm
by megaduck0
you are a glutton for punishment. that's a nice job there :)

Or, if you don't like work

Posted: Fri Jul 08, 2005 11:57 am
by t3knomanser
I took a quick and dirty way of making a kinda mod/deco triangle, and used that to make a quick and dirty Koch.

God, I'm lazy.

Code: Select all

startshape BUD

rule TRIANGLE {
	LINE{}
	TRIANGLE {y 1 s .9}
}

rule LINE {
	CIRCLE {x 5 s 2}
	CIRCLE {x -5 s 2}
	LINE {s .9}
}

rule BUD {
	TRIANGLE{}
	BUD {x -3 y 4 r 65 s .5}
	BUD {x 3 y 4 r -65 s .5}
}

Koch Snowflake

Posted: Tue Dec 20, 2005 1:09 pm
by j
Here's a very simple (terminating) Koch snowflake grammar:

Code: Select all

startshape snowflake

rule snowflake {
   TRIANGLE { }
   snowflake { x .3333 y 0.1924 s 0.3333 }
   snowflake { x -0.3333 y 0.1924 s 0.3333 }
   snowflake { y -0.3849 s 0.3333 }
   snowflake { x  0.3333 y -0.1924 s 0.3333 }
   snowflake { x -0.3333 y -0.1924 s 0.3333 }
   snowflake { y 0.3849 s 0.3333 }
} 
You can remove all but one of those recursive snowflake lines (your choice) and add "snowflake { r 60 }" and it will still draw a correct Koch snowflake, but it won't terminate — it will overdraw the figure ad infinitum.

Enjoy,
Justin Knoll

Posted: Wed Dec 28, 2005 3:45 pm
by robo git
Simple, tight and accurate: Very nice piece of coding!

Simple Koch

Posted: Tue Dec 11, 2007 11:20 pm
by kf6kjg
I apologize for resurectting a long dead conversation, but I didn't see the harm in this case, and I couldn't help myself... I saw this page looking for algorithms to draw a Koch flake/island with...

And I came up with this code:

Code: Select all

startshape SNOWFLAKE
background { b -1 }

rule SNOWFLAKE {
	TRIANGLE { b 1 }
	
	6 * { r 60 }
	SNOWFLAKE {
		size .333333333333333333333
		y 0.385
	}
}
Now, I admit that this is not a fully TRUE Koch flake, and it actually renders more prims than the above by aaronstj, but it is really simple code..

Here it is in a "debugging" mode to demonstrate the reasons why it isn't a true koch.

Code: Select all

startshape SNOWFLAKE
background { b -1 }

rule SNOWFLAKE {
	TRIANGLE { b .2}
	
	6 * { r 60 }
	SNOWFLAKE {
		size .333333333333333333333
		y 0.385
		b +.2
	}
}
I just noticed that my code creates close to the same grammar as J's example just above. (Only a difference between "y 0.385" and "y 0.3849" and how many decimals 1/3 is approximated to...) Funny, since I had not seen this article before I created the code! :D