Line-Line Intersection in C++

November 3rd, 2008

I've just been tasked with creating a function to get the intersection of two lines.
With the help of equations from Wolfram Mathworld I created this nifty function:

Point* intersection(Point p1, Point p2, Point p3, Point p4) {
	// Store the values for fast access and easy
	// equations-to-code conversion
	float x1 = p1.x, x2 = p2.x, x3 = p3.x, x4 = p4.x;
	float y1 = p1.y, y2 = p2.y, y3 = p3.y, y4 = p4.y;
 
	// Get the denominator
	float denominator = (x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4);
	// If denominator is zero, there is no intersection
	if (denominator == 0) return NULL;
 
	// Get the x and y
	float pre = (x1*y2 - y1*x2), post = (x3*y4 - y3*x4);
	float x = ( pre * (x3 - x4) - (x1 - x2) * post ) / denominator;
	float y = ( pre * (y3 - y4) - (y1 - y2) * post ) / denominator;
 
	// Check if the x and y coordinates are within both lines
	if ( x < min(x1, x2) || x > max(x1, x2) ||
		x < min(x3, x4) || x > max(x3, x4) ) return NULL;
	if ( y < min(y1, y2) || y > max(y1, y2) ||
		y < min(y3, y4) || y > max(y3, y4) ) return NULL;	
 
	// Check if the lines are really intersecting
	float checkA = x * y1 - x * y2 - y * x1 + y * x2 +
		x1 * y2 - y1 * x2;
	float checkB = x * y3 - x * y4 - y * x3 + y * x4 +
		x3 * y4 - y3 * x4;
	if (checkA != 0 || checkB != 0) return NULL;
 
	// Return the point of intersection
	Point* ret = new Point();
	ret->x = x;
	ret->y = y;
	return ret;
}

Hope it will be to as good use to you as it is to me.

Swf Class Explorer for AS3

July 18th, 2008

The ApplicationDomain class in Actionscript 3 is a really handy class. It allows you to get any class from a loaded (or the current) swf and instantiate it.

ApplicationDomain currently has two public methods, hasDefinition(name:String):Boolean and getDefinition(name:String):Object. The simplicity of using these classes is a real treat, instantiating a class is as easy as

var myInstance:DisplayObject = new
(ApplicationDomain.currentDomain.getDefinition("com.flassari.MyClass"));

The biggest pain about the ApplicationDomain class is however its lack of a function for displaying all of the classes in a particular ApplicationDomain.
ApplicationDomain.getAllDefinitions():Array would be great! But unfortunately we must find our own way of getting around this flaw... so here it is!

I give you, the SwfClassExplorer class. Its usage is simple, as it has only one static function, getClasses(bytes:ByteArray):Array. To use it you must provide it with the bytes of the swf clip you want to explore. Here is an example of basic usage:

public function init():void {
	// Load the swf
	var urlLoader:URLLoader = new URLLoader();
	urlLoader.dataFormat = URLLoaderDataFormat.BINARY;
	urlLoader.addEventListener(Event.COMPLETE, onSwfLoaded);
	urlLoader.load(new URLRequest("pathToSwf"));
}
 
private function onSwfLoaded(e:Event):void {
	// Get the bytes from the swf
	var bytes:ByteArray = ByteArray(URLLoader(e.currentTarget).data);
	// And feed it to the SwfClassExplorer
	var traits:Array = SwfClassExplorer.getClasses(bytes);
	for each (var trait:Traits in traits) {
		trace(trait.toString()); // The full class name
	}
}

I've prepared an example to demonstrate how it works.
First we have the Primitives.swf file. It will act as our "MovieClip library". Its library consists of four movieclips, Box, Circle, Polystar and Triangle, each with its linkage class set to its library name:

library

Next we have the Example.swf file. It loads any swf file (Primitives.swf is in the path box by default) and lists all of the classes found in it. If the user clicks any of the classes, it will try to instantiate the class using no parameters (exceptions will be caught and displayed to the user) and if is or extends DisplayObject it will display it in the preview area.

Get Adobe Flash player

You can get the example files and source code here.

Iceland Socks Outtakes

July 9th, 2008

I wanted to share with you some takes that didn't make it into the final version of Iceland Socks. When you mix the good people of Gogogic with beverages and sock puppets you'll end up with more outtakes than usable footage.
Enjoy