blog

GeometryMath 3 points in line test at Jozef Chúťka's blog

January 11, 2010

No comments

geometry GeometryMath Point

GeometryMath 3 points in line test

GeometryMath.isLine() static function tests 3 Points and returns true if these points are in line. Optional 4th parameter defines if you require point2 to be in the middle between 1 and 3. Function is not based on vector algorithm, but on triangle equation. This simple function was developed in order to optimize number of points necessary to draw some paths. To draw a line, you do not need all the points, you can omit all the middle points that are on the same line and the result looks the same – optimizes performance or storage.

isLine() function is a part of sk.yoz.math. GeometryMath class

public static function isLine(point1:Point, point2:Point, 
	point3:Point, orderSensitive:Boolean = true):Boolean
{
	var x1:Number = point1.x - point2.x;
	var x2:Number = point2.x - point3.x;
	var y1:Number = point1.y - point2.y;
	var y2:Number = point2.y - point3.y;
	
	if(orderSensitive && ((x1 > 0 && x2 < 0) || (x1 < 0 && x2 > 0) 
		|| (y1 < 0 && y2 > 0) || (y1 < 0 && y2 > 0)))
		return false;
	else if(!y2)
		return !y1;
	else if(!x2)
		return !x1;
	else
		return x1 / x2 == y1 / y2;
}

You may Leave a comment or Subscribe to Comments RSS or Trackback this entry.

Leave a comment

Please be polite and on topic. Your e-mail will never be published.