Crossing Lines

In many, many cases it can be helpful to know when a single point in space has crossed from one side of a line to another. It's the basis of quite a few algorithms actually.

It's easy enough when the line is aligned with the axis of the space it inhabits. If it is directly horizontal or vertical then we can just check if it is greater than a particular x or y value. But what if it's not? What if the line has been rotated in some way?

/* This is just a simple math formula that decides which side of a line a point (for our sake the origin of an object) is on. Useful for ai, rendering and many other little things. To see the return changed you'll have to grab the green ball in the flash document window and move it to the other side of the line. The return of "true" or "false" when you preview the project should change accordingly. Alternately to using shapes, you could just use a "point" object, which is just a set of x and y values, or x and y integers too. Whatever you like. */


function which_side (p1, p2, p3){		
	// p1 = The position of object to be tested.
	// p2 = The beginning point of the line direction.
	// p3 = The end point of line direction.
	
	// And the only line required in the function.  Yeah, it's that short.
	return ( (p3.x - p2.x) * (p1.y - p2.y) - 
			 (p3.y - p2.y) * (p1.x - p2.x) ) > 0;
			 			 
	// Basically we just return whether or not the number is above 0.
	
	// The test statement itself results in either a positive 
	// or negative number, depending on which side of the line
	// the two points are on.
}

You might ask, what is it that dictates whether or not the return of the function is "true" or "false". In this case the idea of something being true or false should be ignored. It is simply a simple, binary, way of conveying that what once was a particular way is now no longer that way.

If for whatever reason you need the return values to be reversed then it will just be a matter of reversing the order of the arguments given to the function. In this case we'd swap points 2 and 3 in the math to produce the opposite boolean value of what we currently get.

Normal Direction

x

x

Vector Normalization

Insetting polygons