Return to Video

Resizing shapes with variable expressions

  • 0:00 - 0:02
    We're back with Winston.
  • 0:02 - 0:06
    We now have both an x and a y variable for
  • 0:06 - 0:07
    Winston's position.
  • 0:07 - 0:09
    So we can move him sideways
  • 0:10 - 0:12
    Up and down, woo!
  • 0:12 - 0:13
    Very nice.
  • 0:14 - 0:16
    Now, let's reset these variables
  • 0:16 - 0:19
    with 200 and 200
  • 0:19 - 0:22
    and review how this program works.
  • 0:23 - 0:26
    So starting here, we've got an eyeSize variable.
  • 0:26 - 0:28
    It's being used to control the eye size,
  • 0:28 - 0:30
    because the eyes are all 40 pixels wide
  • 0:30 - 0:32
    and 40 pixels tall.
  • 0:32 - 0:35
    And then we have these x and y variables,
  • 0:35 - 0:39
    and those position the center of the face.
  • 0:40 - 0:41
    And you can see them used
  • 0:41 - 0:43
    in this ellipse command here
  • 0:43 - 0:45
    that draws the big yellow circle.
  • 0:46 - 0:47
    And then down here,
  • 0:47 - 0:49
    for the eyes,
  • 0:49 - 0:50
    the x and y are used again.
  • 0:51 - 0:52
    And here,
  • 0:52 - 0:54
    the eyes are positioned
  • 0:54 - 0:56
    relative to the center of the face.
  • 0:56 - 0:58
    So maybe this one is
  • 0:58 - 0:59
    fifty pixels
  • 0:59 - 1:01
    to the left of the center
  • 1:02 - 1:03
    and this one is a hundred pixels
  • 1:03 - 1:05
    to the right of the center.
  • 1:05 - 1:08
    OK. So pretty cool
  • 1:08 - 1:09
    and that's why we're able
  • 1:09 - 1:11
    to move Winston up and down
  • 1:11 - 1:13
    Now, I want to be able to control
  • 1:13 - 1:15
    more things about Winston's face
  • 1:15 - 1:17
    with variables
  • 1:17 - 1:18
    so I want to figure out
  • 1:18 - 1:19
    what else in the program
  • 1:19 - 1:21
    we can store as variables
  • 1:21 - 1:21
    to do that
  • 1:21 - 1:23
    I'm going to go through
  • 1:23 - 1:24
    each line of code
  • 1:24 - 1:25
    and look for what we call
  • 1:25 - 1:27
    hard-coded numbers
  • 1:28 - 1:29
    those are numbers that are
  • 1:29 - 1:31
    just literal numbers
  • 1:31 - 1:33
    not variables or dependant on variables
  • 1:34 - 1:35
    so lets start here
  • 1:35 - 1:37
    in the first ellipse call
  • 1:38 - 1:39
    we have 300 and 300
  • 1:39 - 1:41
    for the width and height
  • 1:42 - 1:44
    those are just literal numbers
  • 1:44 - 1:45
    so lets make a variable
  • 1:45 - 1:46
    for those instead
  • 1:46 - 1:48
    called faceSize
  • 1:49 - 1:50
    and have it store 300
  • 1:50 - 1:53
    now we'll just write faceSize,
  • 1:54 - 1:55
    faceSize
  • 1:56 - 1:57
    OK So keep going
  • 1:57 - 1:58
    and skip colours
  • 1:58 - 2:00
    now the ellipse commands are either --
  • 2:00 - 2:02
    they're all variables or
  • 2:02 - 2:03
    dependant on variables
  • 2:03 - 2:04
    so I'm going to leave them
  • 2:04 - 2:05
    like this for now
  • 2:05 - 2:07
    and then the mouth command
  • 2:07 - 2:09
    those are dependant on x and y
  • 2:09 - 2:10
    but these here
  • 2:10 - 2:12
    are just literal numbers
  • 2:13 - 2:15
    150 and 150
  • 2:15 - 2:16
    so we're going to say
  • 2:16 - 2:19
    mouthSize that's a good name
  • 2:19 - 2:20
    equals 150
  • 2:22 - 2:25
    we'll replace this with mouthSize
  • 2:25 - 2:27
    and mouthSize
  • 2:28 - 2:29
    alright so now
  • 2:29 - 2:31
    we have the sizes of the shapes
  • 2:31 - 2:33
    stored as variables at the top
  • 2:33 - 2:35
    that means that its really easy
  • 2:35 - 2:36
    for us to change the sizes
  • 2:36 - 2:37
    like this like
  • 2:37 - 2:39
    Wooo Winston's hungry
  • 2:39 - 2:41
    and then maybe like, you know,
  • 2:41 - 2:43
    Winston's got hungry and
  • 2:43 - 2:44
    then he eats lots of donuts
  • 2:44 - 2:46
    and then he gets super big
  • 2:46 - 2:48
    alright
  • 2:48 - 2:49
    but there is something
  • 2:49 - 2:51
    I don't like about the program right now
  • 2:52 - 2:59
    So if I make the face size really small
  • 2:59 - 3:01
    it starts to look funny
  • 3:01 - 3:03
    because the eyes and the mouth
  • 3:03 - 3:05
    are sticking out of the face
  • 3:05 - 3:07
    and at certain points it doesn't even
  • 3:07 - 3:08
    really look like they're connected
  • 3:08 - 3:09
    to that face
  • 3:09 - 3:11
    or its not really a face any more, is it?
  • 3:12 - 3:14
    So what I really want to happen
  • 3:14 - 3:16
    is that when I change faceSize
  • 3:16 - 3:18
    I want the eyes and the mouth --
  • 3:18 - 3:22
    I want their size to change along with it
  • 3:22 - 3:24
    so if I make faceSize be half the size
  • 3:24 - 3:27
    I want the mouth to be half the size too
  • 3:27 - 3:29
    so that means that
  • 3:30 - 3:33
    I want to calculate mouthSize and eyeSize
  • 3:33 - 3:36
    as fractions of faceSize
  • 3:37 - 3:40
    alright lets reset these variables
  • 3:40 - 3:42
    and I'll show you what I mean
  • 3:42 - 3:44
    Let's start with mouthSize
  • 3:44 - 3:45
    so right now
  • 3:45 - 3:50
    faceSize is 300 and mouthSize is 150
  • 3:50 - 3:52
    so if we think of about them
  • 3:52 - 3:53
    relative to each other
  • 3:53 - 3:56
    we'd say that faceSize is twice as big
  • 3:56 - 3:58
    as mouthSize
  • 3:58 - 4:00
    or that mouthSize is half the size
  • 4:00 - 4:01
    of faceSize
  • 4:01 - 4:04
    and we can write that in code like this
  • 4:04 - 4:08
    one half times faceSize
  • 4:08 - 4:10
    ok so this line of code says
  • 4:10 - 4:12
    that we take the value of faceSize
  • 4:12 - 4:13
    multiply it by a half
  • 4:13 - 4:15
    and store that in mouthSize
  • 4:16 - 4:17
    so that if we change this here
  • 4:18 - 4:20
    it would figure out what half of that was
  • 4:20 - 4:22
    and that would become mouthSize
  • 4:22 - 4:24
    Perfect! That's what we want
  • 4:25 - 4:27
    So now eyeSize
  • 4:27 - 4:29
    so faceSize is 300
  • 4:29 - 4:31
    and eyeSize is 40
  • 4:31 - 4:33
    so we need it to be
  • 4:33 - 4:37
    40 three hundreths of faceSize
  • 4:37 - 4:39
    which is really, well lets see
  • 4:39 - 4:41
    four over 30 which we can
  • 4:41 - 4:44
    simplify down to two over 15
  • 4:44 - 4:46
    so we're going to say
  • 4:46 - 4:50
    two over fifteen times faceSize
  • 4:50 - 4:51
    by the way
  • 4:51 - 4:52
    if you're new to fractions
  • 4:52 - 4:54
    and that math is tricky for you
  • 4:54 - 4:56
    you can learn more about fractions
  • 4:56 - 4:58
    on khanacademy
  • 4:58 - 5:00
    and come back here when you're
  • 5:00 - 5:00
    feeling ready
  • 5:00 - 5:02
    here, you just go there
  • 5:03 - 5:04
    ok
  • 5:04 - 5:06
    so lets try resizing the face again
  • 5:07 - 5:08
    Haha! Check it out
  • 5:08 - 5:10
    the mouth and the eyes resize
  • 5:10 - 5:12
    proportionally to the face
  • 5:12 - 5:13
    but you probably noticed
  • 5:13 - 5:15
    something is wrong
  • 5:15 - 5:17
    the eyes and the mouth
  • 5:17 - 5:19
    are still sticking out of the face
  • 5:19 - 5:21
    even though they are
  • 5:21 - 5:23
    much more appropriately sized
  • 5:23 - 5:25
    That is because we still have
  • 5:25 - 5:27
    some hard-coded numbers
  • 5:27 - 5:28
    in our ellipse commands
  • 5:28 - 5:30
    some numbers that should actually
  • 5:30 - 5:33
    be fractions of variables instead
  • 5:33 - 5:35
    here, I'll show you
  • 5:35 - 5:38
    So for the eye ellipse we have
  • 5:38 - 5:41
    x minus 50 for the x position
  • 5:41 - 5:43
    so this means it's always x minus 50
  • 5:43 - 5:45
    even if we make our faceSize
  • 5:45 - 5:47
    smaller than 50 pixels
  • 5:47 - 5:48
    and that definitely doesn't make sense
  • 5:48 - 5:50
    because that means that the left eye
  • 5:50 - 5:50
    is going to be
  • 5:50 - 5:52
    not even in the face anymore
  • 5:52 - 5:55
    so it should be x minus some fraction
  • 5:55 - 5:57
    the size of our face
  • 5:57 - 5:59
    and we can figure out the fraction
  • 5:59 - 6:00
    the same way
  • 6:00 - 6:04
    50 relative to the original 300
  • 6:04 - 6:08
    so 50 over 300, five over 30, one over six
  • 6:09 - 6:12
    so one over six times faceSize
  • 6:12 - 6:14
    and we see another 50 here
  • 6:14 - 6:16
    so we can do the same thing
  • 6:16 - 6:17
    the same expression
  • 6:17 - 6:19
    here we have 100 over 300
  • 6:19 - 6:21
    that's going to be
  • 6:21 - 6:23
    one third times faceSize
  • 6:23 - 6:25
    this one is 60
  • 6:25 - 6:26
    so that'll end up being
  • 6:26 - 6:29
    one fifth times faceSize
  • 6:29 - 6:32
    and here this is another 50
  • 6:32 - 6:34
    so its one sixth again
  • 6:34 - 6:36
    and then 40
  • 6:36 - 6:38
    that's what we figured out up here
  • 6:38 - 6:40
    two over 15
  • 6:40 - 6:45
    so two over 15 times faceSize
  • 6:45 - 6:48
    alright so lets try again
  • 6:48 - 6:49
    Oh, look at that!
  • 6:49 - 6:51
    Look at it. That's beautiful
  • 6:51 - 6:52
    so good
  • 6:53 - 6:55
    alright so let's review
  • 6:55 - 6:56
    We created this variable
  • 6:56 - 6:58
    that stored the size of the face
  • 6:58 - 7:00
    and it just stores a number
  • 7:00 - 7:02
    then we have these mouthSize
  • 7:02 - 7:03
    and eyeSize variables
  • 7:03 - 7:06
    and we calculate them based as fractions
  • 7:06 - 7:07
    of faceSize
  • 7:07 - 7:08
    to make sure that
  • 7:08 - 7:10
    their values always changed
  • 7:10 - 7:12
    based on what we start this one off as
  • 7:13 - 7:15
    then all of the offsets are calculated
  • 7:15 - 7:17
    based on faceSize too
  • 7:17 - 7:18
    to make sure the position
  • 7:18 - 7:20
    inside the face changes
  • 7:20 - 7:22
    if faceSize changes
  • 7:22 - 7:23
    Whoo! Alright.
  • 7:23 - 7:25
    So now that we really understand
  • 7:25 - 7:26
    how to make variables
  • 7:26 - 7:28
    dependant on the values of other variables
  • 7:28 - 7:30
    we can do so much more with our programmes
  • 7:30 - 7:32
    let's celebrate by making Winston
  • 7:32 - 7:37
    Huge! yeah, go Winston!
Title:
Resizing shapes with variable expressions
Description:

more » « less
Video Language:
English
Duration:
07:37

English subtitles

Revisions