Coding

Programming Trees in Python! (with the turtle graphics library)

  • 00:00:00 hey what's up everyone welcome back to
  • 00:00:01 another video what are we doing today
  • 00:00:03 guys we're interviewing no no no we are
  • 00:00:08 changing the world today that is what
  • 00:00:10 we're doing I'm joined with my friends
  • 00:00:12 who are also changing the word that the
  • 00:00:13 world but yeah if you guys have seen
  • 00:00:16 mark Rober and mr. beasts recent
  • 00:00:19 movement for team trees they're trying
  • 00:00:21 to print 20 million trees by 2020 and I
  • 00:00:25 wanted to join that these guys wanted to
  • 00:00:28 doing that and today we will be using
  • 00:00:30 Python programming
  • 00:00:31 ah to generate some trees do all sorts
  • 00:00:36 of cool stuff with programming to
  • 00:00:37 support the movement and I'll tell you a
  • 00:00:38 little bit more about team trees and
  • 00:00:41 show you how to donate whatnot ok if we
  • 00:00:44 were given this blank file and just how
  • 00:00:47 to implement this draw tree function in
  • 00:00:49 Python it might not be straightforward
  • 00:00:50 how to do that but let's take a step
  • 00:00:52 back and think about how you just doodle
  • 00:00:54 a tree
  • 00:00:54 I am a not good artist but even I can
  • 00:00:57 doodle a tree so if I was to do that it
  • 00:00:59 would look something like this nothing
  • 00:01:03 crazy but right there that's a simple
  • 00:01:05 tree and still if we were trying to
  • 00:01:07 implement that some Python not
  • 00:01:09 straightforward how to do but let's look
  • 00:01:11 at this picture again I'm breaking this
  • 00:01:14 into kind of more programmatic
  • 00:01:15 components well a tree in my eyes is
  • 00:01:17 just a rectangle and then three
  • 00:01:21 triangles stacked on top so if we can
  • 00:01:24 draw a rectangle and three triangles we
  • 00:01:27 can draw a tree we're gonna use the
  • 00:01:29 turtle library of Python and if you've
  • 00:01:32 never heard of turtle I recommend you
  • 00:01:34 check out my video on it it will kind of
  • 00:01:35 give you a bunch of details so you can
  • 00:01:37 more usually if I fall along to this
  • 00:01:39 video but okay so we have the turtle
  • 00:01:41 library imported that's now implement
  • 00:01:43 this chart tree function well as I said
  • 00:01:45 before draw tree will kind of consist of
  • 00:01:47 two different functions probably jaw
  • 00:01:49 trunk and draw Leafs the trunk function
  • 00:01:52 will really just need to implement a
  • 00:01:55 rectangle so let's try do that draw
  • 00:01:58 trunk and we'll have this Tiki and a
  • 00:02:00 turtle width and height and then draw
  • 00:02:05 Leafs
  • 00:02:06 [Applause]
  • 00:02:07 we'll be drawing three different
  • 00:02:09 triangles probably also taking a turtle
  • 00:02:11 at least and I'm just gonna comment this
  • 00:02:15 real quick
  • 00:02:15 draw three triangles pass for now okay
  • 00:02:21 so let's start off with draw trunk how
  • 00:02:23 do I do that well let's first off to
  • 00:02:25 find a turtle that we can use to animate
  • 00:02:28 our tree
  • 00:02:30 we're just call it tert and then usually
  • 00:02:33 when we're doing these turtle things
  • 00:02:34 we'll do
  • 00:02:35 turtle dot done at the end okay so now
  • 00:02:37 we want to test out the draw trunk
  • 00:02:39 method passing that turtle will say our
  • 00:02:43 width is 100 and our height is 200 we're
  • 00:02:45 going to paste in how to draw the trunk
  • 00:02:47 and I'll just animate that because I'll
  • 00:02:50 probably be easier visual but as you see
  • 00:02:52 go forward 100 down 200 over 100 up 200
  • 00:02:57 okay that's good but a trunk a tree
  • 00:02:59 trunk is brown so we also have to
  • 00:03:02 quickly turn our turtle Brown begin fill
  • 00:03:08 and then at the end of it and fill okay
  • 00:03:13 let's see what happens now there cool so
  • 00:03:16 we have a trunk of a tree alright next
  • 00:03:17 we are drawing the leaves well the Leafs
  • 00:03:20 implement triangles so let's actually
  • 00:03:22 write a function that is draw triangle
  • 00:03:26 and that's gonna take in the turtle I
  • 00:03:28 wouldn't just say it takes in the width
  • 00:03:29 and height as well that will probably
  • 00:03:33 mean that the draw leaves has to take in
  • 00:03:34 the width and height okay okay so draw
  • 00:03:38 triangle how do we do that well we
  • 00:03:40 basically just need to turn a try turtle
  • 00:03:42 three times and like move forward a
  • 00:03:44 couple times code for that would look
  • 00:03:46 something like this well we're basically
  • 00:03:49 just defining the different points of
  • 00:03:52 the triangle and then just going to
  • 00:03:55 those different points in the right
  • 00:03:57 sequence and we'll run that real quick
  • 00:03:59 draw triangle turd 100 200 use the same
  • 00:04:06 variables cool we have a rectangle and
  • 00:04:11 triangle and because we're reusing the
  • 00:04:13 same things here let's start abstracting
  • 00:04:16 out a couple things with equals 100
  • 00:04:18 we'll say hi
  • 00:04:19 equals 200 okay
  • 00:04:22 now to draw the Leafs we need to
  • 00:04:25 basically iterate three times so I mean
  • 00:04:27 also pass in the number of triangles we
  • 00:04:29 want this would be three by default so
  • 00:04:33 for I and range triangles basically we
  • 00:04:39 want to iterate for each triangle we're
  • 00:04:40 going to want to draw triangle passing
  • 00:04:44 the turtle the width the height and then
  • 00:04:47 we need to set some sort of like height
  • 00:04:49 increase and I'm going to just Arbor set
  • 00:04:52 arbitrarily set that to height over to
  • 00:04:54 this is how much we're raising the
  • 00:04:55 triangle each time and then we'll set
  • 00:04:59 the turtles y-coordinate to whatever
  • 00:05:01 that y-coordinate was initially I think
  • 00:05:06 this is why core plus the height
  • 00:05:08 increase okay so what happens now if
  • 00:05:11 instead of doing this draw triangle I do
  • 00:05:15 draw Leafs
  • 00:05:18 come on baby
  • 00:05:20 looks good looks good it's good so that
  • 00:05:24 just drew exactly what I was drawing at
  • 00:05:26 the start of the video only issue here
  • 00:05:28 is this is all brown and also the size
  • 00:05:32 was a little bit big so we'll change
  • 00:05:36 this to width comma height width cover
  • 00:05:42 height
  • 00:05:43 let's make this that's a 50 we'll cut it
  • 00:05:47 in half and hundred and then the only
  • 00:05:49 thing else we had to do was also we
  • 00:05:51 should set the color of the turtle will
  • 00:05:53 do copied just how we did it in brown
  • 00:05:55 and I'll pass in green one thing to note
  • 00:05:58 is with turtle II if you set the color
  • 00:06:00 mode to 255 you can use all RGB values
  • 00:06:04 here so that can be useful you'll see
  • 00:06:06 that in my github page that I do do that
  • 00:06:09 yay okay that's a tree we did a tree
  • 00:06:12 let's let's go baby let's go and I guess
  • 00:06:15 the final last detail is that we didn't
  • 00:06:17 want to do draw trunk and jollies we
  • 00:06:19 actually wanted to subs it sub in Jah
  • 00:06:21 tree tert width and height and just make
  • 00:06:26 sure that that's the same thing as it
  • 00:06:28 was before and I'll need update this
  • 00:06:30 real quick with height and updated in
  • 00:06:38 these functions okay
  • 00:06:40 does JA tree work cool cool same as
  • 00:06:45 before okay so that's the tree one thing
  • 00:06:48 to note is that in my github page Keith
  • 00:06:50 Cali / team trees if you go to generic
  • 00:06:54 base tree is basically what we just did
  • 00:06:56 I did do some cleaning up of that so I'm
  • 00:06:59 gonna open that up in sublime basically
  • 00:07:04 what I did to clean it up a little bit
  • 00:07:05 from what you saw was I implemented a
  • 00:07:08 class that was a tree class that had all
  • 00:07:11 those same functions jolliest draw trunk
  • 00:07:14 draw tree but it allowed me to like pass
  • 00:07:16 around some parameters like trunk width
  • 00:07:19 and trunk height I could use easily
  • 00:07:21 without having to pass them into the
  • 00:07:22 draw Leafs and draw triangle function
  • 00:07:24 because it was a class variable I also
  • 00:07:28 did stuff like passing speed and some
  • 00:07:30 sort of scaling factor the reason I
  • 00:07:32 moved into our class was basically it
  • 00:07:34 was more organized to do it kind of
  • 00:07:36 keeps all the logic in one place so if I
  • 00:07:39 ran this you could see same thing
  • 00:07:42 basically I went ahead though and did
  • 00:07:44 something even cooler stuff with the
  • 00:07:48 building off that base tree I created a
  • 00:07:51 line of trees so let's open that file
  • 00:07:53 line of trees
  • 00:07:56 as you can see it's on the screen so fun
  • 00:08:01 gives a little bit of depth to the trees
  • 00:08:03 and I'm like you're using a for loop to
  • 00:08:05 like write more of them so it's kind of
  • 00:08:06 fun so this was pretty straightforward
  • 00:08:10 from just having that base tree as you
  • 00:08:12 see in this file this create forest
  • 00:08:14 functions all I defined and basically
  • 00:08:17 that is just iterating 15 times randomly
  • 00:08:21 kind of scaling up our tree and putting
  • 00:08:25 a position that's kind of dependent on
  • 00:08:27 the width of the tree of the screen
  • 00:08:30 setting a y-value up and down a little
  • 00:08:32 bit and drawing trees 15 times
  • 00:08:36 ultimately with a different scale each
  • 00:08:38 time and then I decided I want to get
  • 00:08:40 even fancier so same within that repo if
  • 00:08:44 you download the forest path code I just
  • 00:08:48 wanted a create kind of little piece of
  • 00:08:50 art using this tree that we've drawn so
  • 00:08:52 that looks like this so I use turtle to
  • 00:08:59 draw the triangles for the grass to draw
  • 00:09:01 the triangle for this like dirt path to
  • 00:09:03 draw the sky and then I use that
  • 00:09:07 basically the create for us or like that
  • 00:09:10 line of trees you just saw create this
  • 00:09:13 like rows of trees so we did that same
  • 00:09:16 line but I did it in multiple places and
  • 00:09:19 then I also have kind of alternated the
  • 00:09:21 color made a random color every time I
  • 00:09:23 generated a tree and got this cool cool
  • 00:09:26 piece of art and you know that's not
  • 00:09:28 much more complex than that basic tree
  • 00:09:30 we did but it's super cool just like
  • 00:09:33 using our programming skills we could
  • 00:09:34 build something like that so all this
  • 00:09:36 code is in my github same thing with the
  • 00:09:41 beasts tree I abstracted out this nature
  • 00:09:44 class that kind of draws the background
  • 00:09:46 and then it basically has this plant
  • 00:09:49 trees function that finds a wide
  • 00:09:52 position plants trees all across that
  • 00:09:56 single Y position and law as long as
  • 00:09:58 it's not in the dirt path
  • 00:10:02 I guess these two together kind of do
  • 00:10:04 what I just mentioned and then I
  • 00:10:06 overrode I did subclass saying
  • 00:10:08 so I had that same base tree but I
  • 00:10:12 overrode the draw tree function of it
  • 00:10:14 and all I did differently in this random
  • 00:10:16 green tree was I passed in a random
  • 00:10:19 color so if you are familiar with RGB
  • 00:10:22 colors red green blue basically I
  • 00:10:24 generated a random color in that green
  • 00:10:27 spectrum passed that in as my draw Leafs
  • 00:10:31 function and that gave me the ability to
  • 00:10:33 do all sorts of different colors in
  • 00:10:35 these trees in this nature scene okay
  • 00:10:39 now that we've gone through some cool
  • 00:10:40 animations of trees how do you actually
  • 00:10:42 get involved in the team trees movement
  • 00:10:44 well there's two ways to support it you
  • 00:10:47 can either go to team trees org or on
  • 00:10:49 any team trees YouTube video so mine
  • 00:10:51 should be included you couldn't click
  • 00:10:53 the donate tab and the right basically
  • 00:10:55 one dollar one US dollar equals one tree
  • 00:10:58 and so as part of this video I'm going
  • 00:11:00 to donate a hundred trees and I think
  • 00:11:02 it's just a cool way like I think mark
  • 00:11:04 rubber put it well in his video is that
  • 00:11:05 you know clenching 20 million trees is
  • 00:11:08 not going to end climate change but it's
  • 00:11:11 like a really positive step in the right
  • 00:11:12 direction and it's a really cool way
  • 00:11:14 that the internet can kind of come
  • 00:11:16 together and do something positive so
  • 00:11:18 I'm like a huge fan of what the movement
  • 00:11:22 is all about so I'm gonna donate a
  • 00:11:23 hundred trees and it's pretty easy
  • 00:11:25 through this slight also don't feel like
  • 00:11:35 you have to donate if you if it's not
  • 00:11:37 manageable one thing I want to do which
  • 00:11:39 will give everyone an opportunity to
  • 00:11:41 donate is if you're inspired after
  • 00:11:44 watching this video if you make some
  • 00:11:45 sort of cool tree visualization you know
  • 00:11:48 kind of building off of something that
  • 00:11:49 maybe I covered in this video or because
  • 00:11:51 something completely different and you
  • 00:11:53 either like tweet that at me or send it
  • 00:11:55 to me on Instagram you know maybe put a
  • 00:11:57 story up on Instagram tag me in it and
  • 00:12:01 do that by the January 1st 2020 I will
  • 00:12:05 kind of selectively pick ones that I
  • 00:12:07 like and i will donate trees on your
  • 00:12:10 behalf and also probably will have some
  • 00:12:14 cool like other prizes you can win to
  • 00:12:16 like maybe
  • 00:12:16 on gift cards so tweet cool
  • 00:12:19 visualizations of trees to me or
  • 00:12:21 Instagram them to me any sort of
  • 00:12:24 messaging platform get them to me and I
  • 00:12:26 would love to share them with all of you
  • 00:12:28 guys and donate trees on behalf of
  • 00:12:31 people that do really cool things all
  • 00:12:33 right I think that's all we're going to
  • 00:12:34 cover in this video guys thank you for
  • 00:12:36 watching thank you for supporting the
  • 00:12:37 team trees movement peace out
  • 00:12:41 [Music]