Koch flake anyone?

If you have a design you're proud of, share the cfdg file here. It's also a good place to ask for feedback and collaborate.

Moderators: MtnViewJohn, chris, mtnviewmark

Post Reply
romulusnr
Posts: 13
Joined: Wed Jul 06, 2005 2:50 pm
Location: Seattle

Koch flake anyone?

Post by romulusnr »

I'm racking my brain on how to create a Koch snowflake (or just one side)... anyone have any examples/suggestions?

User avatar
aaronstj
Posts: 66
Joined: Wed Jul 06, 2005 11:34 am
Location: Seattle

Post 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

User avatar
MtnViewJohn
Site Admin
Posts: 882
Joined: Fri May 06, 2005 2:26 pm
Location: Mountain View, California
Contact:

Post 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.

User avatar
aaronstj
Posts: 66
Joined: Wed Jul 06, 2005 11:34 am
Location: Seattle

Post 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 :)

megaduck0
Posts: 13
Joined: Wed Jul 06, 2005 3:36 pm

Post by megaduck0 »

you are a glutton for punishment. that's a nice job there :)

t3knomanser
Posts: 21
Joined: Wed Jul 06, 2005 4:53 pm

Or, if you don't like work

Post 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}
}

j
Posts: 1
Joined: Tue Dec 20, 2005 12:59 pm
Contact:

Koch Snowflake

Post 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

robo git
Posts: 47
Joined: Sat Jul 09, 2005 11:36 pm
Location: looking for his marbles
Contact:

Post by robo git »

Simple, tight and accurate: Very nice piece of coding!

kf6kjg
Posts: 1
Joined: Tue Dec 11, 2007 11:04 pm

Simple Koch

Post 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
Ricky

Post Reply