I opened the Tree Example, saved the script pic, uploaded it to ChatGPT 4 and asked "Can you show me pseudo code for this Snap! program?"
Here is the response:
Certainly! The Snap! program you've uploaded appears to be a script to draw a fractal tree, with interactivity based on mouse clicks and the level of detail that can be adjusted. Below is a pseudo-code representation of the Snap! blocks:
// Initialize when green flag clicked
Clear screen
Lift pen up
Move to starting position (x: 0, y: -150)
Make the pen visible
Set levels to 3
Store current levels in oldLevels// Draw initial tree
Put pen down
Set pen color
Set branches (not shown what it's set to, assumed it's related to tree)
Point in the initial direction (0 degrees)
Draw tree with size 100 and levels// Main loop
Loop forever {
If levels not equal to oldLevels or mouse is down {
Set oldLevels to current levels
Clear screen
If levels is greater than 6 {
Hide pen
Draw tree with warp effect (instant drawing)
Show pen
} else {
Draw tree normally
}
}If mouse is down and mouse x is within -10 to 10 range and mouse y is less than -47 {
Broadcast mouse event and wait
Draw tree with size 100 and levels
}
}// Function to draw a tree (details are not provided in the image)
Function tree(size, levels) {
// Recursive function to draw branches
}In the pseudo-code above, the
tree
function is assumed to be a recursive function that draws branches based on thelevels
andsize
parameters, which is a typical approach for drawing fractal trees. The exact implementation details of thetree
function would depend on the fractal algorithm used, which is not visible in the image you've provided.
I then uploaded the script pic of the tree function:
// Define the tree function with size and depth as parameters
Function tree(size, depth) {
// Initialize height variable
Declare height// Set the pen size relative to the size parameter
Set pen size to size / 5// Set the pen brightness based on the size
Set pen brightness to 40 - (size / 4)// If we are at the smallest detail level and the levels are above 3, set the pen color differently
If depth equals 1 and levels greater than 3 {
Set pen color to a specific color
}// Move the pen by 'size' steps to draw the trunk
Move pen size steps forward// Set the height to the size for later use
Set height to size// If depth is greater than 1, we need to draw more branches
If depth greater than 1 {
// Loop through each item of branches (assuming branches is a list of branch parameters)
For each item in branches {
// Move forward by a scaled amount of the current item and adjust for height
Move pen forward by (round(item 1 of branches) * size / 100 - height) steps// Update the height for the next branch Change height by (round(item 1 of branches) * size / 100) - height // Turn the pen by the second item of branches (angle) Turn pen by (round(item 2 of branches) degrees) // Recursively call tree function with a smaller size and one less depth level Tree(round(item 3 of branches) * size, depth - 1) // Turn the pen back by the second item of branches (angle) to original position Turn pen by (round(item 2 of branches) degrees) in the opposite direction }
}
// Reset the pen size to original
Set pen size to size / 5// Reset the pen color and brightness to initial values
Set pen color to initial color
Set pen brightness to 40 - (size / 3)// Move the pen back to the starting point of the branch
Move pen backwards by height steps
}
I then asked for natural language pseudo code:
Certainly! Let's describe the process in natural language pseudo-code, which is more like a set of instructions you might give someone who doesn't know programming:
Start the tree-drawing function with two pieces of information: the thickness of the trunk (size) and how many mini-branches we want (depth).
Make the drawing pen a bit thinner by dividing the trunk's thickness by 5. This will be the thickness of our branches.
Make the color of the pen a bit lighter based on how thick the trunk is. The thicker the trunk, the darker the pen.
Now, if we're drawing the very tiny branches at the top and we have more than three levels of branches:
Change the pen to a special color for these tiny branches.Draw the trunk of the tree by moving the pen forward by the trunk's thickness.
Remember how tall the trunk is by saving its thickness in a memory spot called 'height'.
Now, if we're supposed to draw more branches coming out of the trunk:
Look at each set of instructions for the branches, which tell us how long and at what angle to draw them.For each branch: Move the pen forward a little, but not as much as the trunk's thickness, and adjust this based on the height we remembered. Change the height memory by the same amount we just moved. Turn the pen to the angle that we need for this branch. Call the tree-drawing function again to draw this branch, but make it smaller and with one less level of mini-branches. After drawing this branch, turn the pen back to where it was before drawing the branch.
Make the pen thickness back to what it was after drawing the trunk.
Return the pen to the original color and make it a bit lighter again, just like we did at the start.
Move the pen back to where we started drawing the trunk.
That's it! We keep doing this, and we'll have a whole tree with lots of branches!
This description avoids technical terms and focuses on the logical steps of drawing a fractal tree, which involves recursive drawing of branches at decreasing sizes and depths.
Before uploading the 'tree' function I entered "Can you explain to a child how it works please". Here's the response:
Here's how the magic drawing works with your magic computer program:
Starting the Magic: When you click the green flag, it's like saying "Abracadabra!" The magic program gets ready by clearing anything that was on the screen before, moving to the spot where it will start drawing the tree, and getting the magic pen ready to draw.
Drawing the Tree: The magic pen starts at the bottom and draws a straight line. Then, at the end of that line, it splits into two smaller lines, just like the branches of a tree. And those branches split again, and again, until you have a whole tree!
Changing the Tree with Magic Words (or Clicks): If you tell the program to change how many times the branches split by clicking your mouse or changing a number, it listens to you! It erases the old tree and starts over, drawing a new one with more or fewer branches, just like you asked.
The Magic of Speed: If the tree gets very big and has lots of branches, it takes longer to draw. But this program can use a special spell called "warp" to draw it super fast, so you don't have to wait!
Listening to Your Commands: The program is always listening. If you click your mouse in a special spot, it knows you're asking to change the tree, and it does it right away!
So, you see, it's like having a magic paintbrush that can draw an entire tree with branches that look like tiny trees, and it changes whenever you want, just by telling it to or clicking the mouse! Isn't that fun?