1
00:00:06,104 --> 00:00:08,574
Hi, welcome to this new lesson.

2
00:00:08,574 --> 00:00:13,336
We are going to continue, this is
the third video of a series of videos for

3
00:00:13,336 --> 00:00:14,886
our final project.

4
00:00:14,886 --> 00:00:18,385
We are writing Langton's
ant algorithm once more,

5
00:00:18,385 --> 00:00:21,414
which we actually initiated in week four.

6
00:00:21,414 --> 00:00:24,998
We're writing this time using
object-oriented programming.

7
00:00:24,998 --> 00:00:28,680
Now that we've learned about
object-oriented programming,

8
00:00:28,680 --> 00:00:31,883
we are rewriting those ideas into classes,
right, and

9
00:00:31,883 --> 00:00:34,792
multiple classes that
interact with each other.

10
00:00:34,792 --> 00:00:38,040
This time,
we're actually writing a grid class.

11
00:00:38,040 --> 00:00:42,283
And let it kind of have conversations
if you want to send message to this

12
00:00:42,283 --> 00:00:45,936
ant that is going to be moving around and
wandering around.

13
00:00:45,936 --> 00:00:48,960
So let's jump into the project.

14
00:00:48,960 --> 00:00:51,322
If you haven't seen the last two videos,

15
00:00:51,322 --> 00:00:54,296
I'm going to continue from
code from those videos.

16
00:00:54,296 --> 00:00:58,188
What we currently have,
it's a grid class that

17
00:00:58,188 --> 00:01:03,262
basically spreads itself
throughout the size of a screen.

18
00:01:03,262 --> 00:01:06,934
And we are initializing it
with some random data, right?

19
00:01:06,934 --> 00:01:10,697
So we're actually calling two
functions here for the grid,

20
00:01:10,697 --> 00:01:15,542
the main_grid class, initialize the data,
which is random at this point.

21
00:01:15,542 --> 00:01:16,734
You can put a comment there.

22
00:01:16,734 --> 00:01:18,950
I mean, you could initialize
the data in different ways.

23
00:01:18,950 --> 00:01:22,514
You could have multiple ways of initiating
the data, and we can look into that.

24
00:01:23,824 --> 00:01:27,930
You also have the grid.run,
which currently it's being

25
00:01:27,930 --> 00:01:32,556
called within the setup because
this is not a dynamic script yet.

26
00:01:32,556 --> 00:01:37,497
But we might want to later move that
into a draw function if that data,

27
00:01:37,497 --> 00:01:40,536
for whatever reason, starts changing.

28
00:01:40,536 --> 00:01:44,528
And we'll see that the ant that we're
going to draw, the agent that we're

29
00:01:44,528 --> 00:01:48,232
going to draw on top of the grid,
will be able to manipulate the data.

30
00:01:48,232 --> 00:01:50,967
It will be able to switch data around,
right?

31
00:01:50,967 --> 00:01:56,084
So we will be able to have a dynamic
rendition or representation of this grid.

32
00:01:56,084 --> 00:02:03,724
But we don't have that ant yet, we only
have one class, which is our Grid class.

33
00:02:03,724 --> 00:02:10,362
So let's go ahead and create another tab,
which is going to be called the Langton,

34
00:02:13,544 --> 00:02:17,969
_Ant_Class.

35
00:02:17,969 --> 00:02:21,155
And we have a whole new area here.

36
00:02:21,155 --> 00:02:24,789
Just for good measure,
I'm going to import,

37
00:02:27,096 --> 00:02:31,670
random, just because I know
we're going to need it.

38
00:02:31,670 --> 00:02:34,871
And let's just start writing our class,
right?

39
00:02:34,871 --> 00:02:38,897
So class Langton_Ant.

40
00:02:45,974 --> 00:02:51,579
Right, and here,
we can actually write our constructor,

41
00:02:51,579 --> 00:02:56,076
which is going to be defined
by our init function.

42
00:02:59,834 --> 00:03:05,738
And again, this is a class,
the algorithm we covered already.

43
00:03:05,738 --> 00:03:10,314
So let's just think about what defines
an agent that we will call the ant, right?

44
00:03:10,314 --> 00:03:12,282
Well, it needs to have
a position in the grid.

45
00:03:12,282 --> 00:03:17,506
It needs to be located somewhere
in an index in the grid, right?

46
00:03:17,506 --> 00:03:22,894
So we will need a variable for
x and a variable for y.

47
00:03:24,514 --> 00:03:27,734
If you remember the videos
on the Langton side,

48
00:03:27,734 --> 00:03:30,458
we will also need a direction, right?

49
00:03:30,458 --> 00:03:35,298
The ant can switch directions,
it can move in different directions.

50
00:03:35,298 --> 00:03:40,365
So identifying the direction,
we did that with an integer, 0,

51
00:03:40,365 --> 00:03:45,610
1, 2, 3, defining four different
kernel axis of movement.

52
00:03:45,610 --> 00:03:49,294
So we can actually use a similar thing.

53
00:03:49,294 --> 00:03:51,514
And maybe that's enough for now.

54
00:03:52,614 --> 00:03:57,318
Let's just close here our constructor, and

55
00:03:57,318 --> 00:04:03,518
let's just equate our internal
variables of self.x to x,

56
00:04:05,868 --> 00:04:10,114
self.y to y, and self.dir.

57
00:04:13,834 --> 00:04:18,572
The problem that we are seeing right
now is that this ant, actually,

58
00:04:18,572 --> 00:04:23,218
this x and y refers specifically
to a position in the grid, right?

59
00:04:23,218 --> 00:04:28,321
And this being a class,
we would need to be having a reference or

60
00:04:28,321 --> 00:04:34,386
some kind of information about the grid
that we are relating to, right?

61
00:04:34,386 --> 00:04:40,628
There might be different ways of
addressing the passing of information,

62
00:04:40,628 --> 00:04:45,774
but this is basically what this
lesson is trying to address.

63
00:04:45,774 --> 00:04:50,537
Which is how do we start creating
conversations or messages or

64
00:04:50,537 --> 00:04:55,114
interactions between different classes,
right?

65
00:04:55,114 --> 00:04:59,604
So the first thing I'm going to
do here is start thinking, well,

66
00:04:59,604 --> 00:05:05,204
I'm going to need to bring the grid, which
is going to have its own definitions.

67
00:05:05,204 --> 00:05:07,089
We're going to need to
bring that into the script.

68
00:05:07,089 --> 00:05:15,128
So I'm thinking, well,
let's just bring a grid object, Right?

69
00:05:15,128 --> 00:05:18,463
So somewhere in the world,
whenever I construct this class,

70
00:05:18,463 --> 00:05:22,129
this class will be needing a reference
to that grid object, right?

71
00:05:27,924 --> 00:05:32,122
Because we're going to be able
to use that grid object for

72
00:05:32,122 --> 00:05:35,048
the calculations, like neighbors.

73
00:05:35,048 --> 00:05:39,421
Move to a neighboring cell or
affect the data of that grid and

74
00:05:39,421 --> 00:05:43,588
whatnot, all sorts of different behaviors,
right?

75
00:05:43,588 --> 00:05:49,252
So for now, let's just create
a simple function to display.

76
00:05:49,252 --> 00:05:55,208
Well, the way we're going to do this is
we're going to create a run function,

77
00:05:55,208 --> 00:06:00,682
which is going to be a summary of all
the functions that we will write for

78
00:06:00,682 --> 00:06:02,238
this ant, right?

79
00:06:02,238 --> 00:06:05,338
And let's just make some comments
of those functions, right?

80
00:06:05,338 --> 00:06:09,722
So we're going to start with the display,
or the drawing of the ant.

81
00:06:09,722 --> 00:06:11,991
Later, we're going to
move to the ant behavior.

82
00:06:15,314 --> 00:06:19,875
And finally, we're going to
check some form of stay within

83
00:06:19,875 --> 00:06:26,534
bounds, Functionality, right?

84
00:06:26,534 --> 00:06:30,742
And all of those we will cover over
maybe a few different videos, right.

85
00:06:30,742 --> 00:06:33,382
So we will not just do all
of those right now, but

86
00:06:33,382 --> 00:06:35,628
let's start with the display, right?

87
00:06:35,628 --> 00:06:41,415
So, Let's define the draw_ant.

88
00:06:53,064 --> 00:06:58,064
My apologies, I'm working on,
not my personal keyword.

89
00:06:58,064 --> 00:06:59,664
There we go.

90
00:06:59,664 --> 00:07:03,432
So we have the draw_ant here.

91
00:07:03,432 --> 00:07:08,233
And if we really wanted to just draw
something, let's do an ellipse, right?

92
00:07:08,233 --> 00:07:12,900
And we could say, well,

93
00:07:12,900 --> 00:07:16,908
self.x, self.y.

94
00:07:16,908 --> 00:07:20,084
And oops, same problem.

95
00:07:20,084 --> 00:07:26,324
I have self, Comma 20, 20.

96
00:07:28,064 --> 00:07:32,690
And the fill,
let's just do a red fill for now,

97
00:07:32,690 --> 00:07:37,324
just to see this entity in this screen,
right?

98
00:07:39,304 --> 00:07:43,420
We could draw the direction as well,
if we wanted, as a line.

99
00:07:43,420 --> 00:07:49,452
But let's just for
now keep this simple function.

100
00:07:49,452 --> 00:07:53,928
So instead of display, we're going to do

101
00:07:53,928 --> 00:07:58,764
self., draw_ant.

102
00:07:58,764 --> 00:08:03,283
We have something that looks And
it should actually not be

103
00:08:03,283 --> 00:08:07,547
positioned in the x and
y coordinate of the world.

104
00:08:07,547 --> 00:08:10,392
It should actually be located
somewhere in the grid.

105
00:08:10,392 --> 00:08:13,896
But, this way we could actually
start testing if to see,

106
00:08:13,896 --> 00:08:18,872
we're running into errors and how do we
can call this Ant into the world, right?

107
00:08:18,872 --> 00:08:22,647
So, first of all, if we run this,
we haven't called the ant yet so,

108
00:08:22,647 --> 00:08:24,093
we don't have any issue.

109
00:08:24,093 --> 00:08:28,746
So, let's create an instance of
this Ant in the world, right?

110
00:08:28,746 --> 00:08:34,604
So, the first thing we're going to

111
00:08:34,604 --> 00:08:40,673
do is import Langton_Ant_ Class

112
00:08:40,673 --> 00:08:45,495
as Langtons or LA, right?

113
00:08:45,495 --> 00:08:49,393
And then, we created one
instance of the grid here, right?

114
00:08:49,393 --> 00:08:53,624
And let's just make a comment.

115
00:08:53,624 --> 00:08:59,908
[SOUND] And here,
we're going to do the Ant.

116
00:08:59,908 --> 00:09:01,635
I'm going to make plural, because,

117
00:09:01,635 --> 00:09:04,406
we'll see that we could
potentially have more than one.

118
00:09:04,406 --> 00:09:08,246
Like one of the things that we couldn't
do, or at least the way we wrote first

119
00:09:08,246 --> 00:09:12,275
Ant, was written in such a way that
basically you're learning the algorithm.

120
00:09:12,275 --> 00:09:15,768
You're understanding how everything works
but it's very difficult to scale, right?

121
00:09:15,768 --> 00:09:19,975
We actually wrote it as a one-off version
of the script and someone looking

122
00:09:19,975 --> 00:09:24,404
at the script might think, well, this is
not a very efficient way of doing it.

123
00:09:24,404 --> 00:09:26,105
Because if you want to have two Ants,

124
00:09:26,105 --> 00:09:28,952
it's actually you have to kind
of duplicate a lot, right?

125
00:09:28,952 --> 00:09:30,977
You have to kind of,
change a lot of that script.

126
00:09:30,977 --> 00:09:35,267
Well, the object oriented version of this
script will give us the chance to have

127
00:09:35,267 --> 00:09:39,380
multiple instances of this Ant,
all of them operating under the same grid.

128
00:09:39,380 --> 00:09:40,969
And that's kind of what we want.

129
00:09:40,969 --> 00:09:47,444
So let's just create Ant_1

130
00:09:47,444 --> 00:09:53,146
= to LA.Langton_ Ant.

131
00:09:53,146 --> 00:10:00,021
And here, we will provide,
this is a constructor, right?

132
00:10:00,021 --> 00:10:01,819
So, let's just provide some data.

133
00:10:01,819 --> 00:10:06,409
And what are the data that we suggested
was important and needed for this class?

134
00:10:06,409 --> 00:10:13,579
Well, we said x,
let's just give a value of 50, right?

135
00:10:13,579 --> 00:10:19,340
Then y, it's going to value of 50,
direction.

136
00:10:19,340 --> 00:10:23,287
Let's just give it a value of 1.

137
00:10:23,287 --> 00:10:25,954
And, you can do everything in one line.

138
00:10:25,954 --> 00:10:31,411
But I just like, spacing out,
these variables just

139
00:10:31,411 --> 00:10:37,249
to make them a little bit more,
visible, grid object.

140
00:10:37,249 --> 00:10:40,690
So what is the grid object, right?

141
00:10:40,690 --> 00:10:44,137
So, the grid object is going to
be our main grid, right?

142
00:10:44,137 --> 00:10:48,170
So, this is where we're
going to be passing, right?

143
00:10:48,170 --> 00:10:50,937
One of the attributes of the Ant is, hey,

144
00:10:50,937 --> 00:10:54,271
give me a grid object that
I can work with, right?

145
00:10:54,271 --> 00:10:56,944
I need to live within a grid structure,

146
00:10:56,944 --> 00:11:01,405
which is kind of a requirement
of what we're intending, right?

147
00:11:01,405 --> 00:11:05,528
So we are actually referencing we're
passing, as a grid object, the main grid,

148
00:11:05,528 --> 00:11:06,738
that was created here.

149
00:11:06,738 --> 00:11:10,043
So now we have our Ant
we have satisfied all

150
00:11:10,043 --> 00:11:14,680
the different requirements
of the constructor, right?

151
00:11:14,680 --> 00:11:18,654
So we should have, an Ant_1 object, right?

152
00:11:18,654 --> 00:11:23,459
And we could test,
to see we can call some of its functions.

153
00:11:23,459 --> 00:11:26,702
So let's go and,

154
00:11:26,702 --> 00:11:31,682
do Ant_1.run, right?

155
00:11:31,682 --> 00:11:35,531
So, what we're trying to
do here is invoking the run

156
00:11:35,531 --> 00:11:40,444
function which in itself is going to
call the draw function, right?

157
00:11:40,444 --> 00:11:43,099
Which is currently only
drawing an ellipse but

158
00:11:43,099 --> 00:11:47,124
we're going to replace that code
with the actual calculation, right?

159
00:11:47,124 --> 00:11:51,182
So, let's just save this and
see we're running into errors, right?

160
00:11:51,182 --> 00:11:58,441
So we're not we have an instance of
the Ant here as you can see, is nowhere.

161
00:11:58,441 --> 00:12:02,592
It's actually quite
arbitrarily placed in space,

162
00:12:02,592 --> 00:12:07,870
because it's using its x and y,
which is in this case 50, 50,

163
00:12:07,870 --> 00:12:13,734
to place an ellipse, a red ellipse,
of an arbitrary size again, right?

164
00:12:13,734 --> 00:12:19,161
So, we do have an instance of that class,
in the world.

165
00:12:19,161 --> 00:12:25,626
Just for good measure we could
actually do the following which is,

166
00:12:25,626 --> 00:12:31,868
copy paste all this, and have,
two instances, of this class.

167
00:12:31,868 --> 00:12:39,188
So, I'm going to do maybe something like,
80, right?

168
00:12:39,188 --> 00:12:47,260
So, same data, but, basically a second
instance of that class, right?

169
00:12:47,260 --> 00:12:51,970
Let's call it Ant_ 2 and then,
we're going to call the function run for

170
00:12:51,970 --> 00:12:53,001
both of those.

171
00:12:53,001 --> 00:12:57,281
And, you'll see that we now have
two instances of the class, right?

172
00:12:57,281 --> 00:13:03,142
So, as you see, this is kind of the beauty
of working with object-oriented code,

173
00:13:03,142 --> 00:13:05,420
where we are writing one class.

174
00:13:05,420 --> 00:13:10,462
But it's rather easy to kind of
create a new instance of this class,

175
00:13:10,462 --> 00:13:15,329
give it different data,
maybe a different location to start, or

176
00:13:15,329 --> 00:13:18,621
perhaps even different behavior later on.

177
00:13:18,621 --> 00:13:24,847
And let them, operate independently and,
behave and relate to one another as well.

178
00:13:24,847 --> 00:13:28,030
So, we have two of these
Ants in the world, but

179
00:13:28,030 --> 00:13:33,118
they're actually as you can see here,
they're actually a pretty.

180
00:13:33,118 --> 00:13:38,701
Sorry, this is the grid class, but
let's just kind of focus on the Ant class.

181
00:13:38,701 --> 00:13:46,486
So, we're using a very placeholder
function for drawing, right?

182
00:13:46,486 --> 00:13:52,220
So, let's just draw,
a correct representation

183
00:13:52,220 --> 00:13:56,595
of what we want this Ant to be, right?

184
00:13:56,595 --> 00:14:00,711
So, we're going to keep the red color.

185
00:14:00,711 --> 00:14:05,559
Okay, so, in order to access some
information of how to draw in

186
00:14:05,559 --> 00:14:08,856
this Langton Ant correctly into the grid.

187
00:14:08,856 --> 00:14:11,446
As we do have a reference
to the grid object,

188
00:14:11,446 --> 00:14:15,305
we can actually access functions
from this grid object, right?

189
00:14:15,305 --> 00:14:19,269
And there's some functions that it
would be really useful to have.

190
00:14:19,269 --> 00:14:22,391
So I'm going to go back to
the grid object for a moment.

191
00:14:22,391 --> 00:14:26,024
And you see here we actually
got this get data, function,

192
00:14:26,024 --> 00:14:28,258
which is a kind of a handy function.

193
00:14:28,258 --> 00:14:31,722
But there's going to be a few others that
are going to be incredibly important for

194
00:14:31,722 --> 00:14:34,278
us to have and
it's going to make our life easier, right?

195
00:14:34,278 --> 00:14:38,579
So, let's just provide some kind of
functions that are not used internally,

196
00:14:38,579 --> 00:14:40,869
but they might be used by the Ant, right?

197
00:14:40,869 --> 00:14:46,331
So, let's create a function call index

198
00:14:46,331 --> 00:14:52,595
coordinate in x, and
it's going to use a self and

199
00:14:52,595 --> 00:14:56,788
an x, as an argument, right.

200
00:14:56,788 --> 00:15:00,917
So, we're going to return,
I'm going to be a very quick function.

201
00:15:00,917 --> 00:15:06,567
It's going to return the x times

202
00:15:06,567 --> 00:15:11,540
the self.screen_width

203
00:15:11,540 --> 00:15:16,063
divided by self.com.

204
00:15:16,063 --> 00:15:19,451
So, I don't want to be doing
this calculation in the Ant.

205
00:15:19,451 --> 00:15:24,076
I technically could have access to
all this information in the Ant, but,

206
00:15:24,076 --> 00:15:26,247
I want to be able to access one line.

207
00:15:26,247 --> 00:15:31,888
Which is, hey, if I give you an x value,
can you give me the information

208
00:15:31,888 --> 00:15:36,505
of what would be the coordinate
of that index in x, right?

209
00:15:36,505 --> 00:15:39,195
So if I give you the index value, right,

210
00:15:39,195 --> 00:15:43,167
I need to get the real-world
coordinates of that, right?

211
00:15:43,167 --> 00:15:45,902
So this would be the function for
doing so, right?

212
00:15:45,902 --> 00:15:49,339
Let's just do another one of those, and

213
00:15:49,339 --> 00:15:52,885
we're going to do that,
this for y as well.

214
00:15:52,885 --> 00:16:00,180
And again, these functions are going to be
very useful for us as we are converting.

215
00:16:00,180 --> 00:16:06,014
Converting index data into
real world coordinates, right?

216
00:16:07,514 --> 00:16:12,931
And making a graphic representation of
an entity that is operating within,

217
00:16:17,290 --> 00:16:19,914
Within an index of a grid, right?

218
00:16:19,914 --> 00:16:27,510
So let's go screen_height and rows, right?

219
00:16:32,334 --> 00:16:33,567
So those two functions,

220
00:16:33,567 --> 00:16:36,542
again, what I want to do,
like think about the ant, right?

221
00:16:36,542 --> 00:16:40,551
The ant wants to be drawn
using an index value like x,

222
00:16:40,551 --> 00:16:43,070
is going to be its own internal x.

223
00:16:43,070 --> 00:16:48,734
And this function is going to
return where in the grid?

224
00:16:48,734 --> 00:16:54,218
What are the world coordinates for
that grid position, right, for x and y?

225
00:16:54,218 --> 00:16:56,946
So what other functions might we need?

226
00:16:56,946 --> 00:17:02,444
One of the things that we actually do
here is the calculation of the cell size,

227
00:17:02,444 --> 00:17:06,202
right, which we use for
the size of the rectangle.

228
00:17:06,202 --> 00:17:12,118
But that might be some information that
it's not really known by the ant, right?

229
00:17:12,118 --> 00:17:17,320
So let's create a function
that will help understand

230
00:17:17,320 --> 00:17:21,522
the cell size for another entity, right?

231
00:17:21,522 --> 00:17:24,654
What internal is being called cell size,
right?

232
00:17:26,434 --> 00:17:29,780
Is something that could be accessed
by the ant as well, right?

233
00:17:29,780 --> 00:17:33,424
So let's return, in this case,

234
00:17:33,424 --> 00:17:39,551
self.screen_width divided by self.cols,
right?

235
00:17:39,551 --> 00:17:44,344
Which is the same division that we've

236
00:17:44,344 --> 00:17:48,837
done before to split the screen into

237
00:17:48,837 --> 00:17:53,822
the number of columns we have, right?

238
00:17:53,822 --> 00:17:58,677
If you can look into here,
this is the same calculation, but now,

239
00:17:58,677 --> 00:18:02,094
we're doing it as a kind of a function.

240
00:18:02,094 --> 00:18:04,494
And we could have written this as
a function in the first place.

241
00:18:04,494 --> 00:18:09,317
I guess that could have been slightly
more convenient way of doing it.

242
00:18:09,317 --> 00:18:16,604
But here we are, screen_height and rows.

243
00:18:18,384 --> 00:18:24,948
So I guess, my thought process here is
that these functions, I'm realizing,

244
00:18:24,948 --> 00:18:29,848
the ant will need to establish
communication with the grid.

245
00:18:29,848 --> 00:18:35,844
And in order to have that communication
be smoother, if you want,

246
00:18:35,844 --> 00:18:41,320
not really kind of involved a lot
of this math in the ant side.

247
00:18:41,320 --> 00:18:45,678
We can actually create these functions,
which are going to be

248
00:18:45,678 --> 00:18:50,614
almost messaging systems in a way
that we have between the and.

249
00:18:50,614 --> 00:18:53,854
Or the ant will be asking to agree that,
hey, what is your cell size?

250
00:18:53,854 --> 00:18:58,198
The cell size is actually
this calculation.

251
00:18:58,198 --> 00:19:02,590
If I give you an index, what is
the world coordinates for this index?

252
00:19:02,590 --> 00:19:04,758
And I'm going to get this back, right?

253
00:19:04,758 --> 00:19:06,838
That's the logic behind
what we're doing here.

254
00:19:06,838 --> 00:19:10,361
These are four different
messaging functions, and

255
00:19:10,361 --> 00:19:15,800
they're specifically needed for this
ant to be able to be displayed, right?

256
00:19:15,800 --> 00:19:20,363
As you think about it,
the ant wants to show in one of the cells,

257
00:19:20,363 --> 00:19:24,360
specifically in one of
the cells in the grid, right?

258
00:19:24,360 --> 00:19:27,266
So let's create the x
coordinate of the ant.

259
00:19:30,064 --> 00:19:38,678
It's going to be self.grid_object, Dot.

260
00:19:38,678 --> 00:19:42,909
And with the dot operator,
we're actually going to

261
00:19:42,909 --> 00:19:47,754
access the index coordinate,
right, index x, right?

262
00:19:49,094 --> 00:19:50,933
Of what?

263
00:19:50,933 --> 00:19:52,702
Here, we need to provide the argument,
right?

264
00:19:52,702 --> 00:19:59,566
So self, .x,
which is the ant x value, right?

265
00:19:59,566 --> 00:20:01,634
So that's the x coordinate, right?

266
00:20:04,824 --> 00:20:06,213
I'm going to just leave this for now,

267
00:20:06,213 --> 00:20:08,320
we're going to see there
is something missing here.

268
00:20:08,320 --> 00:20:13,542
And let's do this for the y,

269
00:20:16,456 --> 00:20:23,632
And coordinate y, and then self.y, right?

270
00:20:23,632 --> 00:20:27,080
So we have that.

271
00:20:27,080 --> 00:20:31,228
And then finally,
instead of using the x and y,

272
00:20:31,228 --> 00:20:36,624
which now we're going to consider
those to be index positions,

273
00:20:36,624 --> 00:20:39,753
not real world or world positions.

274
00:20:39,753 --> 00:20:44,454
We're going to use this
new variables here, right?

275
00:20:47,032 --> 00:20:48,114
So that's good.

276
00:20:49,214 --> 00:20:55,906
This slightly smaller,
just to kind of be able to see,

277
00:20:55,906 --> 00:21:01,294
or how these rectangles, or this ellipse.

278
00:21:03,034 --> 00:21:05,994
So we don't see them in the screen.

279
00:21:05,994 --> 00:21:10,602
That means that we're probably running
into a mistake at the moment, right?

280
00:21:10,602 --> 00:21:13,555
So let's see what is our
issue at the moment.

281
00:21:18,314 --> 00:21:23,954
Okay, the first thing we need
to check is what coordinates

282
00:21:23,954 --> 00:21:28,386
are we actually asking them to draw,
right?

283
00:21:28,386 --> 00:21:31,074
Let's just go into our grid.

284
00:21:31,074 --> 00:21:35,571
If we think about it,
our grid has 80 columns, and

285
00:21:35,571 --> 00:21:40,376
we are saying, well,
draw in the coordinate 50, but

286
00:21:40,376 --> 00:21:46,194
this is an index now,
it's not a world coordinate, right?

287
00:21:46,194 --> 00:21:47,594
So, and why?

288
00:21:47,594 --> 00:21:51,852
We only have 40 columns,
or sorry, rows, right?

289
00:21:51,852 --> 00:21:53,590
So we're actually kind of out of bounds.

290
00:21:53,590 --> 00:21:59,190
We're actually asking for something that
would not show up in the screen at all.

291
00:21:59,190 --> 00:22:00,870
So let's just do a five and a five.

292
00:22:00,870 --> 00:22:07,554
And in this case, 10 and
a 5, Right, let's see.

293
00:22:09,054 --> 00:22:16,650
So we can see here now these circles
appearing in these positions, right?

294
00:22:16,650 --> 00:22:21,403
So now we know that the x and y represent
indexes, one, two, three, four,

295
00:22:21,403 --> 00:22:22,450
five, right?

296
00:22:22,450 --> 00:22:28,934
The indices in the grid as opposed
to world coordinates, right?

297
00:22:30,634 --> 00:22:34,805
But it actually seems that
Ruby's cutting through,

298
00:22:34,805 --> 00:22:39,674
it's not really positioned very
neatly in the grid, right?

299
00:22:39,674 --> 00:22:43,644
So let's just correct a little bit
how this is actually displayed.

300
00:22:43,644 --> 00:22:46,995
And that's because we have to take
in consideration the cell size and

301
00:22:46,995 --> 00:22:49,316
how we're drawing that circle or
rectangle.

302
00:22:49,316 --> 00:22:53,844
Let's just switch it to a rectangle so

303
00:22:53,844 --> 00:22:59,556
that we have a little bit more control,
right?

304
00:22:59,556 --> 00:23:03,652
So one of the things that we're
missing is the cell size information.

305
00:23:03,652 --> 00:23:07,824
So let's call this cell_x.

306
00:23:08,984 --> 00:23:13,528
And the cell in x is going to be self.,

307
00:23:13,528 --> 00:23:18,024
same thing, grid_object.

308
00:23:18,024 --> 00:23:22,714
But we're going to use the second function
we created, right, if you remember,

309
00:23:22,714 --> 00:23:27,772
cell size in x, To

310
00:23:27,772 --> 00:23:32,000
calculate that.

311
00:23:32,000 --> 00:23:35,192
And we're going to say, well,
the cell size, that's a cell size,

312
00:23:35,192 --> 00:23:41,346
right, In x.

313
00:23:41,346 --> 00:23:45,174
And let's just get it in X and in Y.

314
00:23:50,594 --> 00:23:52,802
And we have the cell size.

315
00:23:52,802 --> 00:23:57,364
What we really need for the coordinate
to be right is that we need to add to

316
00:23:57,364 --> 00:24:00,390
the coordinate half of the cell in x and
half

317
00:24:00,390 --> 00:24:05,176
>> Half on the cell in y, right,

318
00:24:05,176 --> 00:24:10,347
just because we need to push it,

319
00:24:10,347 --> 00:24:15,325
half of its size into the grid,

320
00:24:15,325 --> 00:24:21,452
right, so cell size x divided by 2 and

321
00:24:21,452 --> 00:24:27,787
plus cell size y divided by 2, right?

322
00:24:27,787 --> 00:24:31,960
And we can actually use
the cell size as well for

323
00:24:31,960 --> 00:24:36,134
now we know that
the coordinate is going to be,

324
00:24:39,166 --> 00:24:45,124
Like so, and here,
let's just switch this to a rectangle.

325
00:24:46,784 --> 00:24:50,315
And let's also say that the rect mode,

326
00:24:50,315 --> 00:24:55,345
which is the mode by which we
are drawing the rectangle,

327
00:24:55,345 --> 00:25:00,554
just make clear that we
are using the center mode.

328
00:25:00,554 --> 00:25:02,691
You can look into the documentation
of processing, but

329
00:25:02,691 --> 00:25:04,802
there's different ways
of drawing a rectangle.

330
00:25:04,802 --> 00:25:08,895
We're going to be placing the x and
y coordinate in the center of

331
00:25:08,895 --> 00:25:13,723
the rectangle and expanding it in
both directions from there, right?

332
00:25:13,723 --> 00:25:16,474
Let's see what we get now.

333
00:25:16,474 --> 00:25:22,172
So you can see here now we have
two rectangles in the grid and

334
00:25:22,172 --> 00:25:28,822
they should be matching quite
accurately the grid itself, right?

335
00:25:28,822 --> 00:25:34,183
And that's what we wanted,
we actually wanted a system of a unit,

336
00:25:34,183 --> 00:25:39,414
a grid, sorry, an ant,
that feels like it belongs to the grid.

337
00:25:39,414 --> 00:25:44,576
And everything that we're going to do
from this position onwards is going to be

338
00:25:44,576 --> 00:25:49,702
demonstrating that the ants belong or
participate in a way to this grid.

339
00:25:49,702 --> 00:25:54,569
Let's just do what we have done in past
videos of just changing a little bit

340
00:25:54,569 --> 00:25:58,197
the resolution here maybe
to see this better, right?

341
00:25:58,197 --> 00:26:01,622
And as you can see,
as we change the resolution size

342
00:26:01,622 --> 00:26:06,654
to something a bit more visible for
us, the size of the ant also changes.

343
00:26:06,654 --> 00:26:10,376
And the index position,
it's always referring to,

344
00:26:10,376 --> 00:26:15,836
the coordinate we're providing is
actually based not on world coordinates,

345
00:26:15,836 --> 00:26:19,240
but it's actually index coordinates,
right?

346
00:26:19,240 --> 00:26:25,813
So we have the units in five and
five and then the ten and five, right?

347
00:26:25,813 --> 00:26:29,930
So we finally have a grid entity and
completed.

348
00:26:29,930 --> 00:26:35,324
And we also have an ant that is
displaying itself on the screen in and

349
00:26:35,324 --> 00:26:38,322
really taking in consideration its index

350
00:26:38,322 --> 00:26:42,810
information in order to kind
of displace itself correctly.

351
00:26:42,810 --> 00:26:45,797
So now we're actually ready to
start working on the behavior, so

352
00:26:45,797 --> 00:26:49,234
we're going to to continue on that in
the next lesson, so I'll see you then.