Game AI & Unity/L-system algorithm

[Plants and Python][prac4] 해바라기 만들기2

bay07 2024. 3. 22. 15:50

But sunflowers grow!

Yes, it's nice to create an animation changing the angle of florets with a fixed radius. But in the real world a sunflower is growing! The florets arise from a meristem (a population of stem cells) at the center of the sunflower. At the periphery of the shoot apical meristem in the center of the sunflower, cells are determined to become a floret. We call the ability of cells, like meristems, to produce different types of tissues "pluripotent". We say that cells are "determined" or "fated" to become a particular type of tissue as they lose pluripotency. "Differentiated" cells have already become a particular tissue. After a floret has differentiated, it moves away from the center of the sunflower.

In your previous plots, the florets at the periphery were the first to arise, and were "pushed" from the center by all the other florets that subsequently arose. Let's create an animation where we watch florets arise at the center and move outwards!

You will:

  1. Create pre-populated lists of thetas and radii. Simply use the code you already have to calculate the thetas and radii.
  2. Once born, a floret always keeps the same theta. But the radius gets longer and longer.
  3. Our first loop will create lists of theta and radii that increase in length with each iteration. Because of this, we will start with one floret, then two, and so on, and with each additional floret, the radii of the first florets will become longer and longer. We will start with no florets and grow our sunflower, adding more and more. This loop takes one more member of your pre-specified thetas and radii lists with each iteration.
  4. There is a problem, though. Each time we add more thetas (which are associated with unique florets), the oldest/first thetas are at the beginning of the list. The oldest radii, too, are at the beginning of their list and they will be the shortest radii! The oldest/first thetas should have the longest radii, not the shortest!
  5. We will use the .reverse() function, which reverses the order of elements of a list. What this will do is insure that the oldest/first thetas of the thetas list correspond to the longest radii after the radii list is reversed.
  6. Think through the reasoning above, it's complicated. We are adding one more floret with each iteration. But the first floret would always have the shortest radius, unless we reversed the radii list. By reversing the radii list, the first floret will have an increasing radius.

Follow the reasoning above and make sure you understand it. Pseudo-code with comments is provided in the cells below. Using the skeleton, fill out the rest of the code. The code is in two parts:

  1. First, using a for loop, create two lists: thetas and radii. These lists should contain 750 theta and radius values for florets.
  2. Using the pseudo-code provided, create an animation of a growing sunflower.
# Initialize empty lists for thetas and radii
thetas = []
radii = []

# Calculate coordinates for each floret
for i in range(750):
    radius = math.sqrt(i)  # Radius increases with the square root of the index
    theta = i * phi  # Angle increases by the golden angle for each floret
    thetas.append(theta)
    radii.append(radius)
# Imports to get the animation to work
from IPython.display import display, clear_output
import time  

# For animation, you need to call a figure
fig = plt.figure()

for i in range(len(radii)):

    xlist = []
    ylist = []
    
    # This selects the current set of thetas to use
    current_thetas = thetas[0:(i+1)]
    # This selects the current set of radii to use
    # Every time the loop runs, this list will be recreated in the "forward" direction
    # When created, shortest radii are first, longest radii are last
    current_radii = radii[0:(i+1)]
    # After creating the list of radii, "reverse" its order
    # Reversing the order of the radii means that the 
    # florets first specified (the first thetas) have the longest radius
    # After reversing, longest radii are first, shortest radii are last
    current_radii.reverse()

    for n in range (i+1): 
        
        # Here, for each floret n, calculate r, theta, x, y, and append x & y to their lists
        r = current_radii[n]
        theta = current_thetas[n]
        x = r * math.cos(theta)
        y = r * math.sin(theta)
        xlist.append(x)
        ylist.append(y)
    
    # Plot the florets
    plt.scatter(xlist, ylist, color='gold', alpha=0.6, s=10)
    plt.axis('equal')
    plt.axis('off')
    
    # This is the code that creates the animation
    time.sleep(0.001) 
    clear_output(wait=True)
    display(fig)
    fig.clear()

# Closes the figure animation once complete
# plt.close()

애니메이션으로 burst 하는 영상 

 

* 참고 

https://github.com/PlantsAndPython/PlantsAndPython/blob/master/E_3_LOOPS_AND_THE_GOLDEN_ANGLE/2_Modeling_Sunflowers.ipynb

 

sunflower.ipynb
0.11MB