Pythagorean Tree with L-System?

Post questions on how to use or achieve an effect in Inkscape.
Ailurus
Posts: 115
Joined: Fri Oct 22, 2010 9:53 am
Location: The Netherlands
Contact:

Pythagorean Tree with L-System?

Postby Ailurus » Sat Oct 23, 2010 12:17 am

Hi,

I wondered whether it is possible to create a Pythagorean tree using the L-System (=Lindenmayer System) function. With the help of Tavmjong's book I succeeded in creating a Koch's snowflake, after I couldn't find out how to create one myself (only got one side working).

Next challenge: a Pythagorean Tree.
Info: http://mathworld.wolfram.com/PythagorasTree.html

(Click for full size)
Image

I cannot figure out how to make lines of different lengths (the F-lines should be A/sqrt(2) long). Moreover, when I increase the order, it doesn't work as I thought it would be.
Is it possible?

PS: Found this on Flickr, should be possible? http://www.flickr.com/photos/botond-balazs/3596225404/

User avatar
brynn
Posts: 10309
Joined: Wed Sep 26, 2007 4:34 pm
Location: western USA
Contact:

Re: Pythagorean Tree with L-System?

Postby brynn » Sun Oct 24, 2010 11:46 am

I cannot figure out how to make lines of different lengths (the F-lines should be A/sqrt(2) long).

This is mostly way over my head. But after 15 minutes playing around with the L-System dialog, it looks to me like parameters are needed for pyth. tree, which are not available in the dialog. However, strong emphasis on "way" over my head!
Good luck :D

Ailurus
Posts: 115
Joined: Fri Oct 22, 2010 9:53 am
Location: The Netherlands
Contact:

Re: Pythagorean Tree with L-System?

Postby Ailurus » Sun Oct 24, 2010 11:02 pm

Hey Brynn,

Thanks for your time anyway :). I spend some more time on it, and got a little bit further:

Image

The Axiom and Rules are OK now, but I need some scaling. I edited the scripts and added a box "Factor", and typed the value of sqrt(2)/2. However, I'm not sure how to scale the lines. At first I thought each "level" would be drawn individually, that would make it easy to pass a level parameter to the drawing function. However, first the entire thing is "calculated" (see MessageBox).
Hm, I think this question is becoming a Python related question :D

Ailurus
Posts: 115
Joined: Fri Oct 22, 2010 9:53 am
Location: The Netherlands
Contact:

Re: Pythagorean Tree with L-System?

Postby Ailurus » Mon Oct 25, 2010 12:06 am

Ok, I solved it for now in a dirty way (it is only applicable for Pythagoras Trees which use the exact syntax as in the image below). Click for a larger version.

Image

The B in the rules replaces the F in the old one. Why? Because B shouldn't scale like the other lines. In other words, B is a line at the previous level. The other variables (A and F) should scale with sqrt(2)/2 per level.

I added this line to lindenmayer.inx:

Code: Select all

<param name="factor" type="float" precision="8" min="0.0" max="100.0" _gui-text="Factor">0.0</param>


To the file lindenmayer.py I made some more changes. Most important ones, In the __recurse() part:

Code: Select all

addition = self.__recurse(self.rules[c],level+1)

                    addition = addition.replace('A','A*')
                    addition = addition.replace('B','B*')
                    addition = addition.replace('F','F*')
                   
                    level_string = level_string + addition

This adds an asterisk after each occurrence of A, B and F. So at level 0, there won't be any asterisks, the lines at level 1 will have one, etc. Next, the __compose_path():

Code: Select all

i = 0
        while i < len(string):
            if string[i] in 'ABCDEF':
                self.turtle.pd()
                scaling = 0
                while (i+1+scaling < len(string) and string[i+1+scaling]=='*'):
                    scaling+=1
                   
                if string[i] == 'B': #Scale with respect to the previous level, so decrease scaling.
                    scaling-=1
                   
                self.turtle.fd(self.options.step * (self.options.factor**(scaling)) * (random.normalvariate(1.0, 0.01 * self.options.randomizestep)))


This counts the amount of asterisks after the A, B and F characters. Since B should scale with respect to the previous level, there is an extra if-clause. The actual scaling happens by multiplying the step with the factor^level.

Since I'm not familiar with Python, there may be more elegant solutions :D.

Ailurus
Posts: 115
Joined: Fri Oct 22, 2010 9:53 am
Location: The Netherlands
Contact:

Re: Pythagorean Tree with L-System?

Postby Ailurus » Mon Oct 25, 2010 1:28 am

Oh, one other thing. It is of course also possible to use the factor with other fractals, for instance Koch's Snowflake. When setting the factor to 1/3, the size of the figure stays the same, instead of growing with a factor 3 (like in the original version) each level 8-)

Image
Once again, click for the larger version.

User avatar
brynn
Posts: 10309
Joined: Wed Sep 26, 2007 4:34 pm
Location: western USA
Contact:

Re: Pythagorean Tree with L-System?

Postby brynn » Mon Oct 25, 2010 3:47 am

I edited the scripts and added a box "Factor"....

Wow! You are way above me, being able to write the code, lol. Perhaps you might like to collaborate with the author of that extension, to produce a new extension, possibly for inclusion in a future release? In any case, I would love to have such. I quite enjoy working with fractals, and I think some quite impressive images could be created with your script/extension. I have played with L-System extension in the past, but never got very far, for exactly the reason you found. So this would really be a very nice tool for everyone. (I can think of one Inkscape user in particular, who like me, has a passion for fractals, and there are probably many more.)

Well, congratulations on some fine work :D

Ailurus
Posts: 115
Joined: Fri Oct 22, 2010 9:53 am
Location: The Netherlands
Contact:

Re: Pythagorean Tree with L-System?

Postby Ailurus » Tue Jul 17, 2012 5:44 am

Wow, this topic is even older than I thought.

Anyway, recently I started working with L-systems again, and implemented something in MATLAB. When I have enough time, and know more about coding extensions for Inkscape (is there a tutorial somewhere?), then I would like to update/rewrite the L-system extension, such that you can also create things like this.

Image

I tried to upload the SVG file, but it's about 4.4 MB :D. So I included the PDF file (on A3 format), if you want you can create an SVG from that file.
Attachments
PythTree_A3Format.pdf
(88.28 KiB) Downloaded 349 times
Last edited by Ailurus on Thu Nov 22, 2012 10:45 pm, edited 1 time in total.

User avatar
brynn
Posts: 10309
Joined: Wed Sep 26, 2007 4:34 pm
Location: western USA
Contact:

Re: Pythagorean Tree with L-System?

Postby brynn » Tue Jul 17, 2012 4:45 pm

Wow, impressive!

There aren't any tutorials that I know of, for writing Inkscape extensions. Although we get more requests than ever, these days. I usually direct people to the Developer's section of the Inkscape wiki, as a starting place. Also, in this forum's Programming subforum, a member named LiquidAsh has written somewhat of a guide, but it's not directly about writing extensions. I don't understand much about programming, so I can't explain it very well.

I played around with that L-system extension, since my last reply in this topic. I managed to make some interesting stuff, and actually to understand it a little better. It's really hard to figure out how to make a particular shape, and especially without the lines overlapping each other a bazillion times (resulting in huge files). It's a very complex extension :D


Return to “Help with using Inkscape”