No history yet

Connectivity and Boundaries

Transcript

Beau

Okay, Jo, I'm with you so far. We have a grid, we have colors, and we have this... this recursive idea of a function calling itself to spread the color. But I'm stuck on a really basic idea. When we say 'spread to the neighbors'... what exactly is a neighbor?

Jo

That is the perfect question to ask right now, because the computer doesn't know either. We have to tell it, very explicitly. And we basically have two main choices. It's called 4-way or 8-way connectivity.

Beau

Okay... let me guess. 4-way is up, down, left, right?

Jo

Exactly. Think of yourself standing on one square of a giant chessboard. The 4-way neighbors are the squares you could get to if you took one step north, south, east, or west. Just the ones sharing a full side with you.

Beau

So 8-way must be... oh, it includes the corners. The diagonals.

Jo

Yep. All eight squares immediately surrounding you. For our first implementation, we're just going to stick with the simpler one, 4-way. It's easier to reason about and it's super common. But now that raises another problem.

Beau

Of course it does.

Jo

Imagine our algorithm is running. It's on a pixel at the very top edge of the image. The coordinates are, let's say, row zero, column five. And our code says, 'Okay, now check your neighbor to the north... your neighbor up one row.' What happens?

Beau

Uh... there's no row above row zero. So it tries to check... row negative one? That can't be right. It falls off the edge of the world.

Jo

Exactly! And when a program tries to access a piece of memory that doesn't exist, like a negative array index... it crashes. Hard. So before we ever try to check a neighbor, we have to do what's called a boundary check.

Beau

Like a bouncer at a club. 'Sorry pal, you're not on the list.'

Jo

It's exactly like that. We use a simple 'if' statement in C++. We basically ask four questions before we proceed. Let's say we're checking a coordinate, we'll call it `x` for the column and `y` for the row.

Beau

Okay, I'm with you.

Jo

First, is the row `y` less than zero? If it is, stop. Don't even think about it. Second, is the column `x` less than zero? If so, stop. Those are our two 'floor' checks.

Beau

That handles falling off the top and the left side. What about the other sides?

Jo

Right. For that we need to know the size of our grid. Let's say our image is... 100 pixels wide and 80 pixels tall. So we have columns 0 through 99, and rows 0 through 79. That's a key thing to remember in C++ - arrays start at zero.

Beau

Zero to ninety-nine, not one to a hundred. Got it. That always feels a little weird.

Jo

It trips everyone up at first. So, our third check is: is `y` greater than or equal to 80? Because if it is, that's row 80, which doesn't exist. Our last row is 79. So if it's 80 or more, we stop.

Beau

And the fourth check must be for the width. Is `x` greater than or equal to 100? Because the last valid column is 99.

Jo

You got it. We put all those checks inside a single `if` statement. We say, basically, 'IF the `y` is greater than or equal to zero AND `x` is greater than or equal to zero AND `y` is less than the height AND `x` is less than the width... then, and only then, is it a safe coordinate to look at.'

Beau

So our little recursive function will have this bouncer check at the very top. The first thing it does is make sure the pixel it was just told to check is actually on the canvas.

Jo

Precisely. It becomes one of our base cases. Remember how we said recursion needs a 'stop' signal? This is one of them. If the coordinates are out of bounds, stop this branch of the recursion. Don't call yourself again. Just... return.

Beau

Okay, that makes a lot of sense. So we're not just stopping if the color is wrong, we're also stopping if we're about to step off a cliff.

Jo

It's the very first rule of robotics and algorithms: don't damage yourself and don't crash the program. Boundary checking is our number one safety net.