Make Fractal Art With Python!

Tech With Tim · Intermediate ·⚡ Algorithms & Data Structures ·5y ago

Key Takeaways

This video demonstrates how to create fractal art using Python and the turtle module, with a focus on recursion techniques.

Full Transcript

[Music] hello everybody and welcome to another youtube video so in today's video i'm gonna be showing you how we can create some fractal art using python and the turtle module now specifically we'll be using recursion to actually create these fractals well in case you're unfamiliar with what fractals are let me just show you a really quick example here of what we're actually going to create in this video now what i'm showing you here is really just scratching the surface of what's possible with this kind of method of drawing things and creating these types of patterns you can get really creative with this i actually remember back when i was in about grade 11 i think this was given to us as a school project and we had to create fractal art and i remember people came up with all kinds of really really interesting stuff that uses this kind of method of repeating a pattern over and over again or repeating a series of step steps over and over again sorry to create a pattern or a fractal like this so fractal is a pretty hard thing to define but really as i was saying you're kind of repeating a series of steps and that ends up creating an infinite pattern that kind of looks really interesting and cool in a lot of abstract art you'll see stuff that kind of looks like fractals or may resemble a fractal and well here you can see we have a snowflake and we have a tree these are the two things i'll show you how to create here but as i was saying you can kind of go and extend this and modify these and create some really interesting fractals with this method so that said let's dive into the video after a quick word from our sponsor before we dive in i need to thank typingdna for sponsoring this video typingdna uses typing biometrics to verify users identities and provide a seamless two-factor authentication method typing dna helps you avoid annoying and unnecessary sms authentication messages and only uses sms and email verification as the root of trust not the default authentication method not only are typing biometrics convenient but they're also secure typing dna uses proprietary typing biometrics authentication technology coupled with key phrases of high complexity regarding the number of different characters the way they are dispersed throughout the keyboard layout and the vertical and horizontal travel one of the best parts of typing dna is that it's free and also extremely easy to implement start using typing dna and typing biometrics today to provide a better experience and seamless authentication method for your users check it out from the link in the description and create an account to try out the demo you see here all right so let's just go ahead and dive in here the first thing i'm going to show you is just a little bit about the turtle module so kind of a little summary about that just show you some of the methods and functions that we're going to use here then i will show you how we can build the tree and then how we can make the snowflake now the first thing we need to do here is we need to use the turtle module we need to import the turtle module so i'm going to say from turtle import asterix or import star what this will do is import everything from the turtle module so we can use all of the methods without having to prefix them with turtle dot and then whatever else now i realize i haven't really explained what the turtle module is so this is a 2d graphics module in python allows you to make very simple kind of art i guess you could call it uh or illustrate things or kind of visualize things usually this module is meant for kind of beginners or intermediates but you can make a lot of cool stuff with it super simple it's built into python by default you don't need to install it and actually have an entire tutorial series on my channel showing you how to use the turtle module so if you want to follow along with that if some of this stuff is confusing and you want i guess some information about it beforehand feel free to check that out i'll leave a link to it in the description and put a card to it in the top right hand corner of the video okay so from turtle import star now what i want to do is just give you a quick summary of how the turtle module works the first thing that we need to do here is we just need to type main loop now the reason we need to type this is because what this will do is make it so the turtle module screen or kind of canvas doesn't close immediately as soon as we run the program because right now if we run the program what's going to happen is we're going to import everything from turtle then we're immediately going to end the program so when you put main loop here this will make it so that we kind of stay in the program until we press the x button on the turtle screen so if i run my program now you can see that there's a little x and now when i press x obviously it closes now one last thing i'll note i'm sure some of you guys will ask i'm using sublime text here this is a free text editor you can use this if you want but really the only prerequisites for this video you have python installed and you have some text editor you want to use you can use the default idle that comes with python or you can use whatever one you're comfortable with all right so now we have mean loop okay so when we do main loop you can see it pops up the canvas there and that's where we're actually going to be kind of drawing stuff now the reason this module is called turtle is because you have this little i don't even know what to call it kind of pointer cursor cursor is probably a better word for it and the cursor starts at zero zero in the very middle of the canvas and then you move the cursor around and every time you move it it's going to draw a line based on where you move it so the best way for me to show you this to you is to just move the cursor forward by a certain number of pixels so the first method that i'm introducing here is called forward it takes a number of pixels you do forward number of pixels and then it's going to move your cursor forward that number of pixels now notice how this isn't a turtle but you can actually change this to be a turtle in fact i'll show you how you do that if you do shape and then turtle like this there's a few other ones as well and then you run this notice now this changes to a turtle hence the name of the module regardless though you saw it starts facing right it starts at zero zero so the directions or the coordinate system of turtle is the middle is zero zero as you go up the y increases as you go down the y decreases as you go to the left the x decreases as you go to the right the x increases so it starts facing the right when you go forward it's going to move to the right okay so now i'll show you how we can change the direction of the cursor head so if you wanted to say turn this turtle so it's going to the left what you could do is say left 90 and what this will do is relative to the current position it's facing it will turn the cursor uh 90 degrees to the left so if i do this and then i say forward 100 like that see how we go here then we make a right angle turn and we go left and we go forward by 100 because what we have left 90 forward 100. now you don't have to do them on right angles you could do you know 45 degrees if you do this notice we turn 45 degrees and then we go there all right so that is kind of the basics now in the same sense that there's left there's also right so maybe we go right by 90 after we go left 45 and then we go forward by another 100 there you go you can see we've kind of created the top of a triangle here nice now another method to show you is backward so you can also go backwards in the same way you can go forwards right so i don't think i need to explain what that does you can see that now it goes back nice now there's a bunch of other methods i'll kind of get to them as we go through here but that's the very basics just wanted to give you a quick you know kind of explanation of what's going on here and what we're doing all right so now that we have that what i'm going to do is i'm going to leave the shape equal to turtle but i want to change the speed of this so when you set the speed equal to zero and actually let me go back to what we have before so i can show you what this will do is make the turtle as fast as possible now since we're going to be drawing a lot of stuff it's a good idea to do that otherwise it's going to take a long time so notice i had speed equal to zero and this happened almost instantly obviously because it's very fast now nice okay so now that we have our shape equal to our turtle and speed equal to zero what we can do is start drawing the tree so let's get into that part of the video i'm going to create a function here and i'm going to call this tree and what i want to take for this tree is the size of it the number of levels in this tree and then the angle at which all of the branches should be in this tree so let me just run my previous example here so we can kind of look at the tree that's created and then i'll discuss exactly how we're going to go about making this using recursion so notice here that we kind of have a lot of different levels in this tree right we have our first level which is these two branches where my my mouse is so just these two ones here then off of each of those branches there's two more right and then off of all of those branches there's two more and that's kind of where the recursive aspect comes in here every time we draw a branch we're going to draw two more at the end of that branch until we reach the number of levels that we specified for this tree now i believe the number of levels for this tree is six it's either five or six we have one two three four five okay so there's five levels in this tree this first line doesn't count as a level and anyways that's what a tree of five levels looks like but as you increase the number of levels you increase the number of branches and obviously the tree gets larger and more complex nice so that's kind of the basic so what we want to do is want to go up want to draw a branch to the left draw branch to the right and then off of all of those branches same thing left right left right so on and so forth but how do we actually do that well when we go up we need to determine what direction we want to turn the turtle to the right so at what angle then we need to turn it back to the other side to draw the other branch then we need to draw both of the branches off of those so i'm going to show you how we go about doing that but i just wanted to kind of give you an example here so you can see what it is we're going to be trying to create because it is pretty abstract when i start writing a lot of this stuff so the first thing i need to do here is since it's going to be a recursive function we're going to call this function from inside of itself is i need to define a base case essentially telling this function hey when do i stop when am i allowed to stop recursively calling myself you need a base case if you don't have a base case you're going to run into an infinite loop and you'll have some error pop up saying maximum recursion depth exceeded if you see that that means you didn't implement a base case and your function just never stops running so what i'm going to do here is say if the levels are equal to zero then what i want to do is return and the reason i'm going to do this is because what's going to happen is i'm going to call this function i'm going to pass it some size let's say 90 i'll pass it levels maybe like 6 and then we'll pass some angle let's just say 60 degrees and inside of this function we're going to call itself but every time we call ourself we're going to say with the size levels minus 1 and then the angle now forget about this first and last parameter what you want to look at is this levels parameter here every single time we recall ourself we're going to subtract 1 from levels so eventually we're going to get to a point where we have levels equal to 0 we reach our base case and then we move on with the rest of the program hopefully that makes sense but just trying to give you some explanation here in case you haven't seen recursion a lot before okay so let's keep going here the first thing i want to do inside of this function is i want to go forward by whatever the size of the tree is so this will be responsible for drawing that very first line we saw that first up vertical line but this will also draw all of the branches as well this is actually going to be the uh is this the only forward that we have uh yes this is actually the only four that we have because every single time we get to a point where we're drawing forward we're ready to actually draw a branch and by the way if you see me looking over here i just have another screen open that has the code for this so i don't forget because it's very easy to mess this up there's a lot of stuff that needs to be pretty specific all right so we're going to go forward by the size of the tree and then once we've drawn our current branch what we want to do is draw two more branches one to the left and one to the right so what we're going to do is we're going to go to the right by whatever our angle is and we're going to draw another branch now to draw a branch we're going to call this tree function because inside of the tree function we have this board and then it will draw more branches right so we're going to call tree but this time when we call tree we're going to pass size multiplied by 0.8 so pretty much we want to pass 80 percent of whatever the current size was to be the length or the size of the next tree branch so that they actually get smaller right and it makes sense and then for levels we're going to say levels minus one because at this point we would have already drawn one level and so now we're passing levels minus one to indicate hey now when you go to the next step we're going to be at you know five levels left to draw four levels left to draw so on and so forth and then for the angle we're just going to always pass the same angle unless you want to tweak this feel free to multiply it by a random number or something and you'll see kind of different uh angled branches which is always interesting to do maybe we'll do that yeah okay so we have tree size times 0.8 levels minus 1 angle that is going to draw our right tree branch now we want to draw the left tree branch so to do that we need to go to the left so we're going to say left and we're going to go to the left by angle multiplied by 2. now the reason we're multiplying the angle by 2 is because if we just went to the right by our angle so imagine we're straight we're straight up if we went to the right by some angle i know that's your left but that's my right then if we want to get back to the middle we would have to go the opposite direction by the same angle so if this is 60 degrees and we want to go back to the middle we have to go back 60 degrees so if we want to go to the other side by the additional amount of the angle we need to go two times the angle so from here two times the angle brings us over there hopefully that kind of makes sense i'm sure the hand gestures are a weird way to visualize this but that's why we need angle times two i can actually show you what happens if we don't do that but angle times two is correct okay so now we've gone left what we need to do is draw the other tree branch so we're going to say tree same thing we'll say size multiplied by 0.8 levels minus one and then angle okay and then what we want to do is we want to go to the right by our angle the reason we're doing this is because we're going to straighten ourselves back up now so we've gone to the uh to the right by the angle we've gone to the left by two times the angle and now we're going to the right by the angle so now we're right back in the middle that's kind of where our head is and then we're going to go backwards so that our turtle goes back to where it started from so you'll see why we need this in a second we're going to say backward and then by the size because we went forward by the size right then we turned right then we drew the next tree branch this when we have this recursive call here is going to draw the entire right subtree then what we're going to do is go to the left draw the entire left subtree then we're going to strain ourselves back out and we're going to come back now at this point in time this will actually work i'm just going to show you what this looks like so that we can see what we've done and then i'll explain exactly how this works so let's draw a tree we just write tree size give it whatever you want i'm gonna give it 70. levels i'd recommend you go with something like under eight if you go eight or above it's just going to take a really long time to draw and then angle 60 is a good angle but you can go with any angle that you'd like but i would recommend making it above 20 otherwise you're going to see a very kind of squish tree anyways let's just run this and let's see what we've done here so notice that when i do this we get this kind of strange looking tree right and it's even more weird looking because of the fact that it is sideways so what i'm going to show you how to do now is turn it to the left so the first thing we're going to do before we even draw the tree is we're just going to go left 90 degrees so left 90. so now when we actually draw the tree it doesn't look quite as strange so there you go we got a tree now the reason this looks so weird is because the angle we gave here was 60 degrees and so at every single kind of point where we need to draw another branch we were going off by 60 degrees and so here you can see this is 120 degree angle because well 60 times 2 right and so if we want to make this look a little bit more normal we can decrease the angle and make that something like 30 and now we can see that this tree is looking a bit more as we would expect right that is really how you go about drawing the tree now i'm just going to change the speed here so that it's one so it's the slowest it can possibly be so we can actually see what goes on here so notice we went up we want right we want right we want right we went right then we went back then we went left then we went back then we went left right then we went right left then we went back just kind of watch what goes on here as we draw this tree it draws the branch then it returns back to its original position then it draws the other side it draws that other branch and that's why we had that backwards there and then notice once we get here it goes all the way back to the very beginning the reason why we needed this right the reason why we need it backward is so that we return back to the original position before we try to draw the next branch because if we don't do that if i don't include this backwards here watch what happens you're going to see we get a very strange looking tree that kind of just goes off in random directions right because we're not returning back to the point where we were going to draw that next branch and so that's why we need the backwards there and same thing with you know left angle times 2 if i don't have times 2 and i run this you're going to see what happens we get something that looks like this again a very kind of strange looking thing if you actually run this you'll notice this pattern does kind of repeat and it makes something that looks you know somewhat interesting but obviously that's not a tree that's not what we wanted okay i'm going to stop running that all right so hopefully that kind of explained why we need the backward recursion is a very difficult thing to explain especially when you have a lot of different calls but let's just run through this to make sure it's super clear so we have tree right we're taking size levels angle obviously we understand the base case if level is equal to zero then we're not doing anything we will do something in a second to have those little green bubbles but skip that for now all right so then inside of this the first thing we do is we go forward by whatever the size of our branch is this is good because this means that after we go right and after we call this tree function again we immediately draw the branch that we had wanted to draw right we called tree and then we're already angled the right direction so we just go forward by whatever the size was that was past this function and we draw that first branch that's why that first one is vertical because the first thing we do here is we just go straight up and we draw that branch okay then we turn to the right this is so that we can draw the right subtree so all of the right subtree now once we hit this function here what happens is we go back up here right we go back into this recursive function so now we're calling with size times 0.8 so 80 percent the size one less level and the same angle now we're already turned to the right because in the previous step we had turned to the right so the first thing we do is we go forward right and then we turn right again so we angle by 30 degrees and then we call this recursive function and we draw the right subtree of the right subtree hopefully that makes sense but we draw the right subtree of the right subtree then we get back into this function right so now we go forward again we're already angled the right way we go forward we turn right and then we draw the right subtree of the right subtree of the right subtree and this repeats until obviously we get through all of the levels now as soon as we're at the point where levels is equal to zero what that means is the function that called this we can now go to the next step because we've gone through all of the recursive calls so now we go to the next step and we say okay well we had turned right by a ton so now we need to turn left by the angle times two to draw the left subtree of whatever function just finished so then we go back we return to the left and then we draw the left subtree and now the process repeats here except with the left subject so now we draw the left subtree so we come back up here we say level is equal to zero no it's not so we go forward in whatever the direction was that we were pointed then we turn to the right and we draw the right subtree of the left subtree and then we continue and continue and continue and continue and eventually we get to a point where we have drawn the left subtree of some left subtree and now levels is equal to zero so as soon as we get to the point where this function is done and we return to wherever this was called from now we turn right so we go back straight right and then we go backwards and we return back to the original position that we were at so again the reason we need this is because remember in the first step here we went forward so after we do all of this stuff we need to go backwards to make sure we go back to where we started from so then when we draw the next subtree we're not drawing it from the end of the right subtree that corresponds with the left subtree we're drawing it from kind of the origin of where this function was called from this is very confusing obviously to explain but if you do this in the very slow speed you can kind of see how this works and and why we actually get the tree that we do okay perfect so i'm going to change the speed back to 0 here unfortunately that's the best explanation i can give you let me know in the comments if that was sufficient but what i'm going to show you now is how we can actually draw those kind of green little bubbles right so what i'm going to do here is i'm going to change the color of my turtle to be green so you say color put it in a color there's you know some preset ones one of them is green so we do in a string green that changes the color of our cursor to green then what we can do is say dot what we can actually do here is we can pass whatever the size is that we're currently on so when we get to the last level and we will make a green dot with the radius of this size so we can do that and then we can say color and we can make this black all right so now if we run this you're going to notice that as soon as we get to the point we're at the end of our branch so we reach our base case we do that little dot right and i just want to show you what happens when i increase the number of levels so instead of 5 i go to 7. so now when i go to 7 notice how we're going to get a more complex tree and obviously you guys are welcome to mess around with whatever number of levels that you want and change the angle and maybe you can implement some type of random stuff in here change the color of the branches goham do whatever you'd like i'm just showing you kind of the basic one you guys can extend an add-on from that but there you go a beautiful tree nice okay now we have that let's do the snowflake now the snowflake is i don't want to say more complicated but it's very different how we draw the snowflake so let me just run that previous program here so we can actually have a look at this snowflake uh and actually sorry i'm just going to change this so it's a little bit of a more simple example so that we can have a better look at how this works so i'm just looking at my other script here bear with me okay let's run this there you go that's what i want to look at okay so ignore the tree just look at the snowflake for now and see if you can kind of pick up the pattern that's going on here now it's fairly obvious but this has to do with triangles so the way that this snowflake is actually going to work is we are going to draw three sides of the triangle so the first side is going to go from where this uh this red cursor is to where my mouse is the second side will go from where my mouse is to kind of this point right here and then the third side will go from my mouse to this cursor here so we're going to draw three sides all three of these sides will be generated with the recursive function and we'll kind of connect them together in the sense of a triangle right drawing three sides of the triangle now the way this recursive function is going to work is it's going to draw one of these sides and in fact let me show you what happens when i draw just one side so you can kind of see what it looks like all right so this is what one side of our snowflake looks like now as i was saying we're going to connect three sides together in kind of a triangle form so we'll connect the sides at 60 degree angles so that it creates the snowflake but just examine and kind of look at this pattern here and i'll show you obviously how we make this but the way it works is we draw one small triangle we draw one large triangle and then one small triangle but on the large triangle we draw smaller triangles and this large triangle is comprised of smaller triangles the way it works is we have these three small triangles making up this one larger and so this is with a depth of two now watch what happens when i change the depth here to b1 so when i change the depth to b1 notice how we just do one large triangle right and now if i change the depth to b3 what happens is we have a bunch of triangles right so we've kind of replicated that smaller side you saw so with the depth of two we put those on both sides and then we have one kind of a larger triangle with a bunch of triangles on top of it in the middle so that's kind of the way this works i'm just going to walk you through the process of making this and then obviously we can run it we can see what it looks like and i can explain how this works but that's kind of the pattern there so we draw one of those sides once we can figure out how to do one side it's pretty easy to connect them together and we can connect you know a bunch of different sides together it doesn't just have to be three sides okay so we have all of this stuff for tree what i'm going to do is make another function here and i'll just call this snowflake like that and actually in fact this function is going to be called create snowflake because we're going to have two functions one that will create the snowflake side and one that actually creates the snowflake as a whole so inside of this create snowflake function i'm not going to do anything for right now i'm going to start by just making my snowflake function really this will be a snowflake side so the snowflake side function what this is going to take is a length and again the number of levels that we want to have or kind of the depth i guess we could say you know we'll leave it as levels for this side so inside of here same thing as before in our base case we're going to say if levels is equal to zero then what we're going to do is return but before we do that we're going to go forward by the length now it's a little bit different than what we did inside of this tree function here where we started by going forward here we're going to end by going forward now you'll see how this works in a minute but just kind of bear with me so i'm going to say length divided by equals and then 3.0 i'm then going to say snowflake side so i'm recursively calling this function and what i'm going to do is pass the length and the levels minus 1. then what i'm going to do is turn the cursor so i'm going to go left by 60 degrees i'm then going to call snowflake side again and i'll actually just paste this because we're going to do this four times i'm going to go right by 120 degrees i'm going to call snowflake's side again and then i'm going to go left by 60 degrees and this is actually all we need to do to create a snowflake side so let me just run this function and then i'll walk you through exactly how this works so let's remove this tree for now and let's remove this left and let's just say snowflake side and let's simply pass this a length i guess we'll give it a length of maybe 200 and then for the levels we'll just pass two all right so let's run this okay so let's have a look at this alongside the code just to see exactly what goes on here so we start in our very first recursive call by dividing the length by three then we see this recursive call snowflake side so what we do is we pass the length which is now divided by three and levels minus one so now we come back up here and at this point in time the length is equal to you know three times less whatever the original length was and the levels is equal to one because originally levels was equal to 2. all right we then divide the length by 3 again we then call snowflake side so now the length is even smaller we've divided by 3 two times now levels is equal to zero so at this point we see levels equal to zero so we hit our base case so we go forward by the length that we have which at this point in time is going to be 1 9 of our total length so you can see that's kind of this first line right here is because this first recursive call has now finished all right so now we are done all of those so what happens is the last place to call this we now return back to here and now we turn left by 60 degrees so we turn left by 60 degrees at this point when we're kind of at this next recursive call our length has been divided by 9 because three two times and then we call snowflake side so we turn left by 60 degrees then we come back up to levels levels is now equal to zero and so what that means is we're in our base case so we go forward and we draw this kind of line right here okay so now we finish this recursive call so we go and we see right 120 so we turn right by 120 degrees and then we call snowflake side again now what happens is well levels is equal to zero because levels was equal to one now it's equal to zero so when we're down in our base case we go down okay then what we do is this recursive call is finished so we go left by 60 degrees so that kind of allows us to turn in this direction we call snowflake side and obviously level is equal to zero so we go four all right so now we've kind of finished these four lines right here so this first kind of triangle that's drawn with the two lines on the side that's done all right so we've now finished this entire first call here so line 32 that's done so now we're back at kind of this original top level call to this function and now what we're doing is we're turning left by 60 and then we're calling snowflake side this time we're going to be passing levels equal to 1 not 0 because remember at the top level call so levels is equal to two we subtract one from it at the function call so we turn left by 60. that gives us this kind of angle right here hopefully you guys can see my mouse and now we call snowflake sides so we call snowflake sides levels is equal to one we're not in the base case and so now we repeat this entire process again and that's what then allows us to draw this line this line this line this line notice that this right here it's really hard to show you this but you can see my mouse kind of going over this line that's the same as this so really all we're doing is combining this one kind of very standard pattern which is that triangle with the two lines together we're just combining that at a bunch of different angles such that it creates this kind of pattern so i think i'm going to stop explaining it there because it's really difficult to explain the recursive calls i'm sure i'm confusing a lot of you guys but now we know how to do one sign so now we know how to do one side we need to figure out how we can combine the sides but i'm just going to show you what happens if i make the step equal to like 4 for example so if i do this like notice how small this gets and how advanced this is going to look obviously as you increase increase the depth it exponentially is going to have more and more lines and stuff that's going on just want to show you that okay so now that we've done one side as i was saying we need to do all of it so how do we do all of it well we just need to draw three sides and connect them together but i don't want to keep it super simple and just do three sides i want to show you how we can do any number of odd numbered sides okay so for this function what i want to take and i just want to look at my cheat sheet here is the signs and the length for the snowflake that i want to create and what i'm going to do is say for underscore in range the number of sides that we pass what i want to do is draw a snowflake side and what am i going to pass to snowflake side i'm going to pass my length and i'm going to pass the number of sides that i have as the number of levels that i want to draw for my snowflake side then what i'm going to do is i'm going to turn right by and this is going to be 360 divided by the number of sides so what this is going to do is i mean i could run this and show it to you but what this is going to do is draw the number of sides that we pass and it's going to kind of rotate where the next side is drawn based on how many sides that we have so in a circle we have how many degrees we have 360 degrees which means if we want to have these sides connected all the way around we need to angle them at whatever 360 divided by the number of sides that we have is so i'll show you how this works with three sides so rather than doing snowflake side let's do create underscore snowflake we're going to pass for the sides 3 we'll pass for the length 200 and yeah okay that works so let's just run this and look at what we get here so we do one side that's one side we just did a second side and now we're doing a third side right there and for me to actually show you better when or where the sides are created what i will do here is show you how we can change the color so i'm just going to do this quickly because it's more for illustration here but let's say green uh blue and yellow okay and we're just gonna say color is equal to colors at i plus one okay you don't have to understand what i just did here but this is going to just gonna do different colors so let's run this and you'll see where the different sides are oops index out of range oh crap that's my bad sorry it needs to be that okay so now you can see these are our three sides and notice that when it gets to this point what actually happens is the turtle turns 120 degrees because we had three sides so 360 over 3 is 120 draws the next side turns 120 degrees again then draws the other side now let's see what happens if we draw four sides so if i draw four sides i'm actually nervous to see if this is going to work or not because i haven't tried four sides yet first of all this is going to take a second so once this is done i'll be right back all right so i decided to change something to make sure this worked but you can see that we've drawn this with four sides i just added another color which was orange but this works with four sides and if you go with five it's going to work as well the way that i've kind of programmed this out it's dynamic so you can just change the number of sides and well that will change this the shape of the snowflake but that is how that works right now here rather than turning 120 degrees we're turning at 90 degrees right you can kind of see there's this little interior square if you were to draw a square inside of here it'd be a perfect square because once we get here we turn 90 90 90. and we're good we have connected on all sides so that is how you do the snowflake that's pretty much all i needed to show you now i mean i could continue going through this and explain the recursion but i think that's pretty much all you guys need to know if you wanted to make multiple snowflakes you could call this multiple times but you would probably want to move the turtle to different positions now i'll just show you um like how you do this i think you can say move or maybe it's two pause uh there's a way to set the position i think i have it oh it's called go to if you want to change the position of the turtle what you can do is say go to xy and then we'll actually go to that position uh so i just wanted to show you that so you guys can see how you can draw multiple snowflakes draw a snowflake move the turtle draw another one move the turtle draw another one so on and so forth all right but that is it i will leave a link to this code in the description if i forget someone please do remind me uh but yeah that's really how you can mess around with fractals and recursion in python using the turtle module i'll zoom out so you guys can read all of the code if you would like to do that if you're not on a mobile device i guess because it's pretty hard to read that and yeah there you go now as i was saying this is kind of just scratching the surface of what's possible with this you can imagine if i made this slightly random if i didn't make everything you know perfect angle stuff would be off a little bit but you'd be able to make some really interesting stuff and if you were to add to this maybe rather than doing two tree branches you do three four or five tree branches you're going to see how much more advanced and interesting these shapes get and you can just mess around with different angles different sizes and you'll see you get some really really interesting pictures all right so with that said i think i'm going to end the video here hope you guys enjoyed if you did make sure that like subscribe to the channel and i will see you in another one

Original Description

Welcome back to another YouTube video! In this video, I will be showing you how to create fractal art using Python and the turtle module. More specifically, we'll be using recursion to create the fractals. ⭐️ Thanks to TypingDNA for sponsoring this video! Sign up to create a free account here: https://www.typingdna.com/clients/?view=verify-api and view all product information here: https://www.typingdna.com/verify 📄 Resources & Sources 📄 GitHub Link: https://github.com/techwithtim/Python-Fractal-Art/tree/main Python Turtle Tutorial Series: https://www.youtube.com/watch?v=p7CiFhiTdvY Download Sublime Text: https://www.sublimetext.com/ ⭐️ Timestamps ⭐️ 00:00 | What We're Building 02:24 | Turtle Module Walkthrough 07:23 | Fractal Tree 21:11 | Fractal Snowflake ◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️ 💰 Courses & Merch 💰 💻 The Fundamentals of Programming w/ Python: https://tech-with-tim.teachable.com/p/the-fundamentals-of-programming-with-python 👕 Merchandise: https://teespring.com/stores/tech-with-tim-merch-shop 🔗 Social Medias 🔗 📸 Instagram: https://www.instagram.com/tech_with_tim 📱 Twitter: https://twitter.com/TechWithTimm ⭐ Discord: https://discord.gg/twt 📝 LinkedIn: https://www.linkedin.com/in/tim-ruscica-82631b179/ 🌎 Website: https://techwithtim.net 📂 GitHub: https://github.com/techwithtim 🔊 Podcast: https://anchor.fm/tech-with-tim 🎬 My YouTube Gear 🎬 🎥 Main Camera (EOS Canon 90D): https://amzn.to/3cY23y9 🎥 Secondary Camera (Panasonic Lumix G7): https://amzn.to/3fl2iEV 📹 Main Lens (EFS 24mm f/2.8): https://amzn.to/2Yuol5r 🕹 Tripod: https://amzn.to/3hpSprv 🎤 Main Microphone (Rode NT1): https://amzn.to/2HrZxXc 🎤 Secondary Microphone (Synco Wireless Lapel System): https://amzn.to/3e07Swl 🎤 Third Microphone (Rode NTG4+): https://amzn.to/3oi0v8Z ☀️ Lights: https://amzn.to/2ApeiXr ⌨ Keyboard (Daskeyboard 4Q): https://amzn.to/2YpN5vm 🖱 Mouse (Logitech MX Master): https://amzn.to/2HsmRDN 📸 Webcam (Logitech 1080p Pro): https://amzn.t
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Tech With Tim · Tech With Tim · 0 of 60

← Previous Next →
1 A* Path Finding Algorithm(Visualization)
A* Path Finding Algorithm(Visualization)
Tech With Tim
2 Python Programming Tutorial #1 - Variables and Data Types
Python Programming Tutorial #1 - Variables and Data Types
Tech With Tim
3 Python Programming Tutorial #2 - Basic Operators and Input
Python Programming Tutorial #2 - Basic Operators and Input
Tech With Tim
4 Python Programming Tutorial #3 - Conditions
Python Programming Tutorial #3 - Conditions
Tech With Tim
5 Python Programming Tutorial #4 - IF/ELIF/ELSE
Python Programming Tutorial #4 - IF/ELIF/ELSE
Tech With Tim
6 Python Programming Tutorial #5 - Chained Conditionals and Nested Statements
Python Programming Tutorial #5 - Chained Conditionals and Nested Statements
Tech With Tim
7 Python Programming Tutorial #6 - For Loops
Python Programming Tutorial #6 - For Loops
Tech With Tim
8 Python Programming Tutorial #7 - While Loops
Python Programming Tutorial #7 - While Loops
Tech With Tim
9 Python Programming Tutorial #8 - Lists and Tuples
Python Programming Tutorial #8 - Lists and Tuples
Tech With Tim
10 Python Programming Tutorial #9 - Iteration by Item (For Loops Continued...)
Python Programming Tutorial #9 - Iteration by Item (For Loops Continued...)
Tech With Tim
11 Python Programming Tutorial #10 - String Methods
Python Programming Tutorial #10 - String Methods
Tech With Tim
12 How to Overclock a NVIDIA GPU
How to Overclock a NVIDIA GPU
Tech With Tim
13 Python Programming Tutorial #11 - Slice Operator
Python Programming Tutorial #11 - Slice Operator
Tech With Tim
14 Python Programming Tutorial #12 - Functions
Python Programming Tutorial #12 - Functions
Tech With Tim
15 Python Programming Tutorial #13 - How to Read a Text File
Python Programming Tutorial #13 - How to Read a Text File
Tech With Tim
16 Python Programming Tutorial #14 - Writing to a Text File
Python Programming Tutorial #14 - Writing to a Text File
Tech With Tim
17 Python Programming Tutorial #15 - Using .count() and .find()
Python Programming Tutorial #15 - Using .count() and .find()
Tech With Tim
18 Python Programming Tutorial #16 - Introduction to Modular Programming
Python Programming Tutorial #16 - Introduction to Modular Programming
Tech With Tim
19 Python Programming Tutorial #17 - Optional Parameters
Python Programming Tutorial #17 - Optional Parameters
Tech With Tim
20 Python Programming Tutorial #18 - Try and Except (Python Error Handling)
Python Programming Tutorial #18 - Try and Except (Python Error Handling)
Tech With Tim
21 Python Programming Tutorial #19 - Global vs Local Variables
Python Programming Tutorial #19 - Global vs Local Variables
Tech With Tim
22 Python Programming Tutorial #20 - Classes and Objects
Python Programming Tutorial #20 - Classes and Objects
Tech With Tim
23 Cool VBS Script to Prank Your Friends!
Cool VBS Script to Prank Your Friends!
Tech With Tim
24 How to Overclock an AMD GPU
How to Overclock an AMD GPU
Tech With Tim
25 Best GPU'S For Mining Ethereum (2018)
Best GPU'S For Mining Ethereum (2018)
Tech With Tim
26 Recursion and Memoization Tutorial Python
Recursion and Memoization Tutorial Python
Tech With Tim
27 Ethereum Mining Rig - Hardware Guide
Ethereum Mining Rig - Hardware Guide
Tech With Tim
28 Pygame Tutorial #1 - Basic Movement and Key Presses
Pygame Tutorial #1 - Basic Movement and Key Presses
Tech With Tim
29 How to Install Pygame (Windows 8/10)
How to Install Pygame (Windows 8/10)
Tech With Tim
30 How to Trade Your Cryptocurrency (Bitcoin, Ethereum etc.) For Cash!
How to Trade Your Cryptocurrency (Bitcoin, Ethereum etc.) For Cash!
Tech With Tim
31 How to Mine Ethereum 2018 - WORKING (Super-Easy)
How to Mine Ethereum 2018 - WORKING (Super-Easy)
Tech With Tim
32 Microphone Comparison - $10 Mic vs $150 Mic (Blue Yeti USB)
Microphone Comparison - $10 Mic vs $150 Mic (Blue Yeti USB)
Tech With Tim
33 Pygame Tutorial #2 - Jumping and Boundaries
Pygame Tutorial #2 - Jumping and Boundaries
Tech With Tim
34 Pygame Tutorial #3 - Character Animation & Sprites
Pygame Tutorial #3 - Character Animation & Sprites
Tech With Tim
35 Pygame Tutorial #4 - Optimization & OOP
Pygame Tutorial #4 - Optimization & OOP
Tech With Tim
36 OBS Studio Tutorial - Best OBS Settings
OBS Studio Tutorial - Best OBS Settings
Tech With Tim
37 Linear Search Algorithm - Python Example and Code
Linear Search Algorithm - Python Example and Code
Tech With Tim
38 Make Any Mic Sound AMAZING! (WITH OBS)
Make Any Mic Sound AMAZING! (WITH OBS)
Tech With Tim
39 Binary Search Algorithm - Python Example & Code
Binary Search Algorithm - Python Example & Code
Tech With Tim
40 Pygame Tutorial #5 - Projectiles
Pygame Tutorial #5 - Projectiles
Tech With Tim
41 Pygame Game - Mini Golf
Pygame Game - Mini Golf
Tech With Tim
42 Pygame Tutorial - Projectile Motion (Part 1)
Pygame Tutorial - Projectile Motion (Part 1)
Tech With Tim
43 Pygame Tutorial - Projectile Motion (Part 2)
Pygame Tutorial - Projectile Motion (Part 2)
Tech With Tim
44 Pygame Tutorial #6 - Enemies
Pygame Tutorial #6 - Enemies
Tech With Tim
45 Pygame Tutorial #7 - Collision and Hit Boxes
Pygame Tutorial #7 - Collision and Hit Boxes
Tech With Tim
46 Pygame Tutorial #8 - Scoring and Health Bars
Pygame Tutorial #8 - Scoring and Health Bars
Tech With Tim
47 Cloud Mining vs. Hardware Mining - 2018
Cloud Mining vs. Hardware Mining - 2018
Tech With Tim
48 How to Install Pygame on Mac OSX (Fast-Simple)
How to Install Pygame on Mac OSX (Fast-Simple)
Tech With Tim
49 Pygame Tutorial #9 - Sound Effects, Music & More Collision
Pygame Tutorial #9 - Sound Effects, Music & More Collision
Tech With Tim
50 Pygame Tutorial #10 - Finishing Touches & Next Steps
Pygame Tutorial #10 - Finishing Touches & Next Steps
Tech With Tim
51 How to Fade Your Screen in Pygame [CODE IN DESCRIPTION]
How to Fade Your Screen in Pygame [CODE IN DESCRIPTION]
Tech With Tim
52 How to Create a Button in Pygame [CODE IN DESCRIPTION]
How to Create a Button in Pygame [CODE IN DESCRIPTION]
Tech With Tim
53 Pygame Side-Scroller Tutorial #1 - Scrolling Background/Character Movement
Pygame Side-Scroller Tutorial #1 - Scrolling Background/Character Movement
Tech With Tim
54 Pygame Side-Scroller Tutorial #2 - Random Object Generation
Pygame Side-Scroller Tutorial #2 - Random Object Generation
Tech With Tim
55 Pygame Side-Scroller Tutorial #3 - Collision
Pygame Side-Scroller Tutorial #3 - Collision
Tech With Tim
56 Pygame Side-Scroller Tutorial #4 - Scoring and End Screen
Pygame Side-Scroller Tutorial #4 - Scoring and End Screen
Tech With Tim
57 How to Create A Message Box in Python - Tkinter
How to Create A Message Box in Python - Tkinter
Tech With Tim
58 Is Ethereum Mining Still Profitable - Is It Worth It (April 2018)
Is Ethereum Mining Still Profitable - Is It Worth It (April 2018)
Tech With Tim
59 How to Run MAC OSX on a WINDOWS PC (Clover Boot-loader)
How to Run MAC OSX on a WINDOWS PC (Clover Boot-loader)
Tech With Tim
60 Programming Problem #1 - Alphabet Soup (Beginner/Novice)
Programming Problem #1 - Alphabet Soup (Beginner/Novice)
Tech With Tim

This video teaches you how to create stunning fractal art using Python and the turtle module, with a focus on recursion techniques. By following along, you'll learn how to implement recursive algorithms and create visually striking fractals. The video is sponsored by TypingDNA, and viewers can sign up for a free account to explore more coding resources.

Key Takeaways
  1. Import the turtle module
  2. Define a recursive function to draw fractals
  3. Use the turtle module to create fractal patterns
  4. Experiment with different recursion depths and angles
  5. Customize the fractal art with various colors and shapes
  6. Save the fractal art as an image file
  7. Share the fractal art on social media or with the coding community
  8. Explore more advanced recursion techniques
  9. Apply the recursion techniques to other areas of coding
  10. Join the TypingDNA community to access more coding resources
💡 Recursion is a powerful technique for creating fractal art, and Python's turtle module provides a simple and intuitive way to implement recursive algorithms and create stunning visual patterns.

Related Reads

📰
75 Days of Leetcode — Day 8: #33 — Search in rotated sorted array
Solve the Search in Rotated Sorted Array problem on Leetcode to improve coding skills and learn algorithms
Medium · AI
📰
DP Patterns Unlocked — Palindrome Partitioning II: Interval DP With a Palindrome Constraint
Learn to solve Palindrome Partitioning II using interval DP with a palindrome constraint, a variation of the familiar DP pattern
Medium · Programming
📰
DP Patterns Unlocked — Palindrome Partitioning II: Interval DP With a Palindrome Constraint
Learn to solve Palindrome Partitioning II using interval DP with a palindrome constraint, a variation of the familiar DP pattern
Medium · JavaScript
📰
DP Patterns Unlocked — Matrix Chain Multiplication: Interval DP Hiding in Plain Sight
Learn how to apply Interval Dynamic Programming to solve Matrix Chain Multiplication problems efficiently
Medium · Programming

Chapters (4)

| What We're Building
2:24 | Turtle Module Walkthrough
7:23 | Fractal Tree
21:11 | Fractal Snowflake
Up next
Find the Median in a Data Stream (Two Heaps Trick)
KodeKloud
Watch →