Danqing's project 8
Skip to end of metadata
Go to start of metadata

In this lab we learned how to use 'class' object to improve our 'lsystem' functions. Class object is a good way to make our code more simple. 

For the first required image, I draw a forest with lsystem and class.


I used lsystem provided on the website, and arrange them in 3 rows and 8 columns. I used random location and color to make this forest more interesting. I will discuss more about the code in extension part. 
For the second required image, I used class to show 2 new lsystems. Their number of iteration is from 1-3, since larger iteration number will make the image too big. 

One is what I created last week, and the other is from the ABOP book. 

Extension:

In required image 1, I used random object to make my forest more interesting. My code is shown below.

for j in range(3):

        for i in range(8):

            rf=r.randint(0,3)

            if rf==0:

                tree=ls.Lsystem('systemCL.txt')

                tstr=tree.buildString(r.randint(3,4))

            elif rf==1:

                tree=ls.Lsystem('systemDL.txt')

                tstr=tree.buildString(r.randint(3,6))

            elif rf==2:

                tree=ls.Lsystem('systemGL.txt')

                tstr=tree.buildString(r.randint(3,6))

            elif rf==3:

                tree=ls.Lsystem('systemFL.txt')

                tstr=tree.buildString(r.randint(3,5))

            

            x0=-300+80*i+r.randint(10,50)

            y0=50-150*j+r.randint(0,30)

            terp.place(x0,y0,r.randint(85,95))

            terp.drawString(tstr,r.randint(3,5),r.randint(18,24))

            for n in range( r.randint(20, 40)):

                terp.color((r.random()/1.5,r.random(),0.1))

                terp.place(x0+r.gauss(0,40),y0+r.gauss(0,5),r.randint(0,360))

                terp.drawString('L',r.randint(3,5),90)

I learned from the provided class test code to give my trees random position, and I used random to determine what kind of tree will be drawn. I found that if a systemCL.txt lsystem is drawn with 4 iteration it will be too big, so I modified the random code. 


Otherwise, I add berries to my lsystem. The 'elif' function is just like what I did last week, except that I removed the random color methods so that I can determine the color of berries in lsystems. I also add another color purple to TurtleInterpreter class. 

Then I add _str_ method to Lsystem class. The result is shown above. 
I also learned to use the 'write' method in turtle object and write the lsystem nearby the image.


Well, the image is not about base string, but with a iteration number 3, since in this lsystem image of base string is nothing...


I create 2 new shapes in my TurtleInterpreter function. One is T for a triangle, and the other is V for a star. I created a new lsystem with star shapes and it is shown above. The iteration number is from 1 to 3. 

In this week's lab, I understand that class is another good way to access complex function with simple code. Now I can build my own class for use. 

Labels