1
00:00:05,300 --> 00:00:09,950
so in the previous video we create we
ended up creating this circle function

2
00:00:09,950 --> 00:00:15,240
and actually ran it and I'll just run it
again let's have a look at the output

3
00:00:15,240 --> 00:00:22,400
and there is the output there and as I mentioned the circles are a lil bit vague you can see just

4
00:00:22,400 --> 00:00:27,330
around the edges to the left and the
right their and just a little bit vague the line

5
00:00:27,330 --> 00:00:32,060
the dots don't sort of lined up
correctly the reasons for that at the moment is

6
00:00:32,060 --> 00:00:36,260
we were only plotting points for integer
values of x so we don't get many points plotted

7
00:00:36,260 --> 00:00:41,010
and we can't use decimal values in the
step for our range

8
00:00:41,760 --> 00:00:46,310
and I'm referring to the code here showing there
on line 16 which of course is our

9
00:00:46,310 --> 00:00:50,830
circle function but what we can do is
multiply the range up so that more

10
00:00:50,830 --> 00:00:56,010
values are used and scale each value back down again so doing this by a factor of a

11
00:00:56,010 --> 00:01:03,190
hundred will give the same effect as a range
and increments by 0.01 and that should therefore

12
00:01:03,190 --> 00:01:06,550
then fix up the circle so let's have a look at that

13
00:01:06,550 --> 00:01:09,810
so what we wanna do is make a change to the code on line 17

14
00:01:10,380 --> 00:01:16,450
in the way you can see for x in range g then
g + radius so let's just add the

15
00:01:16,450 --> 00:01:22,260
multiplication step to that so.....

16
00:01:22,260 --> 00:01:29,750
....but then what we

17
00:01:29,750 --> 00:01:38,330
need to do is scaled back X as well so
put.......because again we're just using

18
00:01:38,330 --> 00:01:41,750
the multiplied by a
hundred to give us a bigger range

19
00:01:41,750 --> 00:01:47,940
effectively the same as we had a range
that increments 0.01 so we then have to divide it back by a

20
00:01:47,940 --> 00:01:51,640
hundred to take it back to the original
point that it was before so they will

21
00:01:51,640 --> 00:01:54,640
plot if we now run this again

22
00:01:56,700 --> 00:02:09,229
...and their you can see it's now a lot better it's actually plotting a hundred times as many points

23
00:02:09,229 --> 00:02:14,140
as it was previously and as a result
looking at that it's a lot better but

24
00:02:14,140 --> 00:02:19,030
did you notice that it also took
significantly longer so I'll just run it again

25
00:02:19,030 --> 00:02:26,560
so I ran it and try to tab over and I can't still tab over because it is still generating it and their it is in the screen

26
00:02:26,560 --> 00:02:31,900
there now their you go did you see it pop ups
it took a quite a deal more time to actually

27
00:02:31,900 --> 00:02:35,850
create that and again the reason is why we are
plotting a hundred times as many points

28
00:02:35,850 --> 00:02:41,530
as we did previously so its doing a
hundred times more calculations but the

29
00:02:41,530 --> 00:02:45,650
end result is the circle is a much more
well defined with modifications that

30
00:02:45,650 --> 00:02:49,500
are actually made now if you're not sure
what's going on with scaling the range

31
00:02:49,500 --> 00:02:53,990
what we can do is print the value of x so
let's go ahead and do that just so we

32
00:02:53,990 --> 00:03:05,050
can sort of see what is going on so if
we add a print their...and run that that showing us

33
00:03:05,050 --> 00:03:09,070
a representation of the values that are
being plotted you can see their is quiet a

34
00:03:09,070 --> 00:03:17,690
a few their and if I tab over now and see if its finished we got our parabola their on the screen you can see everything

35
00:03:17,690 --> 00:03:22,940
at once so for the first circle we
ended up with the range running from 100 to 200 in

36
00:03:22,940 --> 00:03:29,900
steps of 0.01 you can see the 0.01 their for each one of course we
did that by scaling up the start and end

37
00:03:29,900 --> 00:03:34,750
values of the range and then scaling x back down again before actually using it so

38
00:03:34,750 --> 00:03:37,990
that it would actually plot in the
right position so although the range

39
00:03:37,990 --> 00:03:43,260
function can only use integer values it can still
be used to produce a range of decimals by

40
00:03:43,260 --> 00:03:47,280
and I'll be showing how to do that now by
multiplying it by a hundred now we are

41
00:03:47,280 --> 00:03:50,880
going to be looking at another way to do
the same thing using a generator later

42
00:03:50,880 --> 00:03:55,120
in the course which can result in much
more readable code now the disadvantage

43
00:03:55,120 --> 00:03:59,570
with this is that performances is
suffered but the result is much nicer

44
00:03:59,570 --> 00:04:03,880
and by having the circle drawing code in
a function you saw that the change has

45
00:04:03,880 --> 00:04:06,960
applied to all circles that were drawn so basically putting the code

46
00:04:06,960 --> 00:04:10,210
in one place enabled us to make the
change to every single circle

47
00:04:10,210 --> 00:04:14,810
that was actually drawn but with that said
in fact the canvas widget has a create

48
00:04:14,810 --> 00:04:19,029
oval method that can be used to draw
circles and is actually much faster than

49
00:04:19,029 --> 00:04:23,780
the method data we've come up with so the
create oval method needs the coordinates

50
00:04:23,780 --> 00:04:28,650
of the top left and bottom right of a
bounding rectangle but we're working with

51
00:04:28,650 --> 00:04:33,090
the radius and coordinates of the center we can still use our circle method we just

52
00:04:33,090 --> 00:04:37,140
change it so that it calculates the
coordinates that the create oval method

53
00:04:37,140 --> 00:04:44,970
needs so lets go ahead and do that so lets close it down and I'm making it a lot easier so comment out this code in our circle

54
00:04:44,970 --> 00:04:56,200
function and the code at the top so all we
need to do is put.....and then

55
00:04:56,200 --> 00:05:02,030
we need to calculate the coordinates for
the create oval method it needs again what it needs is

56
00:05:02,030 --> 00:05:06,020
the coordinates of the top left and bottom
right of a bounding rectangle then it is

57
00:05:06,020 --> 00:05:13,810
gonna draw it over within those parameters so put....

58
00:05:13,810 --> 00:05:20,450
.......

59
00:05:22,120 --> 00:05:31,280
.....

60
00:05:31,280 --> 00:05:39,050
and that's all we need to do and I just need to add a line

61
00:05:39,050 --> 00:05:44,700
there to keep that happy 2
lines in between each function so now if we run

62
00:05:44,700 --> 00:05:55,530
that...and bingo we already got our circles and I have made a

63
00:05:55,530 --> 00:05:59,920
mistake their what I've done is I've actually
multiplied the radius instead of adding

64
00:05:59,920 --> 00:06:02,920
it so that should have been G Plus right radius.....

65
00:06:06,110 --> 00:06:13,150
...and the other two should be fine so again the top left of the first

66
00:06:13,150 --> 00:06:18,120
2 parameters are the bottom right of the last 2 so if I run it again we should get basically results similar to what

67
00:06:18,120 --> 00:06:22,310
we had before you can see that was significantly faster and

68
00:06:22,310 --> 00:06:25,710
it's reduced a very good effect and the
advantage with that is we are using

69
00:06:25,710 --> 00:06:31,770
optimized code those built in part of
Python so always a good idea where possible

70
00:06:31,770 --> 00:06:36,270
if you could find some code that has been already written to
use that rather than reinventing the

71
00:06:36,270 --> 00:06:41,990
wheel that we did initially with our
function circle function on line 60 now

72
00:06:41,990 --> 00:06:47,560
I use the term method instead of function
when referring to the create_oval method and

73
00:06:47,560 --> 00:06:52,700
because a canvas is actually class and
class functions are called methods that's

74
00:06:52,700 --> 00:06:58,950
actually the right terminology to use so
in this case the create_oval is a method and again as

75
00:06:58,950 --> 00:07:03,560
classes methods the correct term
instead of functions but don't worry too

76
00:07:03,560 --> 00:07:06,810
much about that because we'll be looking
at classes and object-oriented

77
00:07:06,810 --> 00:07:10,550
programming in the next section and
you'll understand why they're called

78
00:07:10,550 --> 00:07:14,930
that and once we go through that it will make a lot more sense but it's probably a good idea

79
00:07:14,930 --> 00:07:18,830
to start using the correct terminology
as you can see their isn't really a great

80
00:07:18,830 --> 00:07:21,710
deal of difference between functions and
class methods when it comes down to

81
00:07:21,710 --> 00:07:22,650
actually using them

82
00:07:22,650 --> 00:07:27,110
okay so it's time for a mini challenge
and it is very much a mini challenge

83
00:07:27,110 --> 00:07:31,060
and I'm just going to type it just above the circle
method because it's ultimately where we

84
00:07:31,060 --> 00:07:40,300
are going to be using it we will modify the
circle function so that it allows

85
00:07:42,350 --> 00:07:58,620
the color of the circle to be specified
and defaults to red color is not given and just a

86
00:07:58,620 --> 00:08:15,460
note here you may want to review the previous lecture about named parameters and default values so that's basically it

87
00:08:15,460 --> 00:08:19,290
modify the circle functions so that it allows
the color of the circle to be specified

88
00:08:19,290 --> 00:08:24,060
it defaults to red if the color is not given
now you may want to review the previous

89
00:08:24,060 --> 00:08:29,110
lectures about named parameters and default values to get it working so it's now time to pause the

90
00:08:29,110 --> 00:08:35,000
video go away and make the change to the
circle function and when you made your change come back and

91
00:08:35,000 --> 00:08:39,260
you can compare the code that we've
actually about to put in ours

92
00:08:39,260 --> 00:08:48,310
so we'll see you when you get back pause the video now ok so
hopefully you figure it out all we really need

93
00:08:48,310 --> 00:08:54,440
to do is change make the change now on line 19 actually I'll put 2 spaces their keep it happy

94
00:08:54,440 --> 00:08:57,960
and we need to add an extra parameter

95
00:08:58,960 --> 00:09:06,930
and we just put....so by putting the
equals red that's defaulting that as a parameter

96
00:09:06,930 --> 00:09:11,320
if one is passed and of course we
need to change the code here where it is

97
00:09:11,320 --> 00:09:18,080
hard coded for red and make that color so once we do that that should work that should be all we need to do

98
00:09:18,080 --> 00:09:20,560
and we can test this by the

99
00:09:20,560 --> 00:09:25,330
first four lets changed those so put.....

100
00:09:29,420 --> 00:09:45,750
...and notice we are not getting any errors with
these other circle calls on line 63 through 67

101
00:09:46,390 --> 00:09:50,130
because we are not specifying a parameter its going to default to red and it doesn't come up with an error

102
00:09:50,130 --> 00:09:54,660
but the first four circle should be in green yellow black and blue so

103
00:09:54,660 --> 00:10:04,810
lets try running that and you can see it's working nicely so we got a blue yellow black and green circle

104
00:10:04,810 --> 00:10:08,980
so that's actually it that's the end of the
lecture and I'll see you in the next

105
00:10:08,980 --> 00:10:13,790
lecture where are we are going to take a
little bit of a different tack and we are going to

106
00:10:13,790 --> 00:10:17,810
start looking at creating a blackjack
card game so we'll see you in the next

107
00:10:17,810 --> 00:10:18,030
video

