# Line Intersection Part 2 (optimized)

Actionscript:
1. // optimized line to line intersection test:
2. // from wikipedia: http://en.wikipedia.org/wiki/Line-line_intersection
3. // and http://local.wasp.uwa.edu.au/~pbourke/geometry/lineline2d/
4. var ip:Point = new Point();
5.
6. function intersection(p1:Sprite, p2:Sprite, p3:Sprite, p4:Sprite):Point {
7.     var nx:Number, ny:Number, dn:Number;
8.     var x4_x3:Number = p4.x - p3.x;
9.     var y4_y3:Number = p4.y - p3.y;
10.     var x2_x1:Number = p2.x - p1.x;
11.     var y2_y1:Number = p2.y - p1.y;
12.     var x1_x3:Number = p1.x - p3.x;
13.     var y1_y3:Number = p1.y - p3.y;
14.
15.     nx = x4_x3 * y1_y3 - y4_y3 * x1_x3;
16.     ny = x2_x1 * y1_y3 - y2_y1 * x1_x3;
17.     dn = y4_y3 * x2_x1 - x4_x3 * y2_y1;
18.
19.     nx /= dn;
20.     ny /= dn;
21.
22.     // has intersection
23.     if(nx>= 0 && nx <= 1 && ny>= 0 && ny <= 1){
24.         ny = p1.y + nx * y2_y1;
25.         nx = p1.x + nx * x2_x1;
26.         ip.x = nx;
27.         ip.y = ny;
28.     }else{
29.         // no intersection
30.         ip.x = ip.y = -1000;
31.     }
32.     return ip
33. }
34.
35.
36. //
37. // test out the function:
38. //
39.
40. stage.frameRate = 30;
41. var a:Sprite = dot();
42. a.x = a.y = 100;
43.
44. var b:Sprite = dot();
45. b.x = b.y = 200;
46.
47. var c:Sprite = dot();
48. c.x = 200
49. c.y = 100;
50.
51. var d:Sprite = dot();
52. d.x = 100
53. d.y = 200;
54.
55. var inter:Sprite = dot(0xFF0000,true);
56.
58. function onLoop(evt:Event):void {
59.     var p:Point = intersection(a, b, c, d);
60.     inter.x = p.x;
61.     inter.y = p.y
62.
63.     with(graphics){
64.         clear();
65.         lineStyle(0,0x000000);
66.         moveTo(a.x, a.y);
67.         lineTo(b.x, b.y);
68.         moveTo(c.x, c.y);
69.         lineTo(d.x,d.y);
70.     }
71. }
72.
73. // draggable dot
74. function dot(col:uint = 0x507399, noDrag:Boolean = false):Sprite {
75.     var s:Sprite = Sprite(addChild(new Sprite));
76.     with(s.graphics) beginFill(col), drawCircle(0,0,5);
77.     if (!noDrag){
78.         s.buttonMode = true;
80.     }
81.     return s;
82. }
83. function onDrag(evt:MouseEvent):void {
84.     evt.currentTarget.startDrag()
85. }
86.
88. function onUp(evt:MouseEvent):void{
89.     stopDrag();
90. }

This is a significantly optimized version of yesterdays post. The function intersection() takes four sprites as its arguments. The four arguments could be considered starting points and ending points for two line segments.

The bottleneck in yesterdays post looked something like this;

nx = (p1.x * p2.y - p1.y * p2.x) * (p3.x - p4.x) - (p1.x - p2.x) * (p3.x * p4.y - p3.y * p4.x);
dn = (p1.x - p2.x) * (p3.y - p4.y) - (p1.y - p2.y) * (p3.x - p4.x);
ny = (p1.x * p2.y - p1.y * p2.x) * (p3.y - p4.y) - (p1.y - p2.y) * (p3.x * p4.y - p3.y * p4.x);
return new Point(nx /dn, ny / dn);

nx and ny are the numerators for two fractions both with dn as their denominator.... after taking a look at this page:

http://local.wasp.uwa.edu.au/~pbourke/geometry/lineline2d/

We can simplify things a bit before we start wrapping repetitive math operations up in variables:

Actionscript:
1. nx = (p4.x - p3.x) * (p1.y - p3.y) - (p4.y - p3.y) * (p1.x - p3.x);
2. // ny = (p2.x - p1.x) * (p1.y - p3.y) - (p2.y - p1.y) * (p1.x - p3.x);
3. dn = (p4.y - p3.y) * (p2.x - p1.x) - (p4.x - p3.x) * (p2.y - p1.y);
4. nx /= dn;
5. // ny /= dn
6. ny = p1.y + nx * (p2.y - p1.y);
7. nx = p1.x + nx * (p2.x - p1.x);
8. return new Point(nx, ny);

It's important to note here that if we didn't intend to calculate line segments we could exclude the initial calculation of ny.

At this point we're ready to get rid of duplicate math operations by wrapping them in variables. In this case this only includes subtraction:

Actionscript:
1. var x4_x3:Number=p4.x-p3.x;
2. var y4_y3:Number=p4.y-p3.y;
3. var x2_x1:Number=p2.x-p1.x;
4. var y2_y1:Number=p2.y-p1.y;
5. var x1_x3:Number=p1.x-p3.x;
6. var y1_y3:Number=p1.y-p3.y;
7.
8. nx=x4_x3*y1_y3-y4_y3*x1_x3;
9. ny=x2_x1*y1_y3-y2_y1*x1_x3;
10. dn=y4_y3*x2_x1-x4_x3*y2_y1;
11.
12. nx/=dn;
13. ny/=dn;
14.
15. ny=p1.y+nx*y2_y1;
16. nx=p1.x+nx*x2_x1;
17.
18. return new Point(nx, ny);

After wrapping repetitive math into variables the only optimization left to be made is related to the instantiation of a new Point(). Instantiation of objects is an expensive process, so it's better to define the point once outside of the intersection() function definition.

In the end our function looks like this:

Actionscript:
1. var ip:Point = new Point();
2.
3. function intersection(p1:Sprite, p2:Sprite, p3:Sprite, p4:Sprite):Point {
4.     var nx:Number, ny:Number, dn:Number;
5.     var x4_x3:Number = p4.x - p3.x;
6.     var pre2:Number = p4.y - p3.y;
7.     var pre3:Number = p2.x - p1.x;
8.     var pre4:Number = p2.y - p1.y;
9.     var pre5:Number = p1.y - p3.y;
10.     var pre6:Number = p1.x - p3.x;
11.     nx = x4_x3 * pre5 - pre2 * pre6;
12.     ny = pre3 * pre5 - pre4 * pre6;
13.     dn = pre2 * pre3 - x4_x3 * pre4;
14.     nx /= dn;
15.     ny /= dn;
16.     // has intersection
17.     if(nx>= 0 && nx <= 1 && ny>= 0 && ny <= 1){
18.         ny = p1.y + nx * pre4;
19.         nx = p1.x + nx * pre3;
20.         ip.x = nx;
21.         ip.y = ny;
22.     }else{
23.         // no intersection
24.         ip.x = ip.y = -1000;
25.     }
26.     return ip
27. }

The final version contains a conditional that checks the end values of nx and ny, if they are between 0 and 1 then we know that there is an intersection between the segments.

Note: Optimized code usually breaks some best practices... as best practices are usually geared toward code flexibility and readability... and not speed.

• 