Return to Video

More Drawing (Video Version)

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

more » « less
Video Language:
English
Duration:
05:17

English subtitles

Revisions