More Drawing (Video Version)
-
0:01 - 0:03Let's explore more
of this whole drawing thing. -
0:03 - 0:05What can we make besides rectangles?
-
0:05 - 0:08We can make ovals using the word ellipse,
-
0:08 - 0:10which is another command
the computer knows. -
0:10 - 0:13We actually have a special
programming word for commands. -
0:13 - 0:15We're going to call them functions.
-
0:15 - 0:18I'll be using the word function
from now on just to mean command. -
0:19 - 0:21We'll go ahead and write
the function name ellipse, -
0:21 - 0:23and then ( ) and a ;
-
0:23 - 0:24It's not working!
-
0:24 - 0:26We have this error message
talking about parameters, -
0:26 - 0:27whatever those are.
-
0:27 - 0:30Can you see what we're missing,
by comparing what we just wrote with rect? -
0:31 - 0:33When we just type ellipse,
we aren't telling it the numbers, -
0:33 - 0:35like we did for the rectangle.
-
0:35 - 0:37These numbers here are called parameters.
-
0:38 - 0:40We say that we pass parameters
into functions, -
0:40 - 0:43and they control how the function behaves.
-
0:45 - 0:47Without the parameters,
-
0:47 - 0:49the program doesn't know
where you want your ellipse, -
0:49 - 0:50or how big to make it.
-
0:50 - 0:53Now that error message makes
a little more sense. -
0:53 - 0:55Let's go ahead and pass in
four parameters again, -
0:55 - 0:59to control how far over,
how far down, how wide, -
0:59 - 1:01and how tall we want that ellipse to be.
-
1:01 - 1:04Just like before, we can have some fun
and move around our ellipse, -
1:04 - 1:06and even make it grow and shrink.
-
1:08 - 1:12Now that we've seen the basics,
let's try drawing a big ellipse -
1:12 - 1:13right in the middle of the canvas.
-
1:14 - 1:17The first question you might have is,
where's the middle again? -
1:18 - 1:20Just to review,
we have this upper left, 0, -
1:20 - 1:25and the right, if you remember, is 400,
and the bottom is 400 as well. -
1:25 - 1:28So if we think,
"Where would the middle be?" -
1:28 - 1:31We would say, "It's gonna
to be half of 400 over, so 200. -
1:31 - 1:34Then half of 400 down, so 200."
-
1:34 - 1:35We can go ahead and do that.
-
1:35 - 1:37Let's make our ellipse function,
-
1:37 - 1:41we'll pass the parameters in,
and make it pretty big. -
1:41 - 1:43There it is!
-
1:43 - 1:46Just for fun, let's put
a rectangle there too. -
1:46 - 1:50We'll say rect(200, 200 as well,
and maybe a little bit smaller. 100, 100); -
1:50 - 1:53Hm, this is kind of interesting.
-
1:53 - 1:55What does this little experiment show us?
-
1:55 - 1:58Well, we can see that
that 200, 200 point -
1:58 - 2:02is actually saying where we should put
the center of the ellipse. -
2:02 - 2:04But for rectangles it's different,
because for rectangles -
2:04 - 2:09the 200, 200 says where we should put
this upper left corner of the rectangle. -
2:10 - 2:13That's really important to remember
when we're trying to position our shapes. -
2:15 - 2:17Now let's move on to simple lines.
-
2:17 - 2:19That function name
is just going to be line. -
2:19 - 2:22We can pass it four parameters again,
-
2:22 - 2:25but a line doesn't really have
a size like a rectangle, does it? -
2:25 - 2:27So what will these numbers control?
-
2:28 - 2:31The first and the second parameters,
just like before, -
2:31 - 2:34say how far over and down
the line should start. -
2:35 - 2:37Whereas the second two parameters--
-
2:37 - 2:40or sorry, the second set of parameters,
the 90 and the 200-- -
2:40 - 2:44are going to specify how far over
and how far down the line should end. -
2:47 - 2:48Now that we understand how that works,
-
2:48 - 2:52let's look at something that's going
to seem really weird at first. -
2:53 - 2:57What happens if I make the rectangle start
in that upper left corner, -
2:57 - 3:00again specifying that upper left corner
of the rectangle as well? -
3:01 - 3:02And then really big.
-
3:04 - 3:07We can even make it that big,
but that's a little bit too big, I think. -
3:08 - 3:12We see that it's gradually starting
to make that ellipse disappear. -
3:12 - 3:14We can actually make it
disappear completely. -
3:15 - 3:17Now we're wondering where it went.
-
3:18 - 3:22What the program does is it actually
draws your shapes in order. -
3:22 - 3:25First it draws that ellipse,
then it draws that rectangle on top, -
3:25 - 3:27and then it draws the line.
-
3:27 - 3:30So that ellipse is still there,
it's just, as you saw, underneath. -
3:31 - 3:32This is an important point to remember
-
3:32 - 3:36because what would happen
if we drew our line first? -
3:36 - 3:39We just wouldn't see it at all, would we?
-
3:39 - 3:42You might do that in your programs
and wonder, "Hey, where did my line go?" -
3:42 - 3:45The idea is that it is there,
it's just being hidden right now -
3:45 - 3:48both by the ellipse
and also by that rectangle. -
3:50 - 3:54We can affect which shapes
are drawn on top of which other shapes -
3:54 - 3:57just by changing the order
that we write them in our program. -
3:59 - 4:01Now, I just want to introduce
a couple of technical terms -
4:01 - 4:02before we finish.
-
4:03 - 4:05Just like you might have learned in math,
-
4:05 - 4:07we can use the letter x
to mean how far over -
4:07 - 4:09like we've been talking about,
-
4:09 - 4:12and the letter y to mean how far down.
-
4:12 - 4:14That might seem a little bit weird
if you're not used to it, -
4:14 - 4:17but it's easier to say than
"how far over and how far down" -
4:17 - 4:19every single time.
-
4:19 - 4:22The first two parameters
to our ellipse, for example, -
4:22 - 4:28are saying that x should be at 200
and y should be at 229. -
4:29 - 4:30There you have it,
-
4:30 - 4:33just the same thing as saying
"how far over and how far down". -
4:34 - 4:36A second really good question
you might have is, -
4:36 - 4:39"What units have we been using
this whole time? -
4:39 - 4:43Are we saying 200 centimeters,
200 inches, 200 miles?" -
4:43 - 4:45We're using units called 'pixels',
-
4:45 - 4:48and a pixel is a tiny little point
on your screen. -
4:48 - 4:52This canvas actually happens
to be 400 pixels wide. -
4:52 - 4:56That's why we always say
that this upper left corner is 0, -
4:56 - 5:01and over here is 400,
because it's 400 pixels. -
5:02 - 5:05Similarly, when we say 200,
we actually mean 200 pixels, -
5:05 - 5:07and you probably get the idea.
-
5:07 - 5:08Fantastic!
-
5:08 - 5:11Now you know all about the functions
line, ellipse, and rect, -
5:11 - 5:12and their parameters.
-
5:12 - 5:15We covered a lot, but just stick with it,
keep exploring, -
5:15 - 5:16and you'll get the hang of it soon.
- Title:
- More Drawing (Video Version)
- Description:
-
This is just a screen grab of our interactive coding talk-through, prepared to make captioning and translation easier. It is better to watch our talk-throughs here:
https://www.khanacademy.org/cs/programming/ - Video Language:
- English
- Duration:
- 05:17
April Porter edited English subtitles for More Drawing (Video Version) | ||
Mary Beth Strawn edited English subtitles for More Drawing (Video Version) | ||
Mary Beth Strawn edited English subtitles for More Drawing (Video Version) | ||
pamela-from-khan edited English subtitles for More Drawing (Video Version) |