02Geek HTML5 and JavaScript, TypeScript, React, Flash, ActionScript online School
Previous VideoNext Video

Finding the Direction

its time to create the getLine function to do that we need to know in what direction is our line changing is it through the x or through the y, for more watch this video ;)

Video Transcript

00:00So if you look back, and we just go back into that point where we're actually calling this function, where we're calling our get line function, which is right here, what we need to do is we need to get that dot before it changed.
00:12So what we could do, because we added this to the dot, we could just subtract it from the dot. So we could say dot x minus the direction x.
00:22Basically just going right opposite from the direction that we were.
00:26And same we could do for the y. Basically resetting the change that we just did, just to this new point that we're creating when we're calling the get line.
00:33Now if we try this one more time, now we're going to get a value that matches our y, which is exactly what we were hoping for.
00:42Now, the last thing that we have left here besides knowing the point is, like we were saying, we need to know if we're doing a test horizontally or vertically.
00:50Because if our test is vertical, if we know that our test is only vertical, we don't have to waste our time in searching for the dot in different positions.
01:00Well, if we know that our dot, basically knowing if our x changes or y changes, based on that we could know what is our direction.
01:10In other words, if we look into our application right now, and if I'm going this way, then I know my x is changing.
01:16If my x is changing, it means I'm going to find my starting point to be aligned with the two dots that have the same y.
01:26On the other hand, if my y is changing, I would know that I'm going to find my position, my starting point, to find, would be between two dots that have the same x.
01:35Because our x is not changing.
01:39So what we need to do here, we need to find here a way for us to be able to determine if we're going vertically or going horizontally.
01:47And lucky for us, that's easy enough because our x and y coordinates define it.
01:52So if when we call our change directory, we're calling a negative one or one, that means we're changing our x.
02:00And if we're calling the up or down, we're calling the y.
02:04So we know how to find those values.
02:07Now, the easiest way to do it, because we know that it has to be an or if kind of thing, it can't be both going in the x and the y.
02:15There's no diagonals in this application.
02:17We could use and leverage this information by sending to our function that we've just created just one more parameter.
02:24So our first parameter was this dot.
02:26The second parameter, and this is going to look a little bit weird, our second parameter could be our directory x not or equals equals zero.
02:40Or let's do it with y like that.
02:42We could say right here that in our line and let's see what this what this does.
02:46And here we're going to have your is is x, which is a Boolean.
02:53What does that mean?
02:54So first of all, if the y is zero, if the y is zero, what does that mean that the y is not changing?
03:02If the y is not changing, our direction is towards the x.
03:07Now, if our y is not changing, maybe this name isn't because what we care about is knowing the direction.
03:14So we know that is we know that is our x.
03:17We know that it's our directory.
03:21We know that our y is not changing because it's going to be the x or the opposite.
03:25So if it's the y that's not changing, if it's the y that's not changing, that means that our starting point is the y position.
03:36In other words, so if this is my this is my x and this is my y and I just started moving in my x.
03:45So I started moving with my y up when my y is changing.
03:49Like we said, what it what's going to stay solid or my x is going to stay remain the same.
03:54My x remains the same.
03:55I'll be able to find my x here and my x here.
03:58So whenever my y doesn't change, whenever my y is not changing, my x is changing.

Ready to Level Up Your Skills?

Join thousands of learners on 02GEEK and start your journey to becoming a coding expert today!

Enroll Now for Free!